0

I have a problem with RecyclerView, I have RecyclerView which has a radio button and few other views in each row item,

What I wanted exactly is, when a RadioButton is checked by user I want to uncheck other RadioButton(if anything is checked earlier). Since it is a Recyclerview I cannot use radiogroup.

In the adapter onBindViewHolder I write this listener for each radio button

  holder.radioButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            selectedPosition = holder.getAdapterPosition();
            Toast.makeText(activity, "section: " + selectedPosition, Toast.LENGTH_SHORT).show();
        }
    });

how can i make previous radio button selected uncheck?
in other words, how can i update view property in specific item of recyclerview from another item view listener?

devgun
  • 1,003
  • 13
  • 33
Mosa
  • 353
  • 2
  • 16
  • 1
    see this one ..https://stackoverflow.com/questions/46344032/how-to-select-just-one-radiobutton-with-recyclerview-android/46344598#46344598 – Sanjay Kumar Dec 18 '17 at 08:54
  • @Sanjay Majoka oh ya this is what i want, i try it and it worked fine thank you very much for this great help :) – Mosa Dec 18 '17 at 09:09

2 Answers2

2

You need to have a reference to the selectedItem globally in your adapter and then update all the items when the user checks a new radio button.

public class Adapter extends RecyclerView.Adapter<Adapter.ViewHolder> {
    private int selectedItemPosition = -1;
}

And do things in your onBindViewHolder() method of the adapter,

@Override
public void onBindViewHolder(final Adapter.ViewHolder holder, int position) {
    holder.radioButton.setChecked(holder.getAdapterPosition()==selectedItemPosition);
    holder.radioButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            selectedItemPosition = holder.getAdapterPosition();
            notifyDataSetChanged();
        }
    });
}
devgun
  • 1,003
  • 13
  • 33
  • ok greate and it works fine, but when i update all the items and recyclerview has large mount of data will be some lag for a few seconds ? – Mosa Dec 18 '17 at 09:21
  • recyclerview will not update all the items in the list at a time. As the name indicates, Recyclerview will not render or even draw as much of items in your list, it will just reuse the views. Each item will get updated when they comes in the display screen. So no need to worry about lags. – devgun Dec 18 '17 at 09:27
  • ah i see , thanks for the explanation and making things clear – Mosa Dec 18 '17 at 09:40
1

Try this::

        if (position == selectedPosition) {
            holder.radioButton.setChecked(true);
            Toast.makeText(activity, "section: " + selectedPosition, Toast.LENGTH_SHORT).show();
        } else {
            holder.radioButton.setChecked(false);
        }

        holder.radioButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                selectedPosition = holder.getAdapterPosition();
                notifyDataSetChanged();
            }
        });
n_r
  • 589
  • 7
  • 17
  • i found my request in the link that @Sanjay Majoka gave it to me , but thank you this is another good answer and it work for me – Mosa Dec 18 '17 at 09:12