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' 用這一句換上面那句也可以

根據(jù)自己android.support 版本去調(diào)整recycleView依賴版本

布局

? 實(shí)現(xiàn)布局:

<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>

? 實(shí)現(xiàn)子布局:

<?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>

?

自定義類

? 自定義類記載每個(gè)子布局上面的控件的數(shù)據(jù)信息:

public class MyCellClass {
    String text;
    int image;

    public MyCellClass(String text, int image) {
        this.text = text;
        this.image = image;
    }
}

適配器和ViewHolder

MyViewHolder用來記載每個(gè)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寫在一個(gè)文件中

? 寫一個(gè)適配器繼承 RecyclerView.Adapter<MyViewHolder>

? 需要繼承三個(gè)方法分別是

使用一個(gè)鏈表記錄所有子布局上面的所有數(shù)據(jù)信息:

private LinkedList<MyCellClass> cellList = new LinkedList<>();

可以在構(gòu)造函數(shù)將其賦值:

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函數(shù)</a>,負(fù)責(zé)承載每個(gè)子項(xiàng)的布局,目的是創(chuàng)建viewHolder

@Override
public MyViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
    View view = mInflater.inflate(R.layout.ceil_layout,viewGroup,false); //注意要寫第三個(gè)參數(shù)為false
    MyViewHolder myViewHolder = new MyViewHolder(view);
    return myViewHolder;
}

<a name="onBindViewHolder">onBindViewHolder函數(shù)</a>,負(fù)責(zé)將每個(gè)子項(xiàng)holder綁定數(shù)據(jù)。調(diào)用時(shí)機(jī)是item出現(xiàn)(或?qū)⒁霈F(xiàn))在屏幕上時(shí),這時(shí)需要向傳入的viewHolder中填充數(shù)據(jù)等

@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函數(shù)</a>getItemCount返回子布局?jǐn)?shù)量 可以給一個(gè)cellList.size()的返回值

@Override
public int getItemCount() {
    return cellList.size();
}

子布局點(diǎn)擊事件:

可以通過在onBindViewHolder中使用myViewHolder.itemView.setOnClickListener的方式添加

如果想在onClick中獲取點(diǎn)擊的子布局的index

  • onBindViewHolder的參數(shù)int i設(shè)置為final,這樣onClick就可以使用i
  • MyRecycleAdapter設(shè)置接口,再編寫邏輯代碼

適配器所有代碼在末尾

Activity中設(shè)置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();
    }
}

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容