效果图
1、导入第三方库
implementation 'com.github.CymChad:BaseRecyclerViewAdapterHelper:2.9.49'
2、XML布局
分组标题:
navigation_righthead.xml:
<?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:background="@color/colorGray"
android:orientation="vertical">
<TextView
android:id="@+id/navigation_rightHead"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:padding="10dp"
android:singleLine="true"
android:textColor="@color/colorBlack"
android:textSize="16sp" />
</LinearLayout>
分组文本:
navigation_right.xml
<?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">
<TextView
android:id="@+id/navigation_txt_rightTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
3、实体类 (Navigation)
继承SectionEntity
public class Navigation extends SectionEntity<Navigation.NavigationItem> {
public Navigation(boolean isHeader, String header) {
super(isHeader, header);
}
public Navigation(NavigationItem bean) {
super(bean);
}
public static class NavigationItem {
private String title;
private String link;
public NavigationItem(String title) {
this.title = title;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getLink() {
return link;
}
public void setLink(String link) {
this.link = link;
}
}
}
3、适配器(NavigationRghtAdapter)
继承 (BaseSectionQuickAdapter)
public class NavigationRightAdapter extends BaseSectionQuickAdapter<Navigation, BaseViewHolder> {
public NavigationRightAdapter(int layoutResId, int sectionHeadResId, List<Navigation> data) {
super(layoutResId, sectionHeadResId, data);
}
@Override
protected void convertHead(BaseViewHolder helper, Navigation item) {
helper.setText(R.id.navigation_rightHead, item.header);
}
@Override
protected void convert(@NonNull BaseViewHolder helper, Navigation item) {
Navigation.NavigationItem t = item.t;
helper.setText(R.id.navigation_txt_rightTitle, t.getTitle());
}
}
4、NavigationFragment(碎片)
public class NavigationFragment extends Fragment {
private View view;
private List<Navigation> navigationList;//右边数据
private RecyclerView recyclerView;
private NavigationRightAdapter rightAdapter;
private GridLayoutManager gridLayoutManager;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
view = inflater.inflate(R.layout.fragment_navigation, container, false);
initData();
initRight();
return view;
}
//获取数据
private void initData() {
navigationList = new ArrayList<>();
for (int i = 0; i < 10; i++) {
navigationList.add(new Navigation(true, "标题" + i));
navigationList.add(new Navigation(new Navigation.NavigationItem("数据")));
navigationList.add(new Navigation(new Navigation.NavigationItem("数据")));
navigationList.add(new Navigation(new Navigation.NavigationItem("数据")));
}
}
private void initRight() {
gridLayoutManager = new GridLayoutManager(getContext(), 2);
recyclerView=view.findViewById(R.id.navigation_recycle_right);
if (rightAdapter == null) {
rightAdapter = new NavigationRightAdapter(R.layout.navigation_right, R.layout.navigation_righthead, null);
recyclerView.setLayoutManager(gridLayoutManager);
recyclerView.setAdapter(rightAdapter);
} else {
rightAdapter.notifyDataSetChanged();
}
rightAdapter.setNewData(navigationList);
rightAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
@Override
public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
if (navigationList.get(position).isHeader) {
Toast.makeText(getContext(), "你点击了1" + navigationList.get(position).header, Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(getContext(), "你点击了" + navigationList.get(position).t.getTitle(), Toast.LENGTH_SHORT).show();
}
}
});
}
}