RecycleView实现合并拆分单元格

Recycleview实现好处和实现思路

(1)好处

			 1.实现简单RecycleView 是官方分装好的控件,使用普遍,代码清晰
			 2.比起自定义view实现来说在数据调控和点击事件来说更方便
			 3.对于初学者更好实现和理解

(2)实现思路

在这里插入图片描述
很简单就是把合并宽度改为0

实现代码

1创建类

public class MyShelf {
    private int size ;//一行有几个单元格
    private int length;//单元格数据的个数
    private boolean choose;//选中的单元格

    public boolean isChoose() {
        return choose;
    }

    public void setChoose(boolean choose) {
        this.choose = choose;
    }

    public int getLength() {
        return length;
    }

    public void setLength(int length) {
        this.length = length;
    }

    public int getSize() {
        return size;
    }

    public void setSize(int size) {
        this.size = size;
    }
}

创建实体类放入recycle中

创建Adpter

重要代码:


    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view= LayoutInflater.from(mContext).inflate(R.layout.item_addshelflist, viewGroup, false);
        if (mData.get(i).getSize()==0){//判断数据是发为0 0的话
            view.getLayoutParams().width = 0;
        }else {
            //平均分配每个单元格的大小
            view.getLayoutParams().width =mData.get(i).getSize()*(width/mData.size()) ;
        }
        MyViewHolder holder = new MyViewHolder(view);
        return holder;

	 @Override
    public int getItemViewType(int position) {
        return  position;//这是为了告诉onCreateViewHolder 是哪个数据修改宽度
    }

    @Override
    public void onBindViewHolder(@NonNull MyViewHolder myViewHolder, final int position) {
        if (isChecked.contains(position)){//判断选择哪个
            myViewHolder.rl_1.setBackground(mContext.getDrawable(R.drawable.shelf_bj1));
        }else {
            myViewHolder.rl_1.setBackground(mContext.getDrawable(R.drawable.shelf_bj2));
        }
        myViewHolder.tvDeviceCodeT.setText(mData.get(position).getLength()+"");

        myViewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (!isChecked.contains(position)){
                    isChecked.clear();
                    isChecked.add(position);
                    notifyDataSetChanged();
                }

                itemClickListerner.onClick(view,position);
            }
        });

    }
    }

监听Recycleview的大小传入适配器中

//监听
 ViewTreeObserver viewTreeObserver = rvShelf.getViewTreeObserver();
        viewTreeObserver.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                rvShelf.getViewTreeObserver().removeOnGlobalLayoutListener(this);
                width = rvShelf.getWidth();
            }

        });

监听刷新Adpter刷新

 private void RefrashAdpter(int pos, int mType) {
        if (mType == 0) {//普通刷新 增减数据
            addShelfListAdapter = new AddShelfListAdapter(this, getMySelf(pos), 0, width);
        } else if (mType == 1) {//合并刷新
            int pos1 = addShelfListAdapter.getSelectedPos();
            int size = addShelfListAdapter.getItemCount();
            List<MyShelf> myShelves = addShelfListAdapter.getmData();
            List<MyShelf> newShelves = getMySelf(pos1, size, myShelves);
            if (newShelves != null) {
                addShelfListAdapter = new AddShelfListAdapter(this, newShelves, pos1, width);
            }

        }
        LinearLayoutManager exceptionLayoutManager = new LinearLayoutManager(this);
        exceptionLayoutManager.setOrientation(RecyclerView.HORIZONTAL);
        rvShelf.setLayoutManager(exceptionLayoutManager);
        rvShelf.setAdapter(addShelfListAdapter);
        addShelfListAdapter.SetOnclickLister(new AddShelfListAdapter.OnItemClickListerner() {
            @Override
            public void onClick(View view, int position) {

            }
        });

dialog选取单元格个数

 private void customDialog1(List<String> strings) {
        final BaseDialog baseDialog = new BaseDialog.Builder(this)
                .setContentView(R.layout.dialog_menu)
                .setPercentWidthAndHeight(85,40)
                .create() ;
        RecyclerView rv_menu = baseDialog.findViewById(R.id.rv_menu);
        MenuAdapter1 menuAdapter = new MenuAdapter1(this, strings);
        rv_menu.setLayoutManager(new LinearLayoutManager(this));
        rv_menu.setAdapter(menuAdapter);
        menuAdapter.SetOnclickLister(new MenuAdapter1.OnItemClickListener() {
            @Override
            public void onClick(View view, int position) {

                switch (position) {
                    case 0:
                        tvSearchShelf.setText("6");
                        type = 6;
                        break;
                    case 1:
                        tvSearchShelf.setText("7");
                        type = 7;
                        break;
                    case 2:
                        tvSearchShelf.setText("8");
                        type = 8;
                        break;
                }
                RefrashAdpter(type, 0);
                baseDialog.dismiss();
            }
        });
        baseDialog.show();
    }

dialog是我之前封装的
可以看一下自定义dialog(类AlertDialog)
最后来张效果图和源代码

效果图

在这里插入图片描述

源代码

展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客
应支付0元
点击重新获取
扫码支付

支付成功即可阅读