Android开发RecyclerView中使用多个Switch

如题RecyclerView中使用多个Switch,为每个Switch绑定事件。可以在onBindViewHolder中使用holder.aSwitch.setOnCheckedChangeListener。对ViewHolder中绑定的控件进行控制,参数position定位RecyclerView行数,每个setOnCheckedChangeListener中确定这一行哪一个被改变。
这里我使用了8个Switch。
2个布局代码省略。Acitvity中控制代码如下
adapter代码如下
定义变量

//这里做多支持100个列表项,每行8个Switch开关
boolean[][] switch_position = new boolean[100][8];

adapter 重写

        adapter = new RecyclerView.Adapter<HomeFragment.DeviceViewHolder>() {
            @Override
            public HomeFragment.DeviceViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
                View view = LayoutInflater.from(getContext()).inflate(R.layout.device_detail_main,null);//绑定布局
                return new HomeFragment.DeviceViewHolder(view,this);
            }
            @Override
            public void onBindViewHolder(DeviceViewHolder holder, int position) {
    //绑定事件
                //开关初始化状态,获取switch_position中开关状态
                for(int i=0 ; i< 8 ; i++) holder.aSwitch[i].setChecked(switch_position[position][i]);
                //开关状态动作
                for(int i=0 ; i< 8 ; i++)
                {
                    final int k = i;//下面部分参数要求final
                    holder.aSwitch[i].setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                        @Override
                        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                            int position_2 = k;
                            if (isChecked)
                            {
                            //被选中都输出哪一个开关,什么状态,并存储至switch_position
                                System.out.println(position+" "+position_2+" is be Checked");
                                switch_position[position][position_2] = true;
                            }
                            else
                            {
                                System.out.println(position+" "+position_2+" is not be Checked");
                                switch_position[position][position_2] = false;
                            }
                        }
                    });
                }
            }
            @Override
            public int getItemCount() {
                return devList.size();
            }
        };

ViewHolder :

    class DeviceViewHolder extends RecyclerView.ViewHolder
    {
        //
        Switch aSwitch[] = new Switch[8];//此次使用了8个按钮
        private RecyclerView.Adapter adapter;
        public DeviceViewHolder(View itemView,RecyclerView.Adapter adapter) {
            super(itemView);
            //
            this.aSwitch[0] = itemView.findViewById(R.id.switch1);
            this.aSwitch[1] = itemView.findViewById(R.id.switch2);
            this.aSwitch[2] = itemView.findViewById(R.id.switch3);
            this.aSwitch[3] = itemView.findViewById(R.id.switch4);
            this.aSwitch[4] = itemView.findViewById(R.id.switch5);
            this.aSwitch[5] = itemView.findViewById(R.id.switch6);
            this.aSwitch[6] = itemView.findViewById(R.id.switch7);
            this.aSwitch[7] = itemView.findViewById(R.id.switch8);
            //
            this.rootView = itemView.findViewById(R.id.item_root);
            this.adapter = adapter;
        }

    }

运行结果:这里RecyclerView中有2行。

I/System.out: 0 4 is be Checked
I/System.out: 0 4 is not be Checked
I/System.out: 0 5 is be Checked
I/System.out: 0 5 is not be Checked
I/System.out: 1 1 is be Checked
I/System.out: 1 1 is not be Checked
I/System.out: 1 6 is be Checked
I/System.out: 1 6 is not be Checked
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值