Java RadioGroup中的RecyclerView和RadioButton

Java RadioGroup中的RecyclerView和RadioButton,java,android,android-recyclerview,radio-button,radio-group,Java,Android,Android Recyclerview,Radio Button,Radio Group,我有RecyclerView,在RowItem布局中有RadioGroup,其中有两个RadioButton。如果我选择第一项并滚动到底部,第八项、第十六项,。。。因此,检查:| 怎么处理 tnx的答案。。如果你需要任何信息。。我把主题 ques\u item\u row.xml <RadioGroup android:id="@+id/ques_radio_group" android:layout_wid

我有RecyclerView,在RowItem布局中有RadioGroup,其中有两个RadioButton。如果我选择第一项并滚动到底部,第八项、第十六项,。。。因此,检查:| 怎么处理

tnx的答案。。如果你需要任何信息。。我把主题

ques\u item\u row.xml

 <RadioGroup
                    android:id="@+id/ques_radio_group"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_below="@+id/txt_ques_title"
                    android:layout_gravity="end|center"
                    android:gravity="start"
                    android:orientation="vertical"
                    android:textAlignment="textStart"
                    android:textDirection="rtl">

                    <android.support.v7.widget.AppCompatRadioButton
                        android:id="@+id/first_ques"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_gravity="end|right"
                        android:layout_margin="@dimen/quest_margin"
                        android:gravity="end|right"
                        android:checked="false"
                        android:padding="@dimen/quest_padding"
                        android:text="@{question.firstQuest}"
                        android:textColor="@color/ques"
                        android:textDirection="ltr"
                        android:textSize="@dimen/text_ques"
                        />

                    <android.support.v7.widget.AppCompatRadioButton
                        android:id="@+id/second_ques"
                        android:layout_below="@+id/first_ques"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_gravity="end|right"
                        android:layout_margin="@dimen/quest_margin"
                        android:gravity="end|right"
                        android:checked="false"
                        android:padding="@dimen/quest_padding"
                        android:text="@{question.secondQuest}"
                        android:textColor="@color/ques"
                        android:textDirection="ltr"
                        android:textSize="@dimen/text_ques"
                        />


                </RadioGroup>
<data>

    <variable
        name="item"
        type="com.innovanathinklabs.sample.ui2.Item"/>

</data>

<RadioGroup
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginBottom="20dp"
    android:background="@color/colorPrimary"
    android:checkedButton="@={item.selectedId}"
    >

    <android.support.v7.widget.AppCompatRadioButton
        android:id="@+id/rbMale"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="male"/>

    <android.support.v7.widget.AppCompatRadioButton
        android:id="@+id/rbFemale"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="female"/>
</RadioGroup>
QuestionAdapter.java

public class Question {

  private String questNum;
  private String questTitle;
  private String firstQuest;
  private String secondQuest;
  private int score = 0;
  private int firstQuestScore = 2;
  private int secondQuestScore = 3;

  private boolean isFirstChecked;
  private boolean isSecondChecked;


  public Question(String questNum, String questTitle, String firstQuest, String secondQuest) {
    this.questTitle = questTitle;
    this.firstQuest = firstQuest;
    this.secondQuest = secondQuest;
    this.questNum = questNum;

  }

  public Question() {

  }


  public String getQuestTitle() {
    return questTitle;
  }

  public void setQuestTitle(String questTitle) {
    this.questTitle = questTitle;
  }

  public String getFirstQuest() {
    return firstQuest;
  }

  public void setFirstQuest(String firstQuest) {
    this.firstQuest = firstQuest;
  }

  public String getSecondQuest() {
    return secondQuest;
  }

  public void setSecondQuest(String secondQuest) {
    this.secondQuest = secondQuest;
  }

  public String getQuestNum() {
    return questNum;
  }

  public void setQuestNum(String questNum) {
    this.questNum = questNum;
  }

  public boolean isFirstChecked() {
    return isFirstChecked;
  }

  public void setFirstChecked(boolean firstChecked) {
    isFirstChecked = firstChecked;
  }

  public boolean isSecondChecked() {
    return isSecondChecked;
  }

  public void setSecondChecked(boolean secondChecked) {
    isSecondChecked = secondChecked;
  }

  public int getScore() {
    return score;
  }

  public void setScore(int questNum, int questScore) {
    this.score = ((questNum * questScore) + questNum);
  }


  public int getFirstQuestScore() {
    return firstQuestScore;
  }

  public void setFirstQuestScore(int firstQuestScore) {
    this.firstQuestScore = firstQuestScore;
  }

