复选框自动取消选中

时间:2015-11-12 16:41:51

标签: android checkbox material-design

我正在使用navasmdc的Material Design library复选框。

当加载了带有复选框的片段时,复选框被检查(这是正确的)但是它会立即被取消选中(这不是我想要的)

我已将其放入回收商视图项目中。 所以我在onBindViewHolder中设置了这样的复选框:

   @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {

        ReviewItem current = data.get(position);

        switch (holder.getItemViewType()) {

            case TYPE_SIMPLE:
                ViewHolderSimple simpleViewHolder = (ViewHolderSimple) holder;

                simpleViewHolder.title.setText(current.getTitle());
                simpleViewHolder.subtitle.setText("Wert: " + valuesOfMaschine.get(position));

                simpleViewHolder.image.setImageResource(R.drawable.ic_starticon);

                if(Integer.parseInt(valuesOfMaschine.get(position)) == 0)
                {
                    //simpleViewHolder.checkbox.setChecked(false);
                } else if(Integer.parseInt(valuesOfMaschine.get(position)) == 1){
                    simpleViewHolder.checkbox.setChecked(true);
                }

                break;

            case TYPE_DETAILED:
                ViewHolderDetailed detailedViewHolder = (ViewHolderDetailed) holder;

                detailedViewHolder.title.setText(current.getTitle());
                detailedViewHolder.subtitle.setText("Wert: " + valuesOfMaschine.get(position));

                detailedViewHolder.image.setImageResource(R.drawable.ic_starticon);

                detailedViewHolder.slider.setValue(Integer.parseInt(valuesOfMaschine.get(position)));

                break;
        }
    }

我发现我的代码中没有任何部分可以再次取消选中该复选框...

我的复选框的xml:

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                    xmlns:materialdesign="http://schemas.android.com/apk/res-auto"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    >
                    <com.gc.materialdesign.views.CheckBox
                        android:id="@+id/review_item_checkbox_simple"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:background="#1E88E5"
                        />

                </RelativeLayout>

我错过了什么吗?

1 个答案:

答案 0 :(得分:0)

你可以用这个

在onBindViewHolder中

 simpleViewHolder.checkbox.setTag(position);
 simpleViewHolder.checkbox.setOnClickListener(new CheckBoxListener(position));
 simpleViewHolder.checkbox.setClickable(true);

为clicklistener创建一个类

class CheckBoxListener implements View.OnClickListener {

        int position;

        CheckBoxListener(int position) {
            this.position = position;
        }

        public void onClick(View v) {


            boolean isChecked = ((CheckBox) v).isChecked();
            ((CheckBox) v).setChecked(!isChecked);


            if (((CheckBox) v).isChecked()) {
              // update checked value in the list
            } else {
              // update unchecked value in the list
            }
            notifyDataSetChanged();
        }


    }