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

    }
}

  
相关推荐
Cao_Shixin攻城狮3 小时前
Flutter运行Android项目时显示java版本不兼容(Unsupported class file major version 65)的处理
android·java·flutter
呼啦啦呼啦啦啦啦啦啦6 小时前
利用pdfjs实现的pdf预览简单demo(包含翻页功能)
android·javascript·pdf
idjl8 小时前
Mysql测试题
android·adb
游戏开发爱好者810 小时前
iOS App 电池消耗管理与优化 提升用户体验的完整指南
android·ios·小程序·https·uni-app·iphone·webview
人生游戏牛马NPC1号11 小时前
学习 Flutter (四):玩安卓项目实战 - 中
android·学习·flutter
星辰也为你祝福h12 小时前
Android原生Dialog
android
梁同学与Android13 小时前
Android ---【CPU优化】需要优化的原因及优化的地方
android
Misha韩13 小时前
React Native 基础tabBar和自定义tabBar - bottom-tabs
android·react native
iHero14 小时前
【Nextcloud】在 Ubuntu 22.04.3 LTS 上的 Nextcloud Hub 10 (31.0.2) 后台任务cron 的优化
android·linux·ubuntu·nextcloud
yuanlaile17 小时前
Flutter Android打包学习指南
android·flutter·flutter打包·flutter android