  public int getSecondQuestScore() {
    return secondQuestScore;
  }

  public void setSecondQuestScore(int secondQuestScore) {
    this.secondQuestScore = secondQuestScore;
  }


  public void mathScore(int questNum, int questScore) {
  }
}
 public class MbtiQuestAdapter extends RecyclerView.Adapter<MbtiQuestAdapter.MbtiQuestViewHolder> {

  private Context context;
  private List<Question> questionList;
  private LayoutInflater layoutInflater;
  private FloatingActionButton fabResult;
  private CoordinatorLayout coordinator;
  private int checkRadio = 0;
  private Snackbar snackbar;
  private int totalScore = 0;


  private static final String TAG = "MbtiQuestAdapter";

  public MbtiQuestAdapter(Context context, List<Question> questionList, FloatingActionButton fab, CoordinatorLayout coordinator) {
    this.context = context;
    this.questionList = questionList;
    this.fabResult = fab;
    this.coordinator = coordinator;
  }


  @NonNull
  @Override
  public MbtiQuestViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    if (layoutInflater == null) {
      layoutInflater = LayoutInflater.from(context);
    }

    QuesItemRowBinding binding = DataBindingUtil.inflate(layoutInflater,
      R.layout.ques_item_row, parent, false);

    return new MbtiQuestViewHolder(binding);
  }


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

    final Question question = questionList.get(position);
    holder.binding.setQuestion(question);

    //Fix the select the radio Button when first Select 8th and... do not selected



    //************************************************************


    //Check if all question do not click do not allow go to the next page

    holder.binding.quesRadioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
      @Override
      public void onCheckedChanged(RadioGroup group, int checkedId) {
        if (holder.binding.firstQues.isChecked() || holder.binding.secondQues.isChecked()) {
          checkRadio++;

        } else {
//          snackbar = Snackbar.make(coordinator, R.string.choose_one_of_theme, Snackbar.LENGTH_INDEFINITE);
//          snackbar.show();
        }
      }
    });



   if (fabResult != null) {
      fabResult.setOnClickListener(new View.OnClickListener() {
                                     @Override
                                     public void onClick(View v) {
                                       if (checkRadio == 60) {
                                         context.startActivity(new Intent(context, ResultActivity.class));

                                         totalScore = 0;
                                         for (int i = 0; i < 60; i++) {
                                           totalScore = totalScore + question.getScore();
                                           Log.i(TAG, "TotalScore: " + totalScore);
                                         }


                                         switch (totalScore) {
                                         }

                                       } else {
                                         snackbar = Snackbar.make(coordinator, R.string.check_all_ques_err, Snackbar.LENGTH_SHORT);
                                         snackbar.show();


                                       }
                                     }
                                   }

      );

      //*************************************************************



    }
  }


  @Override
  public int getItemCount() {
    return questionList.size();
  }

  public class MbtiQuestViewHolder extends RecyclerView.ViewHolder {

    private QuesItemRowBinding binding;

    public MbtiQuestViewHolder(QuesItemRowBinding binding) {
      super(binding.getRoot());
      this.binding = binding;

    }
  }


}
public class Item extends BaseObservable{
    private int selectedId;

    public int getSelectedId() {
        return selectedId;
    }

    public void setSelectedId(int selectedId) {
        this.selectedId = selectedId;
    }
}


private int isFirstQuestionChecked=false添加到您的模型中,并在您选择此
单选按钮时对其进行更改。在适配器中显示
单选按钮的正确值

if (element.isFirstQuestionChecked == true) {
    selectRadioButton()
} else {
    deselectRadioButton() // it's important!
}

您必须将选定的单选按钮id保留到您的型号。

1> 在您的模型中使用selectedId

class Model{
  int selctedId;
  // getter setter
}
2> 将此id附加到您的广播组

@Override
public void onBindViewHolder(final CoachListViewHolder holder, final int position) {
    Model model = list.get(position);
    holder.radioGroup.check(model.getSelectedId);
    holder.radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener()
    {
        public void onCheckedChanged(RadioGroup group, int checkedId)
        {
         model.setSelectedId(checkedId);
        }
    });
在这个解决方案中,我们在模型中保留所选id,并通过
radioGroup.check(model.getSelectedId)将此字段附加回无线组

原因 当您不持有所选值时,当用户滚动位置时,该值将被回收

我还找到了一个相关的

经过充分测试的溶液 您使用的是数据绑定,所以上述解决方案可能会更简短。用于保存所选id

