//1.第一步 导入依赖库:
//RecyclerView
implementation 'com.android.support:recyclerview-v7:28.0.0'
//RecyclerAdapter
implementation 'com.github.CymChad:BaseRecyclerViewAdapterHelper:2.9.28'
//2.第二步 新建SectionActivity页面:
//manifest注册:
<activity
android:name=".phone.activity.SectionActivity"
android:launchMode="singleTop"
android:screenOrientation="portrait"
android:windowSoftInputMode="stateHidden"
tools:ignore="LockedOrientationActivity" />
//activity代码:
/**
* @author CJF
*/
public class SectionActivity 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(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tree_list);
RecyclerView mTreeListRecy = findViewById(R.id.mTreeListRecy);
mTreeListRecy.setLayoutManager(manager);
mTreeListRecy.setAdapter(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);
}
}
//3.第三步 新建SectionBean类继承SectionEntity,写出两个重要的构造方法:
/**
* @author CJF
*/
public class SectionBean extends SectionEntity<SectionBean.SectionDataBean> {
/**
* 绑定标题头布局的构造方法
*
* @param isHeader
* @param header
*/
public SectionBean(boolean isHeader, String header) {
super(isHeader, header);
}
/**
* 绑定数据内容的构造方法
*
* @param sectionDataBean
*/
public SectionBean(SectionDataBean sectionDataBean) {
super(sectionDataBean);
}
/**
* 数据内容
*/
public static class SectionDataBean {
private String str;
public String getStr() {
return str;
}
public void setStr(String str) {
this.str = str;
}
public SectionDataBean(String str) {
this.str = str;
}
}
}
//4.第四步 新建SectionAdapter适配器:
/**
* @author CJF
*/
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);
}
@Override
protected void convert(BaseViewHolder helper, SectionBean item) {
SectionBean.SectionDataBean bean = item.t;
helper.setText(R.id.mSectionItemText, bean.getStr());
}
}
//5.第五步 各个xml布局文件:
//activity_tree_list:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:background="@color/color_white"
android:orientation="vertical">
<android.support.v7.widget.RecyclerView
android:id="@+id/mTreeListRecy"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
//section_item:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="@dimen/dp_5"
android:layout_marginLeft="@dimen/dp_20"
android:layout_marginRight="@dimen/dp_20"
android:layout_marginTop="@dimen/dp_5"
android:background="@drawable/selector_common_item"
android:gravity="center"
android:orientation="horizontal">
<TextView
android:id="@+id/mSectionItemText"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="8"
android:background="@drawable/selector_common_item"
android:gravity="left|center_vertical"
android:minHeight="@dimen/dp_50"
android:padding="@dimen/dp_10"
android:text="text"
android:textColor="@color/black"
android:textSize="@dimen/sp_15" />
</LinearLayout>
//section_item_header:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="@dimen/dp_5"
android:layout_marginLeft="@dimen/dp_20"
android:layout_marginRight="@dimen/dp_20"
android:layout_marginTop="@dimen/dp_5"
android:background="@drawable/selector_common_item"
android:gravity="center"
android:orientation="horizontal">
<TextView
android:id="@+id/mSectionItemHeaderText"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="8"
android:background="@drawable/selector_common_item"
android:gravity="left|center_vertical"
android:minHeight="@dimen/dp_50"
android:padding="@dimen/dp_10"
android:text="text"
android:textColor="@color/black"
android:textSize="@dimen/sp_15" />
</LinearLayout>
//-------------------------------------------------------------END-----------------------------------------------------------