0

I have a fragment which contains a recyclerview and a floating action button. In recyclerview, each row contains a radio button set as disabled by default. my requirement is I need to enable these radio buttons in every row upon floating action button click event. Is there any solution?

Thanks in advance..

OK... I got a solution.

here is my code.

fab.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {

                    for (int i = 0; i <= adapter.getItemCount(); i++) {

                        View childVIew = recyclerView.getChildAt(i);

                        if (childVIew != null) {
                            RadioButton radioYes = childVIew.findViewById(R.id.radio_yes_my_health_info);
                            radioYes.setEnabled(true);

                            RadioButton radioNo = childVIew.findViewById(R.id.radio_no_my_health_info);
                            radioNo.setEnabled(true);
                        }
                    }

                    fab.setImageResource(R.drawable.ic_save_white_24dp);
                    recyclerView.getRecycledViewPool().setMaxRecycledViews(0, 0);
                }
            });

Now on FAB button click, all the radio buttons inside my recyclerview will be enabled and I can click to change the state of every radio button. But here I have got a problem. When I click FAB button, all the radio buttons inside the recyclerview will be activated but when I scroll the recyclerview all those radio buttons will be disabled or deactivated again. Please give me a solution for this.

2 Answers2

0

You can try looping through every view when you click the button, like so:

for (int i = 0; i < adapter.getItemCount(); i++) {
    View view = recycler.getLayoutManager().findViewByPosition(i);
    view.setSelected(true); //Select item
    notifyItemChanged(i); //Notify item changed
}
Justin Dachille
  • 133
  • 1
  • 1
  • 9
  • how can I select only radio button from every row? In fact, the recycler contains one textbox and two radio buttons in every row. I need to select only these two radio buttons from every row. In your method, we are getting a view by using findViewByPosition(i). This may return all the views including text box and two radio buttons. I only need two radio buttons and I need to enable these buttons. By default these buttons are disabled. – Shibin Kalliatt Nov 27 '17 at 02:41
  • You can use view.findViewById(R.id.radio_button) to get the radio button you want to select. – Justin Dachille Nov 27 '17 at 02:46
  • I have 20 items in my adapter. But in the for loop, it shows only 7 items. After 7 I'm getting null pointer exception. any idea? – Shibin Kalliatt Nov 27 '17 at 04:15
  • ok. I have got a solution. Please review my question once again. – Shibin Kalliatt Nov 27 '17 at 05:12
  • Looks like it isn't recycling correctly. Take a look at your onBindViewHolder, as it is called every time the recyclerview needs a new view when scrolling. Some other things you can try is adding these functions to your adapter: `@Override public long getItemId(int position) { return position; } @Override public int getItemViewType(int position) { return position; }` and setting the recycler to `setHasStableIds(true);` - from this thread https://stackoverflow.com/questions/32065267/recyclerview-changing-items-during-scroll – Justin Dachille Nov 27 '17 at 17:08
0

You can have a flag in your adapter like

boolean isActivated = false;

public void setActivated(boolean activated) {
    isActivated = activated;
}

and in your onBindViewHolder() method you can check for the flag and set the state of your flag accordingly.

In the OnClickListener of your FAB, call

adapter.setActivated(true);//true or false as per your requirement
adapter.notifyDataSetChanged();

This should work.

Shivam
  • 1,086
  • 7
  • 8