How to Select Just One Radiobutton With Recyclerview Android

Select only one radiobutton in a recyclerview

Here is another method to do the same. This is more elegant than my previous answer. But I have kept both as the previous answer provides more flexibility.

private RadioButton lastCheckedRB = null;
...
@Override
public void onBindViewHolder(final CoachListViewHolder holder, final int position) {
holder.priceRadioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
RadioButton checked_rb = (RadioButton) group.findViewById(checkedId);
if (lastCheckedRB != null) {
lastCheckedRB.setChecked(false);
}
//store the clicked radiobutton
lastCheckedRB = checked_rb;
}
});

Single selection in RecyclerView

The solution for the issue:

public class yourRecyclerViewAdapter extends RecyclerView.Adapter<yourRecyclerViewAdapter.yourViewHolder> {

private static CheckBox lastChecked = null;
private static int lastCheckedPos = 0;


public void onBindViewHolder(ViewHolder holder, final int position) {

holder.mTextView.setText(fonts.get(position).getName());
holder.checkBox.setChecked(fonts.get(position).isSelected());
holder.checkBox.setTag(new Integer(position));

//for default check in first item
if(position == 0 && fonts.get(0).isSelected() && holder.checkBox.isChecked())
{
lastChecked = holder.checkBox;
lastCheckedPos = 0;
}

holder.checkBox.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
CheckBox cb = (CheckBox)v;
int clickedPos = ((Integer)cb.getTag()).intValue();

if(cb.isChecked())
{
if(lastChecked != null)
{
lastChecked.setChecked(false);
fonts.get(lastCheckedPos).setSelected(false);
}

lastChecked = cb;
lastCheckedPos = clickedPos;
}
else
lastChecked = null;

fonts.get(clickedPos).setSelected(cb.isChecked);
}
});
}
}

Single choice Radion Button in Recycler view (Duplicate)

Add your onCheckedChnageListener() in your onBindViewHolder() method and maintain your mSelectedItem inside the onCheckedChnageListener()

 viewHolder.rdCategoryItem.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
mSelectedItem=position;
notifyDataSetChanged();
}
});

How to select one radio button from radiogroup in a RecylcerView in android?

Use an interface as a listener of radio button click.

public interface AnswerSelectedListener {
void answerSelected(String answer);
}

Add an AnswerSelectedListener class attribute to your DailyQuestionModel class and constructor & Pass an implementation of this interface to each of your DailyQuestionModel:

questionList.add(new DailyQuestionModel(
getResources().getString(R.string.q01),
new AnswerSelectedListener() {
@Override
public void answerSelected(String answer) {
//Here you have the selected answer to question 01
}
})
)

Inside you RecyclerView adapter, call this listener when an answer is selected:

@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
final DailyQuestionModel model = questionList.get(position);
String question = model.getQuestion();
holder.questionText.setText(question);
answers.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
public void onCheckedChanged(RadioGroup group, int checkedId) {
RadioButton checkedRadioButton = (RadioButton)group.findViewById(checkedId);
boolean isChecked = checkedRadioButton.isChecked();
if (isChecked) {
model.answerListener.answerSelected(checkedRadioButton.getText()))
}
}
});
}

This code is not tested but I think it gives the overall flow.

android - Single RadioButton in RecyclerView

You can manage it by creating a model class with a boolean variable isChecked.

when you select a RadioButton first do isChecked = false for all and then make true for your selected button and then call notifyDataSetChanged.

In adapter use

radioButton.setChecked(list.get(position).isChecked())

It will help you definitely.



Related Topics



Leave a reply



Submit