欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

班门弄斧之浅谈androidx recyclerview

程序员文章站 2022-06-08 12:17:24
...

班门弄斧之浅谈androidx.recyclerview

基本用法

首先当然是添加RecyclerView 依赖库

implementation 'androidx.recyclerview:recyclerview:1.0.0'

RecylerView的要素

对于listView的使用大家应该比较熟悉,RecylerView有许多相似之处
还是来看看一张图。班门弄斧之浅谈androidx recyclerview与ListView类似,需要数据集合、item布局、适配器等零部件构成。数据集合与item布局通过适配器Adapter适配到RecylerView里面去
班门弄斧之浅谈androidx recyclerview

使用步骤

  1. 首先来创建一个RecylerViewActivity,并且创建他的布局文件activity_recylerview.xml;
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <Button
            android:id="@+id/btn_add"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="添加"
            android:textAllCaps="false" />
        <Button
            android:id="@+id/btn_delete"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="删除"
            android:textAllCaps="false" />
        <Button
            android:id="@+id/btn_list"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="List"
            android:textAllCaps="false" />
        <Button
            android:id="@+id/btn_grid"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Grid"
            android:textAllCaps="false" />
        <Button
            android:id="@+id/btn_flow"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="flow"
            android:textAllCaps="false" />
    </LinearLayout>


    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recyclerview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>


</LinearLayout>
  1. 对布局中的控件初始化,并设置点击事件;
//初始化控件
    private void initView() {
        btn_add = findViewById(R.id.btn_add);
        btn_delete = findViewById(R.id.btn_delete);
        btn_list = findViewById(R.id.btn_list);
        btn_grid = findViewById(R.id.btn_grid);
        btn_flow = findViewById(R.id.btn_flow);
        recyclerView = findViewById(R.id.recyclerview);

        //设置点击事件
        btn_add.setOnClickListener(this);
        btn_delete.setOnClickListener(this);
        btn_list.setOnClickListener(this);
        btn_grid.setOnClickListener(this);
        btn_flow.setOnClickListener(this);
    }
@Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btn_add:
            break;
            case R.id.btn_delete:
                break;
            case R.id.btn_list:
                break;
            case R.id.btn_grid:
                break;
            case R.id.btn_flow:
                break;

        }
    }
  1. 对数据集合进行准备;
    这里我创建了一个实体类Shopinfo.java
public class Shopinfo {
    private int icon;
    private String name;
    private  String content;

    public Shopinfo(int icon, String name, String content) {
        this.icon = icon;
        this.name = name;
        this.content = content;
    }

    public Shopinfo() {
    }

    public int getIcon() {
        return icon;
    }

    public void setIcon(int icon) {
        this.icon = icon;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    @Override
    public String toString() {
        return "Shopinfo{" +
                "icon=" + icon +
                ", name='" + name + '\'' +
                ", content='" + content + '\'' +
                '}';
    }
}

装载数据集合
private ArrayList datas;

private void initData() {
        //准备数据集合
        datas = new ArrayList<Shopinfo>();
        for (int i = 0; i < 10; i++) {

            datas.add(new Shopinfo(R.drawable.icon0+i,"name--"+i,"content--"+i));
        }
    }
  1. 设置RecylerView的适配器;(关键)
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;
import java.util.List;

public class MyRecylerViewAdapter extends RecyclerView.Adapter<MyRecylerViewAdapter.ViewHolder> {
    private  Context context;
    private  ArrayList<Shopinfo> datas;

    public MyRecylerViewAdapter(Context context, List<Shopinfo> datas) {
        this.context = context;
        this.datas = (ArrayList<Shopinfo>) datas;
    }

    //相当于getView方法中创建Viewholer
    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View itemView = View.inflate(context,R.layout.item_recyclerview,null);
        return new ViewHolder(itemView);
    }
    //数据和View绑定
    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        //根据位置得到对应的数据
        Shopinfo data = datas.get(position);
        holder.iv_icon.setImageResource(data.getIcon());
        holder.name.setText(data.getName());
        holder.content.setText(data.getContent());



    }
    //得到总条数
    @Override
    public int getItemCount() {
        return datas.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        private ImageView iv_icon;
        private TextView name;
        private TextView content;
         public ViewHolder(@NonNull View itemView) {
             super(itemView);
             iv_icon = itemView.findViewById(R.id.iv_icon);
             name = itemView.findViewById(R.id.name);
             content = itemView.findViewById(R.id.content);
         }
     }
}

