万一任何人发现此问题,希望以后对您有所帮助。我想分享最终的解决方案,因为我认为它比我开始的代码有更多帮助,并且无法获得导致该解决方案的任何答案。
这是我的解决方案的参考: How can I select only one checkbox in Recyclerview and notifydataset changed
我只共享代码的关键部分,这些部分直接影响单选按钮的选择问题。
这是我的解决方案:
public class NewGameCourseAdapter extends RecyclerView.Adapter<NewGameCourseAdapter.NewGameCourseViewHolder> {
private int selectedPosition = -1;// no selection by default
@Override
public void onBindViewHolder(@NonNull final NewGameCourseViewHolder holder, final int position) {
/** This can prevent some unwanted actions in some cases **/
holder.mRadioButton.setOnCheckedChangeListener(null);
holder.mRadioButton.setChecked(selectedPosition == position);
holder.mRadioButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
selectedPosition = holder.getAdapterPosition();
if (selectedPosition == position) {
holder.mRadioButton.setChecked(true);
notifyDataSetChanged();
} else {
holder.mRadioButton.setChecked(false);
notifyDataSetChanged();
}
}
});