【问题标题】:Checking a radioButton in RecyclerView makes other random radiobutton checked too检查 RecyclerView 中的单选按钮也会检查其他随机单选按钮
【发布时间】:2017-12-31 08:22:35
【问题描述】:

我是 RecyclerView 的新手,当我在 RecyclerView 中使用带有 4 个 RadioButtonRadioGroup 时,一切正常,但是当我在 RecyclerView 中向下滚动时,一个单选按钮随机被签入到另一个位置 布局 row_myList.xml

      <RadioGroup>      
        <RadioButton
            android:text="RadioButton"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/btn_radio_test2"
            android:button="@null"
            android:drawableRight="?android:attr/listChoiceIndicatorSingle"
            android:layout_weight="1"
            android:gravity="right|center_vertical"
            android:textSize="10sp" />

        <RadioButton
            android:text="RadioButton"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/btn_radio_test3"
            android:button="@null"
            android:drawableRight="?android:attr/listChoiceIndicatorSingle"
            android:layout_weight="1"
            android:gravity="right|center_vertical"
            android:textSize="10sp" />

        <RadioButton
            android:text="RadioButton"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/btn_radio_test1"
            android:layout_weight="1"
            android:button="@null"
            android:drawableRight="?android:attr/listChoiceIndicatorSingle"
            android:gravity="right|center_vertical"
            android:textSize="10sp" />

        <RadioButton
            android:text="RadioButton"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/btn_radio_test4"
            android:layout_weight="1"
            android:button="@null"
            android:drawableRight="?android:attr/listChoiceIndicatorSingle"
            android:gravity="right|center_vertical"
            android:textSize="10sp" />

        <RadioGroup
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:id="@+id/radioGp_testi"
            android:layout_marginTop="20dp"
            android:layout_marginBottom="10dp"
            android:layout_marginLeft="5dp"
            android:layout_marginRight="5dp"
            >

        </RadioGroup>

我的适配器代码

public class AdapterQuiz extends RecyclerView.Adapter<AdapterQuiz.MyViewHolder> {
private Activity context;
private ArrayList<HashMap<String,String>>quizs=new ArrayList<>();
private static HashMap<String,String>hashMap;

public AdapterQuiz(final Activity context, ArrayList<HashMap<String,String>>quizs){
    this.context=context;
    this.quizs=quizs;

}
 @Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view;
    view= LayoutInflater.from(context).inflate(R.layout.row_soalat,parent,false);
    return new MyViewHolder(view);
}

@Override
public void onBindViewHolder(final MyViewHolder holder, final int position) {

    hashMap=new HashMap<>();
    hashMap=quizs.get(position);

 holder.rgTesti.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int i) {

                switch (i){
                    case R.id.btn_radio_test1:
                        //show a toast
                        break;
                    case R.id.btn_radio_test2:
                        //show a toast
                        break;
                    case R.id.btn_radio_test3:
                        //show a toast
                        break;
                    case R.id.btn_radio_test4:
                        //show a toast
                        break;

                }

            }
        });
    }   }

【问题讨论】:

  • 是的,你做过点击监听器之类的监听器吗?
  • 是的,我有它。
  • 好的,请您分享您的 RecyclerView 适配器代码,也请在 RadioGroup 中制作所有单选按钮。
  • 确定。请稍候

标签: android android-recyclerview radio-button radio-group


【解决方案1】:

您需要在 Quiz 对象中定义一个参数来保存选定的单选按钮值(意味着选择了哪个)。当用户选择单选按钮时,更新 Quiz 对象的值依赖于选定的单选按钮值并调用 notifyDataSetChanged()。然后在 bindViewHolder根据此参数设置单选按钮值。这是我如何使用它的示例

holder.radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
      @Override
      public void onCheckedChanged(RadioGroup radioGroup, @IdRes int i) 
      {
          RadioButton rb = (RadioButton)radioGroup.findViewById(i);
          String txt = rb.getText().toString();
          int value = Integer.parseInt(txt);
          surveyList.get(position).setRating(value);
          notifyDataSetChanged();
      }
  });

【讨论】:

    【解决方案2】:

    您走错了路...将所有单选按钮包装在 Radio Group 中。

    【讨论】:

    • 如果没有单选按钮,单选组将毫无用处。
    猜你喜欢
    • 2014-01-06
    • 1970-01-01
    • 1970-01-01
    • 2019-05-29
    • 1970-01-01
    • 1970-01-01
    • 2019-01-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多