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

    }
}

  
相关推荐
Wellijohn8 小时前
一个 Android 老兵的十年开源路:从 167 star 的滑动控件,到本地优先的 AI Agent
android·人工智能·开源
好的佩奇12 小时前
Dart 之元数据与注解
android·flutter·dart
三少爷的鞋14 小时前
很多开发者把物理上的分层,当成了架构
android
FungLeo15 小时前
FengCMS · PHP+MySQL 升级实录 · MySQL DDL不能回滚:模型字段结构操作的失败续做
android·mysql·php·工程实践·fengcms
雨白16 小时前
Android 建造者模式实战:从 AlertDialog 源码到 TopBar 的两种实现
android
_祝你今天愉快18 小时前
Android 高级绘制:[五] 贝塞尔曲线与计算规则
android
FungLeo1 天前
FengCMS · PHP+MySQL 升级实录 · 危险默认值之殇:root/123456、认证码 8888 写进配置加载器
android·mysql·php·安全加固·fengcms
yz_aiks1 天前
Termux 使用教程:把安卓手机变成随身 Linux(装 Python、SSH 远程、跑自动化脚本)
android·linux·python·智能手机·ssh·termux
booboosui2 天前
编号没变,消息还是来了:Mac 端通知读取漏第二条的原因
android
雨白2 天前
单例设计模式:从 Java/Kotlin 实现到 Android 实战
android