Selecting one RadioButton value and scrolling back removing the selected one in RecyclerView

后端 未结 9 1883
温柔的废话
温柔的废话 2020-12-15 18:24

In my application am displaying 20 multiple choice questions with the help of RecyclerView.

If I change the value of first RadioGroup and s

9条回答
  •  轮回少年
    2020-12-15 19:02

    This problem occurs because of a bug in either RecyclerView or somewhere in the Android SDK. Anyway, I took the advice from another SO question - onCheckedChanged called automatically.

    For RadioGroup, it will be a little different. There are two ways to go about. I recommend 1st one since it is plug-n-play.

    1. set OnCheckedChangeListener on RadioGroup:

      mRadioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
          @Override
          public void onCheckedChanged(RadioGroup group, int checkedId) {
              if(checkedId == -1) {
                  Log.v("onCheck", "Android bug since RadioButton doesn't get unchecked normally!");
              }
              else {
                  Log.v("onCheck", "Valid click. By user");
                  mMyListObjectArr[position].setChecked(checkedId);
              }
          }
      });
      
    2. or set onCheckedChangeListener on all RadioButtons inside the RadioGroup:

      CompoundButton.OnCheckedChangeListener onCheckedChangeListener = new CompoundButton.OnCheckedChangeListener() {
          @Override
          public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
              if(buttonView.isPressed()) {
                  Log.v("onCheck", position + ", valid click by user");
                  mMyListObjectArr[position].setChecked(buttonView.getId());
              }
              else {
                  Log.v("onCheck", "Android bug");
              }
          }
      };
      mRadioButton1.setOnCheckedChangeListener(onCheckedChangeListener);
      mRadioButton2.setOnCheckedChangeListener(onCheckedChangeListener);
      

提交回复
热议问题