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

- //加入二个包
- 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
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);
-
-
- }
- }
-
"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:id="@+id/mTreeListRecy"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"/>
-
-
- import com.chad.library.adapter.base.BaseSectionQuickAdapter;
- import com.chad.library.adapter.base.BaseViewHolder;
-
- public class SectionAdapter extends BaseSectionQuickAdapter
{ - /**
- * 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
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());
-
- }
- }
-