1.RecylerView是什么
RecylerView是support-v7包中的新組件辽聊,和ListView一樣有item回收復(fù)用的功能,同時(shí),又是listView的升級(jí)版,它封裝了viewholder的回收復(fù)用搔涝,也就是說(shuō)RecylerView標(biāo)準(zhǔn)化了ViewHolder黍图,編寫Adapter面向的是ViewHolder而不再是View了曾雕;它提供了一種插拔式的體驗(yàn),高度的解耦助被,異常的靈活剖张,針對(duì)一個(gè)Item的顯示RecyclerView專門抽取出了相應(yīng)的類,來(lái)控制Item的顯示揩环,使其的擴(kuò)展非常強(qiáng)搔弄。
2.ViewHolder和Adapter
RecyclerView的任務(wù)僅限于回收和定位屏幕上的View。還有另外兩個(gè)類:ViewHoler和Adapter丰滑。
ViewHolder就是容納View視圖顾犹。
Adapter創(chuàng)建必要的ViewHoler,綁定ViewHoler至模型層數(shù)據(jù)
3.使用RecylerView
先要添加RecylerView依賴庫(kù),單擊File->Project Structure菜單項(xiàng)切換至項(xiàng)目結(jié)構(gòu)窗口褒墨,選擇左邊的app模塊炫刷,單機(jī)Dependencies選項(xiàng)頁(yè),單擊+按鈕添加依賴庫(kù)
也可以在build.gradle中添加郁妈,如圖
這里的例子是帖子信息顯示
我們可以新建一個(gè)fragment_post_list布局文件浑玛,修改根視圖為RecyclerView
<android.support.v7.widget.RecyclerView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/post_recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v7.widget.RecyclerView>
下面是視圖和fragment關(guān)聯(lián),新建PostListFragment文件噩咪,讓他的布局為RecyclerView
public class PostListFragment extends Fragment {
private RecyclerView mPostRecyclerView;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view=inflater.inflate(R.layout.fragment_post_list,container,false);
mPostRecyclerView=(RecyclerView)view.findViewById(R.id.post_recycler_view);
mPostRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
return view;
}
注意這個(gè)時(shí)候如果沒(méi)有LayoutManager我們的RecyclerView無(wú)法工作顾彰,會(huì)導(dǎo)致應(yīng)用崩潰,我們應(yīng)該創(chuàng)建完成后立即交給LayoutManager
現(xiàn)在需要初始的Activity文件中托管我們的fragment文件
public class PostListActivity extends AppCompatActivity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fragment);
FragmentManager fm=getSupportFragmentManager();
Fragment fragment=fm.findFragmentById(R.id.fragment_container);
if(fragment==null){
fragment=new PostListFragment();
fm.beginTransaction()
.add(R.id.fragment_container,fragment)
.commit();
}
}
}
然后運(yùn)行發(fā)現(xiàn)是一個(gè)空的界面
3.1列表項(xiàng)視圖
我們的列表里面有很多的數(shù)據(jù)胃碾,我們要?jiǎng)?chuàng)建一個(gè)視圖布局一條條根據(jù)自己的需要拜訪它
新建一個(gè)list_item_crime文件
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="8dp">
<TextView
android:id="@+id/post_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Post Title"/>
<TextView
android:id="@+id/post_date"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Post Date"/>
</LinearLayout>
3.2實(shí)現(xiàn)ViewHolder涨享、Adapter
我們需要實(shí)例化并使用剛剛新建的list_item_crime布局
定義ViewHolder(PostListFragment.java)
private class PostHolder extends RecyclerView.ViewHolder{
public PostHolder(LayoutInflater inflater,ViewGroup parent){
super(inflater.inflate(R.layout.list_item_post,parent,false));
}
}
定義Adapter(PostListFragment.java)
private class PostAdapter extends RecyclerView.Adapter<PostHolder>{
private List<Post> mPosts;
public PostAdapter(List<Post> posts){
mPosts=posts;
}
@Override
public PostHolder onCreateViewHolder(ViewGroup parent, int viewType) {
LayoutInflater layoutInflater=LayoutInflater.from(getActivity());
return new PostHolder(layoutInflater,parent);
}
@Override
public void onBindViewHolder(PostHolder holder, int position) {
}
@Override
public int getItemCount() {
return mPosts.size();
}
}
接下來(lái)我們要將Adapter和RecyclerView關(guān)聯(lián)起來(lái),實(shí)現(xiàn)一個(gè)設(shè)置PostListFragment用戶界面的updateUI方法书在,創(chuàng)建PostAdapter灰伟,然后設(shè)置給RecyclerView
public class PostListFragment extends Fragment {
...
private PostAdapter mAdapter;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view=inflater.inflate(R.layout.fragment_post_list,container,false);
mPostRecyclerView=(RecyclerView)view.findViewById(R.id.post_recycler_view);
mPostRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
updateUI();
return view;
}
private void updateUI(){
PostLab postLab=PostLab.get(getActivity());
List<Post> posts=postLab.getPosts();
mAdapter=new PostAdapter(posts);
mPostRecyclerView.setAdapter(mAdapter);
}
}
3.2綁定列表項(xiàng)
讓java代碼和組件關(guān)聯(lián)起來(lái)。
private class PostHolder extends RecyclerView.ViewHolder {
private Post mPost;
private TextView mTitleTextView;
private TextView mDateTextView;
public void bind(Post post){
mPost=post;
mTitleTextView.setText(mPost.getTitle());
mDateTextView.setText(mPost.getDate().toString());
}
public PostHolder(LayoutInflater inflater,ViewGroup parent){
super(inflater.inflate(R.layout.list_item_post,parent,false));
mTitleTextView=(TextView) itemView.findViewById(R.id.post_title);
mDateTextView=(TextView) itemView.findViewById(R.id.post_date);
}
現(xiàn)在只要取到一個(gè)Post,PostHolder就會(huì)刷新顯示TextView標(biāo)題視圖和TextView日期視圖儒旬,記得調(diào)用bind(Post)方法
@Override
public void onBindViewHolder(PostHolder holder, int position) {
Post post=mPosts.get(position);
holder.bind(post);
}
運(yùn)行
3.2響應(yīng)點(diǎn)擊
現(xiàn)在先達(dá)到點(diǎn)擊一下彈出一個(gè)toast的效果栏账,RecycleView功能強(qiáng)大,但是要自己處理觸摸事件栈源,雖然它也能幫我們轉(zhuǎn)發(fā)觸摸事件挡爵,不過(guò)大多數(shù)時(shí)候還是沒(méi)必要這么做的
通過(guò)修改PostHolder類處理用戶點(diǎn)擊事件
private class PostHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
public PostHolder(LayoutInflater inflater,ViewGroup parent){
super(inflater.inflate(R.layout.list_item_post,parent,false));
mTitleTextView=(TextView) itemView.findViewById(R.id.post_title);
mDateTextView=(TextView) itemView.findViewById(R.id.post_date);
itemView.setOnClickListener(this);
}
@Override
public void onClick(View view) {
Toast.makeText(getActivity(),mPost.getTitle()+" clicked!",Toast.LENGTH_SHORT).show();
}
}
點(diǎn)擊某個(gè)列表項(xiàng),可看到彈出的toast響應(yīng)消息