- Android Studio依赖RecyclerView, 在使用该控件的module节点下的build.gradle中的dependencies内添加compile 'com.android.support:recyclerview-v7:25.3.1',使用gradle同步后即可使用。创建RecyclerView对象
RecyclerView mRecyclerView =
(RecyclerView) view.findViewById(R.id.recycler_view);
- 设置布局格式,可选参数LinearManager(线性布局,与ListView相似)、GridLayoutManager(表格布局,与GridView相似)、StaggeredGridLayoutManager(瀑布流布局)。
mRecyclerView .setLayoutManager(new LinearLayoutManager(this));
- 创建适配器对象,新建适配器类MyAdapter,然后继承自RecyclerView.ViewHolder的内部类MyViewHolder,并实现其构造方法。
public class MyAdapter {
public class MyViewHolder extends RecyclerView.ViewHolder{
public MyViewHolder(View itemView) {
super(itemView);
}
}
}
- 接下来使适配器MyAdapter继承RecyclerView.Adapter<MyViewHolder>,其中的泛型参数是上一步中自己定义的MyViewHolder类。
public class MyAdapter extends RecyclerView.Adapter<MyViewHolder>{
@Override public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
// 绑定布局
return null;// 返回一个MyViewHolder对象
}
@Override public void onBindViewHolder(MyViewHolder holder, int position) {
// 为holder对象中的条目设置数据
}
@Override public int getItemCount() {
return 0;// 返回条目总数
}
public class MyViewHolder extends RecyclerView.ViewHolder{
public MyViewHolder(View itemView) {
super(itemView);
}
}
}
- 在MyAdapter中的onCreateViewHolder方法中要加载布局因此要传入Context,在getItemCount中要获取条目总数因此要传入List。因此创建MyAdapter的构造方法,含有参数为Context,List<String>,其中泛型可自己更改为自己想要的数据类型。
private Context mContext;
private List<String> mList;
public MyAdapter(Context context,List<String> list){
this.mContext = context;
this.mList = list;
}
- 创建Recycler条目布局item_recycler_view.xml。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</LinearLayout>
- 在MyAdapter中的onCreateViewHolder加载布局,并创建MyViewHolder对象。
@Override public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(mContext).inflate(R.layout.item_recycler_view,null);
return new MyViewHolder(view);
}
- 在MyViewHolder的构造方法中来初始化布局中所用到的控件。至此,MyViewHolder类编写完成。
public class MyViewHolder extends RecyclerView.ViewHolder {
private TextView textView;
public MyViewHolder(View itemView) {
super(itemView);
textView = (TextView) itemView.findViewById(R.id.textView);
}
}
- 在MyAdapter中的getItemCount函数内,返回mList.size()。
@Override public int getItemCount() {
return mList.size();
}
- 在MyAdapter中的onBindViewHolder函数内进行对控件的数据绑定。
@Override public void onBindViewHolder(MyViewHolder holder, int position) {
holder.textView.setText(mList.get(position));
}
- 在主Activity中为List<String> 填充数据,此处为模拟因此在类初始化的时候在静态代码块中添加。
private static List<String> mList;
static {
mList = new ArrayList<>();
for (int i=0;i<30;i++){
mList.add("条目--------"+i);
}
}
- 接下来在 mRecyclerView.setLayoutManager(new LinearLayoutManager(this)); 后创建MyAdapter对象, 并为RecyclerView设置适配器。
MyAdapter mAdapter = new MyAdapter(this, mList);
mRecyclerView.setAdapter(mAdapter);
- 为RecyclerView中的条目设置条目间距。创建SpaceItemDecoration继承自RecyclerView.ItemDecoration,并且重写getItemOffsets,从该类的构造函数中传入left,right,top,bottom的间距。
public class SpaceItemDecoration extends RecyclerView.ItemDecoration {
private int top;
private int bottom;
private int left;
private int right;
public SpaceItemDecoration(int top,int bottom,int left,int right) {
this.top = top;
this.bottom = bottom;
this.left = left;
this.right = right;
}
@Override public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
outRect.top = top; // 设置上边距
outRect.bottom = bottom; // 设置下边距
outRect.left = left; // 设置左边距
outRect.right = right; // 设置右边距
}
}
- 在mRecyclerView.setAdapter(mAdapter)之前调用mRecyclerView.addItemDecoration(new SpaceItemDecoration(2,2,2,2));方法
mRecyclerView = (RecyclerView) findViewById(R.id.recycler_view);
mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
MyAdapter mAdapter = new MyAdapter(this, mList);
mRecyclerView.addItemDecoration(new SpaceItemDecoration(20,20,20,20));
mRecyclerView.setAdapter(mAdapter);