RecycleView
[TOC]
需要以下依赖:
implementation 'com.android.support:appcompat-v7:28.0.0' implementation 'com.android.support:recyclerview-v7:28.0.0' //implementation 'com.android.support:design:28.0.0' 用这一句换上面那句也可以
根据自己android.support 版本去调整recycleView依赖版本
布局
实现布局:
<android.support.v7.widget.RecyclerView
android:id="@+id/rv"
android:background="@color/colorPrimary"
android:layout_margin="10dp"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v7.widget.RecyclerView>
实现子布局:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/cell_bg"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="50dp"
android:layout_margin="10dp"
android:gravity="center">
<ImageView
android:id="@+id/iv_icon"
android:src="@drawable/icon_computer"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/tv_text"
android:layout_marginTop="5dp"
android:gravity="center"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:text="computer"
android:layout_below="@+id/iv_icon"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</RelativeLayout>
</RelativeLayout>
自定义类
自定义类记载每个子布局上面的控件的数据信息:
public class MyCellClass {
String text;
int image;
public MyCellClass(String text, int image) {
this.text = text;
this.image = image;
}
}
适配器和ViewHolder
MyViewHolder用来记载每个
itemView
的控件信息:
class MyViewHolder extends RecyclerView.ViewHolder {
TextView tv;
ImageView iv;
RelativeLayout bg;
public MyViewHolder(View itemView) {
super(itemView);
bg = (RelativeLayout) itemView.findViewById(R.id.cell_bg);
iv = (ImageView)itemView.findViewById(R.id.iv_icon);
tv = (TextView)itemView.findViewById(R.id.tv_text);
}
}
ps :我将适配器和
MyViewHolder
写在一个文件中
写一个适配器继承 RecyclerView.Adapter<MyViewHolder>
需要继承三个方法分别是
使用一个链表记录所有子布局上面的所有数据信息:
private LinkedList<MyCellClass> cellList = new LinkedList<>();
可以在构造函数将其赋值:
public MyRecycleAdapter(Context context) {
cellList.add(new MyCellClass("1",R.drawable.icon_computer));
cellList.add(new MyCellClass("1",R.drawable.icon_computer));
cellList.add(new MyCellClass("1",R.drawable.icon_computer));
cellList.add(new MyCellClass("1",R.drawable.icon_computer));
cellList.add(new MyCellClass("1",R.drawable.icon_computer));
this.context = context;
mInflater = LayoutInflater.from(context);
}
<a name="onCreateViewHolder">onCreateViewHolder
函数</a>,负责承载每个子项的布局,目的是创建viewHolder
@Override
public MyViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View view = mInflater.inflate(R.layout.ceil_layout,viewGroup,false); //注意要写第三个参数为false
MyViewHolder myViewHolder = new MyViewHolder(view);
return myViewHolder;
}
<a name="onBindViewHolder">onBindViewHolder
函数</a>,负责将每个子项holder绑定数据。调用时机是item出现(或将要出现)在屏幕上时,这时需要向传入的viewHolder中填充数据等
@Override
public void onBindViewHolder(MyViewHolder myViewHolder, final int i)
{
MyCellClass cellClass = cellList.get(i);
myViewHolder.tv.setText(cellClass.text);
myViewHolder.iv.setImageResource(cellClass.image);
}
<a name="getItemCount">getItemCount
函数</a>getItemCount
返回子布局数量 可以给一个cellList.size()
的返回值
@Override
public int getItemCount() {
return cellList.size();
}
子布局点击事件:
可以通过在onBindViewHolder
中使用myViewHolder.itemView.setOnClickListener
的方式添加
如果想在onClick
中获取点击的子布局的index
- 将
onBindViewHolder
的参数int i
设置为final
,这样onClick
就可以使用i
- 给
MyRecycleAdapter
设置接口,再编写逻辑代码
适配器所有代码在末尾
在Activity中设置RecycleView和适配器:
adapter = new MyRecycleAdapter(getApplicationContext());
rv.setAdapter(adapter);
gridLayoutManager = new GridLayoutManager(getApplicationContext(), 4);//(Context,spanCount)
rv.setLayoutManager(gridLayoutManager);
<a name="end"></a>
public class MyRecycleAdapter extends aRecyclerView.Adapter<MyViewHolder>{
private LinkedList<MyCellClass> cellList = new LinkedList<>();
private Context context;
private LayoutInflater mInflater;
private final static String TAG = "MyRecycleAdapter";
public MyRecycleAdapter(Context context) {
cellList.add(new MyCellClass("1",R.drawable.icon_computer));
cellList.add(new MyCellClass("1",R.drawable.icon_computer));
cellList.add(new MyCellClass("1",R.drawable.icon_computer));
cellList.add(new MyCellClass("1",R.drawable.icon_computer));
cellList.add(new MyCellClass("1",R.drawable.icon_computer));
this.context = context;
mInflater = LayoutInflater.from(context);
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View view = mInflater.inflate(R.layout.ceil_layout,viewGroup,false);
MyViewHolder myViewHolder = new MyViewHolder(view);
return myViewHolder;
}
@Override
public void onBindViewHolder(MyViewHolder myViewHolder, final int i) {
MyCellClass cellClass = cellList.get(i);
myViewHolder.tv.setText(cellClass.text);
myViewHolder.iv.setImageResource(cellClass.image);
myViewHolder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.i(TAG," This is :" + i);
}
});
}
@Override
public int getItemCount() {
return cellList.size();
}
}
网友评论