Item.java

public class Question {

  private String questNum;
  private String questTitle;
  private String firstQuest;
  private String secondQuest;
  private int score = 0;
  private int firstQuestScore = 2;
  private int secondQuestScore = 3;

  private boolean isFirstChecked;
  private boolean isSecondChecked;


  public Question(String questNum, String questTitle, String firstQuest, String secondQuest) {
    this.questTitle = questTitle;
    this.firstQuest = firstQuest;
    this.secondQuest = secondQuest;
    this.questNum = questNum;

  }

  public Question() {

  }


  public String getQuestTitle() {
    return questTitle;
  }

  public void setQuestTitle(String questTitle) {
    this.questTitle = questTitle;
  }

  public String getFirstQuest() {
    return firstQuest;
  }

  public void setFirstQuest(String firstQuest) {
    this.firstQuest = firstQuest;
  }

  public String getSecondQuest() {
    return secondQuest;
  }

  public void setSecondQuest(String secondQuest) {
    this.secondQuest = secondQuest;
  }

  public String getQuestNum() {
    return questNum;
  }

  public void setQuestNum(String questNum) {
    this.questNum = questNum;
  }

  public boolean isFirstChecked() {
    return isFirstChecked;
  }

  public void setFirstChecked(boolean firstChecked) {
    isFirstChecked = firstChecked;
  }

  public boolean isSecondChecked() {
    return isSecondChecked;
  }

  public void setSecondChecked(boolean secondChecked) {
    isSecondChecked = secondChecked;
  }

  public int getScore() {
    return score;
  }

  public void setScore(int questNum, int questScore) {
    this.score = ((questNum * questScore) + questNum);
  }


  public int getFirstQuestScore() {
    return firstQuestScore;
  }

  public void setFirstQuestScore(int firstQuestScore) {
    this.firstQuestScore = firstQuestScore;
  }

  public int getSecondQuestScore() {
    return secondQuestScore;
  }

  public void setSecondQuestScore(int secondQuestScore) {
    this.secondQuestScore = secondQuestScore;
  }


  public void mathScore(int questNum, int questScore) {
  }
}
 public class MbtiQuestAdapter extends RecyclerView.Adapter<MbtiQuestAdapter.MbtiQuestViewHolder> {

  private Context context;
  private List<Question> questionList;
  private LayoutInflater layoutInflater;
  private FloatingActionButton fabResult;
  private CoordinatorLayout coordinator;
  private int checkRadio = 0;
  private Snackbar snackbar;
  private int totalScore = 0;


  private static final String TAG = "MbtiQuestAdapter";

  public MbtiQuestAdapter(Context context, List<Question> questionList, FloatingActionButton fab, CoordinatorLayout coordinator) {
    this.context = context;
    this.questionList = questionList;
    this.fabResult = fab;
    this.coordinator = coordinator;
  }


  @NonNull
  @Override
  public MbtiQuestViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    if (layoutInflater == null) {
      layoutInflater = LayoutInflater.from(context);
    }

    QuesItemRowBinding binding = DataBindingUtil.inflate(layoutInflater,
      R.layout.ques_item_row, parent, false);

    return new MbtiQuestViewHolder(binding);
  }


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

    final Question question = questionList.get(position);
    holder.binding.setQuestion(question);

    //Fix the select the radio Button when first Select 8th and... do not selected



    //************************************************************


    //Check if all question do not click do not allow go to the next page

    holder.binding.quesRadioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
      @Override
      public void onCheckedChanged(RadioGroup group, int checkedId) {
        if (holder.binding.firstQues.isChecked() || holder.binding.secondQues.isChecked()) {
          checkRadio++;

        } else {
//          snackbar = Snackbar.make(coordinator, R.string.choose_one_of_theme, Snackbar.LENGTH_INDEFINITE);
//          snackbar.show();
        }
      }
    });



   if (fabResult != null) {
      fabResult.setOnClickListener(new View.OnClickListener() {
                                     @Override
                                     public void onClick(View v) {
                                       if (checkRadio == 60) {
                                         context.startActivity(new Intent(context, ResultActivity.class));

                                         totalScore = 0;
                                         for (int i = 0; i < 60; i++) {
                                           totalScore = totalScore + question.getScore();
                                           Log.i(TAG, "TotalScore: " + totalScore);
                                         }


                                         switch (totalScore) {
                                         }

                                       } else {
                                         snackbar = Snackbar.make(coordinator, R.string.check_all_ques_err, Snackbar.LENGTH_SHORT);
                                         snackbar.show();


                                       }
                                     }
                                   }

      );

      //*************************************************************



    }
  }


  @Override
  public int getItemCount() {
    return questionList.size();
  }

  public class MbtiQuestViewHolder extends RecyclerView.ViewHolder {

    private QuesItemRowBinding binding;

    public MbtiQuestViewHolder(QuesItemRowBinding binding) {
      super(binding.getRoot());
      this.binding = binding;

    }
  }


}
public class Item extends BaseObservable{
    private int selectedId;

