• Android RecycleView分组


     效果图

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

    }
    }
    });
    }

    }



  • 相关阅读:
    "LC.exe" exited with code -1 错误
    GridControl的用法(1)
    oracle建库及plsql建表空间的用法
    sql server还原数据库文件(.bak)常见问题解决办法笔记
    ubuntu下安装deb包
    ubuntu下安装五笔输入法
    ubuntu下安装codeTyphon
    centos7下源码安装lazarus
    后台对Json数据加密、解密
    Http请求纯后台简单实现分页并返回JSON格式
  • 原文地址:https://www.cnblogs.com/guochangxin/p/11532127.html
Copyright © 2020-2023  润新知