RecyclerView折叠item的垃圾实现方法

时间:2021-7-21 作者:qvyue
RecyclerView折叠item的垃圾实现方法
比较喜欢先上图

Android 原生ExpandableListView 是继承ListView绘制,可直接实现此UI,自行搜索使用

无论何种实现,均需我们手动转化数据源
实现方式简单,代码未优化,无扩展性,性能未知,勿喷
最下方有源码

方案一,加载所有数据通过控制itemview高度实现假折叠

平级数据,数据需要转化我们需要的

List list = new ArrayList();

        list.add(new TestBean("这是数据 1", 1, true, true));
        list.add(new TestBean("这是数据 1 子项", 1));
        list.add(new TestBean("这是数据 1 子项", 1));
        list.add(new TestBean("这是数据 1 子项", 1));
        list.add(new TestBean("这是数据 1 子项", 1));

        list.add(new TestBean("这是数据 2", 2, true, true));
        list.add(new TestBean("这是数据 2 子项", 2));
        list.add(new TestBean("这是数据 2 子项", 2));
        list.add(new TestBean("这是数据 2 子项", 2));

        list.add(new TestBean("这是数据 3", 3, true, true));
        list.add(new TestBean("这是数据 3 子项", 3));
        list.add(new TestBean("这是数据 3 子项", 3));

        list.add(new TestBean("这是数据 4", 4, true, false));
        list.add(new TestBean("这是数据 5", 5, true, true));
        list.add(new TestBean("这是数据 5 子项", 5));
        list.add(new TestBean("这是数据 5 子项", 5));
        list.add(new TestBean("这是数据 5 子项", 5));

        list.add(new TestBean("这是数据 6", 6, true, true));
        list.add(new TestBean("这是数据 6 子项", 6));
        list.add(new TestBean("这是数据 6 子项", 6));
        list.add(new TestBean("这是数据 6 子项", 6));
        list.add(new TestBean("这是数据 6 子项", 6));
        list.add(new TestBean("这是数据 6 子项", 6));

adapter实现

public class RecyclerViewFoldingAdapter extends RecyclerView.Adapter {

    private Context context;
    private List list;

    public RecyclerViewFoldingAdapter(Context context, List list) {
        this.context = context;
        this.list = list;
    }

    @NonNull
    @Override
    public RecyclerViewFoldingAdapter.FoldingViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_view, parent, false);
        return new FoldingViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerViewFoldingAdapter.FoldingViewHolder viewHolder, int position) {
        final TestBean testBean = list.get(position);
        ViewGroup.LayoutParams layoutParams = viewHolder.itemView.getLayoutParams();
        if (testBean.isMain()){
            viewHolder.textView.setText(testBean.getId() + "    " + testBean.getTitle());
            if (testBean.isHasChild()){
                viewHolder.imageView.setVisibility(View.VISIBLE);
            } else {
                viewHolder.imageView.setVisibility(View.GONE);
            }
            if (testBean.isFolding()){
                viewHolder.imageView.setImageResource(R.drawable.down);
            } else {
                viewHolder.imageView.setImageResource(R.drawable.up);
            }
            layoutParams.height = TestBean.dp2px(context, 50);
        } else {
            viewHolder.textView.setText("     " + testBean.getTitle());
            if (testBean.isFolding()){
                layoutParams.height = 0;
            } else {
                
                layoutParams.height = TestBean.dp2px(context, 50);
            }
            viewHolder.imageView.setVisibility(View.GONE);
        }
        viewHolder.itemView.setLayoutParams(layoutParams);

        viewHolder.imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                testBean.setFolding(!testBean.isFolding());
                changeItem(testBean);
                notifyDataSetChanged();
            }
        });
    }

    private void changeItem(TestBean testBean) {
        for (TestBean bean : list) {
            if (testBean.getId() == bean.getId() && !bean.isMain()){
                bean.setFolding(testBean.isFolding());
            }
        }
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    static class FoldingViewHolder extends RecyclerView.ViewHolder{

        TextView textView;
        ImageView imageView;

        public FoldingViewHolder(@NonNull View itemView) {
            super(itemView);
            textView = itemView.findViewById(R.id.tv);
            imageView = itemView.findViewById(R.id.iv);
        }
    }
}

