Reputation: 253
I have a RecyclerView with RadioButton I just want to select one RadioButton not many in the same time and my code make it works fine, but when I repeat the selection from top to bottom the select disappear how can I fix it please thanks.
private RadioButton lastCheckedRB = null;
@Override
public void onBindViewHolder(final MyViewHolder holder, int position) {
ContactUsModel contactUsModel = orderList.get(position);
holder.orderNum.setText(contactUsModel.getOrderNum());
holder.orderCost.setText(contactUsModel.getOrderCost());
holder.radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {
RadioButton checked_rb = (RadioButton) group.findViewById(checkedId);
if (lastCheckedRB != null &&lastCheckedRB.isChecked()) {
lastCheckedRB.setChecked(false);
}
//store the clicked radiobutton
lastCheckedRB = checked_rb;
}
});
Upvotes: 4
Views: 10455
Reputation: 45
I know I am way way way late to reply to this thread but if anyone in the future gets stuck in a similar problem please use the below example to resolve it, especially with RadioButton
on RecyclerView
:
At the TOP:
//initialize as Global variable below variable
int lastSelectedPosition = -1;// you can use any value but keep it
//at negative because array numbering starts at the positive
int tempPosition;
//Below code inside onBindViewHolder() function of RecyclerView
//selectedRadio is my RadioButton
holder.selectedRadio.setOnCheckedChangeListener(new
CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton,
boolean b)
{
tempPosition = position;//getting position of click radioButton
if (lastSelectedPosition != position)//checking last position
//and present position
{
holder.selectedRadio.setChecked(true);
if (lastSelectedPosition != -1){
//very Important code below line where the magic happens
notifyItemChanged(lastSelectedPosition);
}
lastSelectedPosition = tempPosition;
}
}
});
That's all, waaalaaaa..Tat tara raaaaaa... now your radio button is working as you have wanted it.
Thank you.
Upvotes: -1
Reputation: 121
You don't need keep the position with Tag
or to reload the list with notifyDataSetChanged()
.
Just use checkedRadioButton
and toggle it. Then you can use checkedRadioButton
to get the selected item.
private var checkedRadioButton: CompoundButton? = null
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
holder.itemView.radio_button.setOnCheckedChangeListener(checkedChangeListener)
if (holder.itemView.radio_button.isChecked) checkedRadioButton = holder.itemView.radio_button
}
private val checkedChangeListener = CompoundButton.OnCheckedChangeListener { compoundButton, isChecked ->
checkedRadioButton?.apply { setChecked(!isChecked) }
checkedRadioButton = compoundButton.apply { setChecked(isChecked) }
}
Upvotes: 8
Reputation: 302
1st: get individual radio button id. 2nd: assign click listener to individual radio button.
say you have 6 radio buttons:
{
radioButton1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
radioButton1.setChecked(true);
radioButton2.setChecked(false);
radioButton3.setChecked(false);
radioButton4.setChecked(false);
radioButton5.setChecked(false);
radioButton6.setChecked(false);
}
});
//similarly add listeners to all radio buttons:
radioButton2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
radioButton2.setChecked(true);
radioButton1.setChecked(false);
radioButton3.setChecked(false);
radioButton4.setChecked(false);
radioButton5.setChecked(false);
radioButton6.setChecked(false);
}
});
}
Upvotes: -5
Reputation: 1185
You can do as,Use RadioButton I just write without check But this is an hint for you -
private CompoundButton lastCheckedRB = null;
@Override
public void onBindViewHolder(final MyViewHolder holder, int position) {
ContactUsModel contactUsModel = orderList.get(position);
holder.orderNum.setText(contactUsModel.getOrderNum());
holder.orderCost.setText(contactUsModel.getOrderCost());
holder.readioBtn.setOnCheckedChangeListener(ls);
holder.readioBtn.setTag(position);
}
private OnCheckedChangeListener ls = (new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
int tag = (int) buttonView.getTag();
;
if (lastCheckedRB == null) {
lastCheckedRB = buttonView;
} else if (tag != (int) lastCheckedRB.getTag()) {
lastCheckedRB.setChecked(false);
lastCheckedRB = buttonView;
}
}
});
Upvotes: 11
Reputation: 1614
Use One temp Variable in your orderList
ex: isSelected = false;
Make sure this is added in all object of your arrayList
When u selects any radio button just update current position orderList
isSelected = true
and remaining isSelected = false
;
and call notifyDataSetChanged()
also make sure you are checking
if(isSelcted)
redioButton.setChecked(true);
else
redioButton.setChecked(false);
ex: if(csSelectedAddressPos == position) holder.radioBtn.setChecked(true); else holder.radioBtn.setChecked(false);
holder.radioBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//set object isChecked here
csSelectedAddressPos = position;
notifyDataSetChanged();
}
});
Upvotes: 1