关于布局
1.添加依赖
2.添加<android.support.v7.widget.RecyclerView></android.support.v7.widget.RecyclerView>
关于Activity
1.初始化RecyclerView
2.创建RecyclerView适配器
1)继承RecyclerView.Adapter<MyViewHolder>泛型为自定义内部类ViewHolder
2)创建内部类ViewHolder 继承于RecyclerView.ViewHolder 在这里自定义布局加载控件
class MyViewHolder extends RecyclerView.ViewHolder{
public final TextView tv;
public MyViewHolder(View itemView) {
super(itemView);
tv = (TextView)itemView.findViewById(R.id.tv);
}
3)构造方法中存入数据与上下文对象
4)重写三个方法
①创建RecyclerView的布局并返回viewHolder
LayoutInflater mLayoutInflater=LayoutInflater.from(mContent);
View inflate = mLayoutInflater.inflate(R.layout.recy_item, parent,false);
MyViewHolder myViewHolder = new MyViewHolder(inflate);//把条目的布局传递给viewholer
return myViewHolder;
②给控件传入数据
holder.tv.setText(arrayList.get(position));
③设置RecyclerView的条目个数
return arrayList.size();
5)管理使用布局及给适配器传入上下文及数据
recyclerView.setLayoutManager(new LinearLayoutManager(mContent));//布局管理者;布局样式这个是和listview一样的
recyclerView.setAdapter(new MyAdapter(mContent,arrayList));
6)使用布局样式可参考张宏洋博客
链接:http://blog.csdn.net/lmj623565791/article/details/45059587
2)创建内部类ViewHolder 继承于RecyclerView.ViewHolder 在这里自定义布局加载控件
class MyViewHolder extends RecyclerView.ViewHolder{
public final TextView tv;
public MyViewHolder(View itemView) {
super(itemView);
tv = (TextView)itemView.findViewById(R.id.tv);
}
3)构造方法中存入数据与上下文对象
4)重写三个方法
①创建RecyclerView的布局并返回viewHolder
LayoutInflater mLayoutInflater=LayoutInflater.from(mContent);
View inflate = mLayoutInflater.inflate(R.layout.recy_item, parent,false);
MyViewHolder myViewHolder = new MyViewHolder(inflate);//把条目的布局传递给viewholer
return myViewHolder;
②给控件传入数据
holder.tv.setText(arrayList.get(position));
③设置RecyclerView的条目个数
return arrayList.size();
5)管理使用布局及给适配器传入上下文及数据
recyclerView.setLayoutManager(new LinearLayoutManager(mContent));//布局管理者;布局样式这个是和listview一样的
recyclerView.setAdapter(new MyAdapter(mContent,arrayList));
6)使用布局样式可参考张宏洋博客
链接:http://blog.csdn.net/lmj623565791/article/details/45059587
来源:https://www.cnblogs.com/livelihood/p/6758731.html