    public int getSelectedId() {
        return selectedId;
    }

    public void setSelectedId(int selectedId) {
        this.selectedId = selectedId;
    }
}
行列表.xml

 <RadioGroup
                    android:id="@+id/ques_radio_group"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_below="@+id/txt_ques_title"
                    android:layout_gravity="end|center"
                    android:gravity="start"
                    android:orientation="vertical"
                    android:textAlignment="textStart"
                    android:textDirection="rtl">

                    <android.support.v7.widget.AppCompatRadioButton
                        android:id="@+id/first_ques"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_gravity="end|right"
                        android:layout_margin="@dimen/quest_margin"
                        android:gravity="end|right"
                        android:checked="false"
                        android:padding="@dimen/quest_padding"
                        android:text="@{question.firstQuest}"
                        android:textColor="@color/ques"
                        android:textDirection="ltr"
                        android:textSize="@dimen/text_ques"
                        />

                    <android.support.v7.widget.AppCompatRadioButton
                        android:id="@+id/second_ques"
                        android:layout_below="@+id/first_ques"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_gravity="end|right"
                        android:layout_margin="@dimen/quest_margin"
                        android:gravity="end|right"
                        android:checked="false"
                        android:padding="@dimen/quest_padding"
                        android:text="@{question.secondQuest}"
                        android:textColor="@color/ques"
                        android:textDirection="ltr"
                        android:textSize="@dimen/text_ques"
                        />


                </RadioGroup>
<data>

    <variable
        name="item"
        type="com.innovanathinklabs.sample.ui2.Item"/>

</data>

<RadioGroup
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginBottom="20dp"
    android:background="@color/colorPrimary"
    android:checkedButton="@={item.selectedId}"
    >

    <android.support.v7.widget.AppCompatRadioButton
        android:id="@+id/rbMale"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="male"/>

    <android.support.v7.widget.AppCompatRadioButton
        android:id="@+id/rbFemale"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="female"/>
</RadioGroup>

同时从单选组和单选按钮中删除任何其他不必要的逻辑

数据绑定的魔力在于 此代码被转换为
android:checkedButton=“@={item.selectedId}”

使现代化 这是在项目布局中绑定项目的方式

holder.binding.setItem(list.get(position));

在这里添加你的回收适配器代码了解你的问题,参考这个答案链接我累了,但我得到了错误,等一下我得到了这个错误,看起来你的帖子主要是代码;请添加更多详细信息。请尝试添加onBindViewHolder()和相关代码。不要尝试添加整个code.tnx,但是isFirstQuestionChecked应该是布尔值,对吗?请给我解释一下密码。选择RadioButton(),取消选择RadioButton()?是,firstQuestionChecked是布尔变量。每次您选中/取消选中radioButton时,您都会在您的模型中执行相同的操作。所以列表中的每个单选按钮都存储在你们的模型中——并没有机会将答案附加到错误的单元格中。在stackoverflow上有很多类似的主题。。。我打了个结,但我有个问题。。。当选中radioButton和scrool时,所有选项都变为取消选中:(::‌‌ ‍‍‍‍‍<代码>if(question.isFirstChecked()){holder.binding.firstQues.setChecked(true);}else{holder.binding.firstQues.setChecked(false);}if(question.isSecondChecked()){holder.binding.secondQues.setChecked(true);}else{holder.binding.secondQues.setChecked(false);}
您是否在模型中更改了getter isFirstChecked()下的变量?您也应该在适配器外执行此操作。使用接口回调来执行此操作。``private boolean isFirstQuestionChecked=false;private boolean isSecondQuestionChecked=false;``在模型中和::
if(question.isFirstQuestionChecked()){holder.binding.firstQues.setChecked(true);}else{holder.binding.firstQues.setChecked(false);}if(question.isSecondQuestionChecked()){holder.binding.secondQues.setChecked(true);}else{holder.binding.secondQues.setChecked(false);}
在adapter.callback中?你是什么意思?请为我们解释一下。