美文网首页
Andoroid RecyclerView 分组、分割线

Andoroid RecyclerView 分组、分割线

作者: Enowr | 来源:发表于2019-10-22 10:25 被阅读0次

效果图

WechatIMG25.png

源码

简单使用

  1. gradle 集成
api 'com.enowr.android:Widgets:0.0.8'
  1. 分割线:设置左上右下的Drawable,以及大小
new ItemDecorationDivider(binding.recycler) {
            @Override
            public DividerInfo onCreateDividerLeft(int position) {
                DividerInfo info = new DividerInfo();
                info.setColor(Color.RED);
                return info;
            }

            @Override
            public DividerInfo onCreateDividerTop(int position) {
                DividerInfo info = new DividerInfo();
                info.setColor(Color.BLUE);
                return info;
            }

            @Override
            public DividerInfo onCreateDividerRight(int position) {
                DividerInfo info = new DividerInfo();
                info.setColor(Color.RED);
                return info;
            }

            @Override
            public DividerInfo onCreateDividerBottom(int position) {
                DividerInfo info = new DividerInfo();
                info.setColor(Color.GREEN);
                return info;
            }

            @Override
            public void onBindDividerLeft(@NonNull DividerInfo info, int position) {
                info.setDistance(position + 2);
            }

            @Override
            public void onBindDividerTop(@NonNull DividerInfo info, int position) {
                info.setDistance(position + 10);
            }

            @Override
            public void onBindDividerRight(@NonNull DividerInfo info, int position) {

            }

            @Override
            public void onBindDividerBottom(@NonNull DividerInfo info, int position) {
                info.setDistance(10);
            }
        };
  1. 分组:设置header、footer的View 以及偏移量
new ItemDecorationSection(binding.recycler, true) {

            @Override
            public SectionInfo onCreateHeaderSectionInfo(int position) {
                SectionInfo info = null;
                if (position % 20 == 0) {
                    info = new SectionInfo();
                    info.setView(LayoutInflater.from(getApplication()).inflate(R.layout.recycler_layout_item_decoration, binding.recycler, false));
                    RecyclerLayoutItemDecorationBinding binding = DataBindingUtil.bind(info.getView());
                    binding.executePendingBindings();
                    binding.text.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            UtilsToast.show("点击了分组" + v.getTag(v.getId()));
                        }
                    });
                }
                return info;
            }

            @Override
            public SectionInfo onCreateFooterSectionInfo(int position) {
                SectionInfo info = null;
                if (position % 20 == 19) {
                    info = new SectionInfo();
                    info.setView(LayoutInflater.from(getApplication()).inflate(R.layout.recycler_layout_item_decoration_footer, binding.recycler, false));
                    RecyclerLayoutItemDecorationFooterBinding binding = DataBindingUtil.bind(info.getView());
                    binding.executePendingBindings();
                    binding.text.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            UtilsToast.show("点击了Footer" + v.getTag(v.getId()));
                        }
                    });
                }
                return info;
            }

            @Override
            public void onBindHeaderSectionInfo(@NonNull SectionInfo info, int position) {
                info.setTranslationY(-position - 10);
                RecyclerLayoutItemDecorationBinding binding = DataBindingUtil.getBinding(info.getView());
                binding.text.setText("分组" + position / 20);
                binding.text.setTag(binding.text.getId(), position / 20);
            }

            @Override
            public void onBindFooterSectionInfo(@NonNull SectionInfo info, int position) {
                info.setTranslationY(10);
                RecyclerLayoutItemDecorationFooterBinding binding = DataBindingUtil.getBinding(info.getView());
                binding.text.setText("Footer View" + position / 20);
                binding.text.setTag(binding.text.getId(), position / 20);
            }
        };

相关文章

网友评论

      本文标题:Andoroid RecyclerView 分组、分割线

      本文链接:https://www.haomeiwen.com/subject/abztvctx.html