Android RecyclerView BaseSectionQuickAdapter实现分组功能

详情网站:手把手教你使用BaseSectionQuickAdapter实现分组功能,史上最详细Adapter使用教程_basequickadapter 分组_杨阿程的博客-CSDN博客

java 复制代码
 //加入二个包
 implementation 'com.android.support:recyclerview-v7:26.0.0-beta1'
 implementation 'com.github.CymChad:BaseRecyclerViewAdapterHelper:2.9.34'



import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;

public class MainActivity extends AppCompatActivity {

    private final GridLayoutManager manager = new GridLayoutManager(this, 4);
    private final SectionAdapter adapter = new SectionAdapter(R.layout.section_item, R.layout.section_item_header, new ArrayList<>());


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        RecyclerView mTreeListRecy = findViewById(R.id.mTreeListRecy);

        mTreeListRecy.setLayoutManager(manager);
        mTreeListRecy.setAdapter((RecyclerView.Adapter) adapter);

        List<SectionBean> list = new ArrayList<>();
        for (int i = 0; i < 3; i++) {
            //添加标题内容
            list.add(new SectionBean(true, "标题" + i));

            //添加数据内容
            for (int j = 0; j < 10; j++) {
                list.add(new SectionBean(new SectionBean.SectionDataBean("数据" + j)));
            }
        }

        adapter.addData(list);


    }
}

<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"
    tools:context="com.example.recycler_view_list.MainActivity">


    <android.support.v7.widget.RecyclerView
        android:id="@+id/mTreeListRecy"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

</android.support.constraint.ConstraintLayout>

import com.chad.library.adapter.base.BaseSectionQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;

public class SectionAdapter extends BaseSectionQuickAdapter<SectionBean, BaseViewHolder> {
    /**
     * Same as QuickAdapter#QuickAdapter(Context,int) but with
     * some initialization data.
     *
     * @param layoutResId      The layout resource id of each item.
     * @param sectionHeadResId The section head layout id for each item
     * @param data             A new list is created out of this one to avoid mutable list
     */
    public SectionAdapter(int layoutResId, int sectionHeadResId, List<SectionBean> data) {
        super(layoutResId, sectionHeadResId, data);
    }

    @Override
    protected void convertHead(BaseViewHolder helper, SectionBean item) {
        helper.setText(R.id.mSectionItemHeaderText, item.header);
        Log.i(TAG + "_Log_", "2023/2/28: item.header=" +item.header);

    }

    @Override
    protected void convert(BaseViewHolder helper, SectionBean item) {
        SectionBean.SectionDataBean bean = item.t;
        helper.setText(R.id.mSectionItemText, bean.getStr());

    }
}

  
相关推荐
Clockwiseee12 分钟前
PHP之伪协议
android·开发语言·php
小林爱18 分钟前
【Compose multiplatform教程08】【组件】Text组件
android·java·前端·ui·前端框架·kotlin·android studio
小何开发1 小时前
Android Studio 安装教程
android·ide·android studio
开发者阿伟2 小时前
Android Jetpack LiveData源码解析
android·android jetpack
weixin_438150992 小时前
广州大彩串口屏安卓/linux触摸屏四路CVBS输入实现同时显示!
android·单片机
CheungChunChiu3 小时前
Android10 rk3399 以太网接入流程分析
android·framework·以太网·eth·net·netd
木头没有瓜3 小时前
ruoyi 请求参数类型不匹配,参数[giftId]要求类型为:‘java.lang.Long‘,但输入值为:‘orderGiftUnionList
android·java·okhttp
键盘侠0073 小时前
springboot 上传图片 转存成webp
android·spring boot·okhttp
江上清风山间明月4 小时前
flutter bottomSheet 控件详解
android·flutter·底部导航·bottomsheet
Crossoads5 小时前
【汇编语言】外中断(一)—— 外中断的魔法:PC机键盘如何触发计算机响应
android·开发语言·数据库·深度学习·机器学习·计算机外设·汇编语言