MyRecylerViewAdapter 继承自RecyclerView.Adapter<MyRecylerViewAdapter.ViewHolder>
在写Adapter时不妨先创建ViewHolder类并重写RecyclerView.ViewHolder当中的方法。

接下来就是分别重写RecyclerView.Adapter中的onCreateViewHolder、onBindViewHolder、getItemCount三大方法。

在onCreateViewHolder()中
@NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View itemView = View.inflate(context,R.layout.item_recyclerview,null);
        return new ViewHolder(itemView);
    }

调用inflate从XML资源中展现视图。创建item的布局文件item_recyclerview.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@android:color/white"
    android:padding="5dp">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#22000000"
        android:gravity="center"
        android:orientation="horizontal"
        android:padding="5dp">

        <ImageView
            android:id="@+id/iv_icon"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            />
        <RelativeLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">
        <TextView
            android:id="@+id/name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="5dp"
            android:text="name"
            android:textAllCaps="false"
            android:textColor="#000000" />

        <TextView
            android:id="@+id/content"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="5dp"
            android:text="Content"
            android:layout_below="@id/name"
            android:textAllCaps="false"
            android:textColor="#000000" />
        </RelativeLayout>
    </LinearLayout>

</RelativeLayout>

接下来重写viewHolder,对item_recyclerview.xml当中的控件进行初始化

class ViewHolder extends RecyclerView.ViewHolder {
        private ImageView iv_icon;
        private TextView name;
        private TextView content;
         public ViewHolder(@NonNull View itemView) {
             super(itemView);
             iv_icon = itemView.findViewById(R.id.iv_icon);
             name = itemView.findViewById(R.id.name);
             content = itemView.findViewById(R.id.content);
         }
     }

在onBindViewHolder中根据位置进行数据绑定

 @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        //根据位置得到对应的数据
        Shopinfo data = datas.get(position);
        holder.iv_icon.setImageResource(data.getIcon());
        holder.name.setText(data.getName());
        holder.content.setText(data.getContent());
    }

RecyclerViewActivity.java

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;
import java.util.List;


public class RecylerViewActivity extends Activity implements View.OnClickListener {
    private Button btn_add;
    private Button btn_delete;
    private Button btn_list;
    private Button btn_grid;
    private  Button btn_flow;

    private RecyclerView recyclerView;
    private List<Shopinfo> datas;
    private MyRecylerViewAdapter adapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_recylerview);
        initView();
        initData();
        //设置RecylerView的适配器
        MyRecylerViewAdapter adapter = new MyRecylerViewAdapter(RecylerViewActivity.this,datas);
        recyclerView.setAdapter(adapter);

        //设置LayoutMannger
        recyclerView.setLayoutManager(new LinearLayoutManager(RecylerViewActivity.this,LinearLayoutManager.VERTICAL,false));

    }

    private void initData() {
        //准备数据集合
        datas = new ArrayList<Shopinfo>();
        for (int i = 0; i < 10; i++) {

            datas.add(new Shopinfo(R.drawable.icon0+i,"name--"+i,"content--"+i));
        }
    }
    //初始化控件
    private void initView() {
        btn_add = findViewById(R.id.btn_add);
        btn_delete = findViewById(R.id.btn_delete);
        btn_list = findViewById(R.id.btn_list);
        btn_grid = findViewById(R.id.btn_grid);
        btn_flow = findViewById(R.id.btn_flow);
        recyclerView = findViewById(R.id.recyclerview);

        //设置点击事件
        btn_add.setOnClickListener(this);
        btn_delete.setOnClickListener(this);
        btn_list.setOnClickListener(this);
        btn_grid.setOnClickListener(this);
        btn_flow.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btn_add:
            break;
            case R.id.btn_delete:
                break;
            case R.id.btn_list:
                break;
            case R.id.btn_grid:
                break;
            case R.id.btn_flow:
                break;

        }
    }
}

效果展示

班门弄斧之浅谈androidx recyclerview本文以记录学习心得,相互学习交流为目的,如果有不当之处希望大家多多指出!