Android RecyclerView列表实现

1.在项目的build.gradle文件中引用

implementation 'com.github.CymChad:BaseRecyclerViewAdapterHelper:2.9.33'
implementation 'com.android.support:recyclerview-v7:28.0.0'

在这里插入图片描述
实现上述图中效果,主要是需要:

  • 一个RecyclerView的布局文件
  • 一个需要显示的单项item的布局文件
  • 一个后台返回的list集合
  • 一个适配器(Adapter)

2.首先创建一个RecyclerView布局:activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    
	<!--头部返回栏引用,可以忽略-->
    <include layout="@layout/header" android:id="@+id/header_content"/>
	<!--android.support.v7.widget.RecyclerView这个是重点,就是为了配置RecyclerView-->
    <android.support.v7.widget.RecyclerView
        android:id="@+id/rvMain"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_margin="8dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
      	app:layout_constraintTop_toBottomOf="@+id/header_content"
      	<!--@layout/item_for_main_rv 这个是单项item的布局文件,加入下面一行可以在预览中看到item显示在RecyclerView中样子-->
        tools:listitem="@layout/item_for_main_rv" />


</android.support.constraint.ConstraintLayout>

3.然后创建一个item文件:item_for_main_rv.xml 注意:文件用什么布局格式没有关系


注意:文件用什么布局格式没有关系)

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="4dp">

    <android.support.constraint.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="4dp"
        <!--樱花的背景图是作者自己找的,可以随便找个图换上-->
        android:background="@drawable/banner2"
        tools:context=".view.ActivityYkrkScan">

        <TextView
            android:id="@+id/djhYkrk"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginStart="@dimen/size4dp"
            android:layout_marginTop="@dimen/size4dp"
            android:layout_marginEnd="@dimen/size4dp"
            android:textSize="16sp"
            android:textStyle="bold"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintBottom_toTopOf="@+id/lyck"
            />

        <TextView
            android:id="@+id/lyck_text"
            android:layout_width="0dp"
            android:text="发货仓库:"
            android:textSize="12sp"
            android:textStyle="bold"
            android:layout_height="wrap_content"
            android:layout_marginTop="@dimen/size4dp"
            android:layout_marginStart="@dimen/size4dp"
            app:layout_constraintTop_toBottomOf="@+id/djhYkrk"
            app:layout_constraintRight_toLeftOf="@+id/lyck"
            app:layout_constraintBottom_toTopOf="@+id/mbck_text"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintHorizontal_weight="1"
            />

        <TextView
            android:id="@+id/lyck"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginTop="@dimen/size4dp"
            android:layout_marginEnd="@dimen/size4dp"
            android:textSize="12sp"
            android:textStyle="bold"
            android:layout_marginStart="@dimen/size4dp"
            app:layout_constraintTop_toBottomOf="@+id/djhYkrk"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintLeft_toRightOf="@+id/lyck_text"
            app:layout_constraintHorizontal_weight="3"
            />

        <TextView
            android:id="@+id/mbck_text"
            android:layout_width="0dp"
            android:text="发货库房:"
            android:textSize="12sp"
            android:textStyle="bold"
            android:layout_height="wrap_content"
            android:layout_marginTop="@dimen/size4dp"
            android:layout_marginStart="@dimen/size4dp"
            app:layout_constraintTop_toBottomOf="@+id/lyck_text"
            app:layout_constraintRight_toLeftOf="@+id/mbck"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintHorizontal_weight="1"
            />

        <TextView
            android:id="@+id/mbck"
            android:layout_width="0dp"
            android:textSize="12sp"
            android:textStyle="bold"
            android:layout_height="wrap_content"
            android:layout_marginTop="@dimen/size4dp"
            android:layout_marginEnd="@dimen/size4dp"
            android:layout_marginStart="@dimen/size4dp"
            app:layout_constraintLeft_toRightOf="@+id/mbck_text"
            app:layout_constraintTop_toBottomOf="@+id/lyck"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintHorizontal_weight="3"
            />


        <TextView
            android:id="@+id/zlYkrk"
            android:layout_width="0dp"
            android:textSize="12sp"
            android:textStyle="bold"
            android:gravity="center_vertical|left"
            android:layout_height="wrap_content"
            android:layout_marginTop="@dimen/size4dp"
            android:layout_marginBottom="@dimen/size4dp"
            android:layout_marginStart="@dimen/size4dp"
            app:layout_constraintTop_toBottomOf="@+id/mbck"
            app:layout_constraintRight_toLeftOf="@+id/djrq"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintHorizontal_weight="1"
            />

        <TextView
            android:id="@+id/djrq"
            android:layout_width="0dp"
            android:textSize="12sp"
            android:textStyle="bold"
            android:gravity="center_vertical|right"
            android:layout_height="wrap_content"
            android:layout_marginTop="@dimen/size4dp"
            android:layout_marginBottom="@dimen/size4dp"
            android:layout_marginEnd="@dimen/size4dp"
            android:layout_marginStart="@dimen/size4dp"
            app:layout_constraintTop_toBottomOf="@+id/mbck"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintLeft_toRightOf="@+id/zlYkrk"
            app:layout_constraintHorizontal_weight="1"
            />
    </android.support.constraint.ConstraintLayout>
