1、在布局文件中声明
<android.support.v7.widget.RecyclerView
android:id="@+id/my_recycler_view"
android:scrollbars="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
2、在Activity中使用
// 1,找到这个View
mRecyclerView = (RecyclerView)findViewById(R.id.my_recycler_view); // 2,设置布局管理LayoutManager mLayoutManager = new LinearLayoutManager(this); mRecyclerView.setLayoutManager(mLayoutManager); // 3,(可选)如果可以确定每个item的高度是固定的,设置这个选项可以提高性能 mRecyclerView.setHasFixedSize(true); // 4,设置Adapter mRecyclerView.setAdapter(new MyAdapter());
目前SDK中提供了三种自带的LayoutManager:
-
LinearLayoutManager
-
GridLayoutManager
-
StaggeredGridLayoutManager
LinearLayoutManager
mLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
GridLayoutManager
mLayoutManager = new GridLayoutManager(context,columNum); mRecyclerView.setLayoutManager(mLayoutManager);
注:在Grid布局中也可以设置列表的Orientation属性,来实现横向和纵向的Grid布局。
StaggeredGridLayoutManager
瀑布流就使用StaggeredGridLayoutManager吧,具体使用方法见http://blog.csdn.net/duanymin/article/details/44979355
3、Adapter的创建
1 public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> { 2 public String[] datas = null; 3 public MyAdapter(String[] datas) { 4 this.datas = datas; 5 } 6 //创建新View,被LayoutManager所调用 7 @Override 8 public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) { 9 View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item,viewGroup,false); 10 ViewHolder vh = new ViewHolder(view); 11 return vh; 12 } 13 //将数据与界面进行绑定的操作 14 @Override 15 public void onBindViewHolder(ViewHolder viewHolder, int position) { 16 viewHolder.mTextView.setText(datas[position]); 17 } 18 //获取数据的数量 19 @Override 20 public int getItemCount() { 21 return datas.length; 22 } 23 //自定义的ViewHolder,持有每个Item的的所有界面元素 24 public static class ViewHolder extends RecyclerView.ViewHolder { 25 public TextView mTextView; 26 public ViewHolder(View view){ 27 super(view); 28 mTextView = (TextView) view.findViewById(R.id.text); 29 } 30 } 31 }
4、添加单击事件
// 1 定义接口 public static interface OnRecyclerViewItemClickListener { void onItemClick(View view , DataModel data); } // 2 添加接口和设置接口的方法 private OnRecyclerViewItemClickListener mOnItemClickListener = null; public void setOnItemClickListener(OnRecyclerViewItemClickListener listener) { this.mOnItemClickListener = listener; } // 3 在Adapter实现OnClickListener方法 public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> implements View.OnClickListener{ @Override public ViewHolder onCreateViewHolder(ViewGroup viewGroup, final int i) { View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item, viewGroup, false); ViewHolder vh = new ViewHolder(view); //将创建的View注册点击事件 view.setOnClickListener(this); return vh; } @Override public void onBindViewHolder(ViewHolder viewHolder, final int i) { viewHolder.mTextView.setText(datas.get(i).title); //将数据保存在itemView的Tag中,以便点击时进行获取 viewHolder.itemView.setTag(datas.get(i)); } ... @Override public void onClick(View v) { if (mOnItemClickListener != null) { //注意这里使用getTag方法获取数据 } ... }
在Activity中
mAdapter = new MyAdapter(getDummyDatas()); mRecyclerView.setAdapter(mAdapter); mAdapter.setOnItemClickListener(new MyAdapter.OnRecyclerViewItemClickListener() { @Override public void onItemClick(View view, DataModel data) { //DO your fucking bussiness here! } });
5、Item动画
// 设置item动画 mRecyclerView.setItemAnimator(new DefaultItemAnimator());
如果要自定义动画效果需要继承ItemAnimator
GitHub上的动画效果:RecyclerViewItemAnimators
6、更新数据
这里更新数据集不是用adapter.notifyDataSetChanged()
而是 notifyItemInserted(position)
与notifyItemRemoved(position)
否则没有动画效果。
为adapter中添加两个方法:
public void addData(int position) { mDatas.add(position, "Insert One"); notifyItemInserted(position); } public void removeData(int position) { mDatas.remove(position); notifyItemRemoved(position); }