美文网首页
RecycleView

RecycleView

作者: Maiiiiiiiiiiiid | 来源:发表于2019-05-02 17:08 被阅读0次

    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();
        }
    }
    
    

    相关文章

      网友评论

          本文标题:RecycleView

          本文链接:https://www.haomeiwen.com/subject/gggnnqtx.html