RecycleView

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依赖版本

布局

​ 实现布局:



​ 实现子布局:



    
        
        
    

自定义类

​ 自定义类记载每个子布局上面的控件的数据信息:

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

​ 需要继承三个方法分别是

  • onCreateViewHolder
  • onBindViewHolder
  • getItemCount

使用一个链表记录所有子布局上面的所有数据信息:

private LinkedList 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);
}

onCreateViewHolder函数,负责承载每个子项的布局,目的是创建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;
}

onBindViewHolder函数,负责将每个子项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);
}

getItemCount函数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);

public class MyRecycleAdapter extends aRecyclerView.Adapter{
    private LinkedList 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();
    }
}

你可能感兴趣的:(RecycleView)