方案二,通过数据源 list.add和remove方式更新到adapter中实现假折叠

数据源,list中bean带有自己List beans
List list = new ArrayList();
TestBean bean1 = new TestBean("这是数据 1", 1,true, true);
        list.add(bean1);
        bean1.getBeans().add(new TestBean("这是数据 1 子项"));
        bean1.getBeans().add(new TestBean("这是数据 1 子项"));
        bean1.getBeans().add(new TestBean("这是数据 1 子项"));
        bean1.getBeans().add(new TestBean("这是数据 1 子项"));
        TestBean bean2 = new TestBean("这是数据 2", 2,true, true);
        list.add(bean2);
        bean2.getBeans().add(new TestBean("这是数据 2 子项"));
        bean2.getBeans().add(new TestBean("这是数据 2 子项"));
        bean2.getBeans().add(new TestBean("这是数据 2 子项"));

        TestBean bean3 = new TestBean("这是数据 3", 3,true, true);
        list.add(bean3);
        bean3.getBeans().add(new TestBean("这是数据 3 子项"));
        bean3.getBeans().add(new TestBean("这是数据 3 子项"));

        list.add(new TestBean("这是数据 4", 4, true, false));

        TestBean bean5 = new TestBean("这是数据 5", 5,true, true);
        list.add(bean5);
        bean5.getBeans().add(new TestBean("这是数据 5 子项"));
        bean5.getBeans().add(new TestBean("这是数据 5 子项"));
        bean5.getBeans().add(new TestBean("这是数据 5 子项"));

        TestBean bean6 = new TestBean("这是数据 6", 6, true, true);
        list.add(bean6);
        bean6.getBeans().add(new TestBean("这是数据 6 子项"));
        bean6.getBeans().add(new TestBean("这是数据 6 子项"));
        bean6.getBeans().add(new TestBean("这是数据 6 子项"));
        bean6.getBeans().add(new TestBean("这是数据 6 子项"));
        bean6.getBeans().add(new TestBean("这是数据 6 子项"));

适配器

public class RecyclerViewFoldingAdapter2 extends RecyclerView.Adapter {

    private Context context;
    private List list;

    public RecyclerViewFoldingAdapter2(Context context, List list) {
        this.context = context;
        this.list = list;
    }

    @NonNull
    @Override
    public RecyclerViewFoldingAdapter2.FoldingViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_view, parent, false);
        return new FoldingViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerViewFoldingAdapter2.FoldingViewHolder viewHolder, final int position) {
        final TestBean testBean = list.get(position);

        if (testBean.isMain()){
            viewHolder.textView.setText(testBean.getId() + "    " + testBean.getTitle());
            if (testBean.isHasChild()){
                viewHolder.imageView.setVisibility(View.VISIBLE);
                if (testBean.isFolding()){
                    viewHolder.imageView.setImageResource(R.drawable.down);
                } else {
                    viewHolder.imageView.setImageResource(R.drawable.up);
                }
            } else {
                viewHolder.imageView.setVisibility(View.GONE);
            }
        } else {
            viewHolder.textView.setText("     " + testBean.getTitle());
            viewHolder.imageView.setVisibility(View.GONE);
        }

        viewHolder.imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                testBean.setFolding(!testBean.isFolding());

                changeItem(position, testBean.isFolding());
                notifyDataSetChanged();
            }
        });
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    private void changeItem(int index, boolean isFold){
        TestBean bean = list.get(index);
        if (isFold){
            list.removeAll(bean.getBeans());
        } else {
            list.addAll(index + 1, bean.getBeans());
        }
        notifyDataSetChanged();
    }

    static class FoldingViewHolder extends RecyclerView.ViewHolder{

        TextView textView;
        ImageView imageView;

        public FoldingViewHolder(@NonNull View itemView) {
            super(itemView);
            textView = itemView.findViewById(R.id.tv);
            imageView = itemView.findViewById(R.id.iv);
        }
    }
}

以上代码只是实现效果,未做优化,只是提供这个实现思路而已,如需使用请同学自行优化代码
码云 —->>> https://gitee.com/woslxm_limeng/fold_recyclerview.git

声明:本文内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:qvyue@qq.com 进行举报,并提供相关证据,工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。