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

    }
}

  
相关推荐
无心水3 分钟前
【Python实战进阶】7、Python条件与循环实战详解:从基础语法到高级技巧
android·java·python·python列表推导式·python条件语句·python循环语句·python实战案例
g***78911 小时前
鸿蒙NEXT(五):鸿蒙版React Native架构浅析
android·前端·后端
Bervin121387 小时前
Flutter Android环境的搭建
android·flutter
e***877014 小时前
windows配置永久路由
android·前端·后端
fouryears_2341715 小时前
现代 Android 后台应用读取剪贴板最佳实践
android·前端·flutter·dart
YF021116 小时前
Frida for MacBook/Android 安装配置
android·前端
雨白17 小时前
Android实战:构建高可维护的日志系统
android
茄子凉心18 小时前
android 开机启动App
android·java·开发语言
2501_9371931420 小时前
神马影视 8.8 版源码:4K 播放优化体验测评
android·源码·源代码管理·机顶盒
修炼者21 小时前
Kotlin中的Flow流
android·kotlin