</android.support.v7.widget.CardView>

配置完这些之后就有上面效果图那样的效果了

4.接下来就是制作一个list集合作为数据源ActivityMain.java文件,即activity_main.xml对应的java文件中进行渲染

private List<YkrkItem> data = new ArrayList<>();
private RecyclerView recycler;
private YkrkAdapter adapter = null;
private void initData(String objct) {
            for (int i = 0; i < 20; i++) {
            YkrkItem item = new YkrkItem();
            item.setDjh("RKD111111123344444");
            item.setCkmc("仓库"+i);
            item.setKfmc("杭州仓库"+i);
            item.setDjrq("2021-05-25");
            item.setZlb("123433");
            data.add(item);
        }
    }

onCreate方法中书写:

recycler = findViewById(R.id.rvMain);//获取RecyclerView组件
initData(); //初始化数据
//YkrkAdapter 是适配器,第6步就会说明
adapter = new YkrkAdapter(R.layout.item_for_main_rv, data);
//下面两句是最重要的,不然列表是不会显示的,GridLayoutManager是个人理解是显示的效果,第二个参数写几,就代表一行显示几个item
recycler.setLayoutManager(new GridLayoutManager(this,1));
recycler.setAdapter(adapter);


//item点击事件
myAdapter .setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
     @Override
     public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
     	Toast.makeText(ActivityMain.this, "" + position, Toast.LENGTH_SHORT).show();
     }
});

//item子控件点击事件
myAdapter .setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
    @Override
    public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
    	//根据布局中的控件id区分子控件的点击
        switch (view.getId()){
            case R.id.textview:
                break;
            case R.id.imageview:
                break;
        }
    }
});

5.适配器-YkrkAdapter.java

public class YkrkAdapter extends BaseQuickAdapter<YkrkItem, BaseViewHolder> {
	//BaseQuickAdapter中第一个参数为实体类,这边我的实体类是YkrkItem,第二个是BaseViewHolder
    //自定义 YkrkAdapter 创建时传入布局和数据,data为数据源
    public YkrkAdapter(int layoutResId, @Nullable List<YkrkItem> data) {
        super(layoutResId, data);
    }
    //在次方法中进行 viewHolder 和数据的关联
    //BaseViewHolder helper是item布局
    @Override
    protected void convert(BaseViewHolder helper, YkrkItem item) {
    	//直接给自定义布局中的控件填充数据
        helper.setText(R.id.djhYkrk, item.getDjh());
        helper.setText(R.id.zlYkrk, item.getZlb());
        helper.setText(R.id.lyck, item.getCkmc());
        helper.setText(R.id.mbck, item.getKfmc());
        helper.setText(R.id.djrq, item.getDjrq());
        //子控件 点击事件
        //参数为控件的id
        //helper.addOnClickListener(R.id.djhYkrk);
        //子控件长按事件
        //helper.addOnLongClickListener(R.id.zlYkrk);
    }


}

6.YkrkItem.java实体文件

public class YkrkItem {

    private String djh;

    private String kfmc;


    private String djrq;

    private String zlb;

    private String ckmc;

    private String wlmc;

    public String getDjh() {
        return djh;
    }

    public void setDjh(String djh) {
        this.djh = djh;
    }

    public String getKfmc() {
        return kfmc;
    }

    public void setKfmc(String kfmc) {
        this.kfmc = kfmc;
    }

    public String getDjrq() {
        return djrq;
    }

    public void setDjrq(String djrq) {
        this.djrq = djrq;
    }

    public String getZlb() {
        return zlb;
    }

    public void setZlb(String zlb) {
        this.zlb = zlb;
    }

    public String getCkmc() {
        return ckmc;
    }

    public void setCkmc(String ckmc) {
        this.ckmc = ckmc;
    }

    public String getWlmc() {
        return wlmc;
    }

    public void setWlmc(String wlmc) {
        this.wlmc = wlmc;
    }
}
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值