4

This is my code for single selection in the recyclerview. I am able to select single choice in the recycler list but when i deselect the radiobutton then i have to click twice on the radio button to again enable is i.e to select the radiobutton again. Can any one help me with this....

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CompoundButton;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

public class Address_Adapter extends RecyclerView.Adapter<Address_Adapter.AddresssHolder>{

    private List<Model_Address> listData;
    private LayoutInflater inflater;
    private Context context;
    private RadioButton rbChecked = null;
    private int rbPosoition = 0;

    public Address_Adapter(List<Model_Address> listData, Context context) {
        this.listData = listData;
        this.context = context;
        this.inflater = LayoutInflater.from(context);
    }

    @Override
    public AddresssHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = inflater.inflate(R.layout.layout_recycler_address, parent,false);
        return new AddresssHolder(view,listData,context);    }

    @Override
    public void onBindViewHolder(final AddresssHolder holder, final int position) {
        Model_Address item = listData.get(position);
        holder.address.setText(item.getAddress());
        holder.state.setText(item.getState());
        holder.pin_code.setText(item.getPin_code());
        holder.contact.setText(item.getContact());

        if (holder.selected.isChecked()){
            rbPosoition = holder.getAdapterPosition();
            Toast.makeText(context, "" + rbPosoition, Toast.LENGTH_SHORT).show();
        }

    }

    @Override
    public int getItemCount() {
        return listData.size();
    }

    public class AddresssHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

        private TextView address, state, pin_code, contact;
        private RadioButton selected;
        private  View container;
        private  List<Model_Address> listData;
        private Context context;

        public AddresssHolder(View itemView,List<Model_Address> listdata,Context context) {
            super(itemView);
            this.listData = listdata;
            this.context = context;
            itemView.setOnClickListener(this);
            address = (TextView) itemView.findViewById(R.id.text_address);
            state = (TextView) itemView.findViewById(R.id.state_text);
            pin_code = (TextView) itemView.findViewById(R.id.pin_code_text);
            contact = (TextView) itemView.findViewById(R.id.contact_text);
            selected = (RadioButton) itemView.findViewById(R.id.select_radioButton);
            if (rbPosoition == 0 && selected.isChecked())
            {
                rbChecked = selected;
                rbPosoition = 0;
            }
            selected.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    RadioButton rb = (RadioButton) v;
                    int clickedPos = getAdapterPosition();
                    if (rb.isChecked()){
                        if(rbChecked != null)
                        {
                            rbChecked.setChecked(false);
                        }
                        rbChecked = rb;
                        rbPosoition = clickedPos;
                    }
                    else{
                        rbChecked = null;
                        rbPosoition = 0;
                    }

                }
            });

        }

        @Override
        public void onClick(View v) {
            int positenter code hereion = getAdapterPosition();
        }
    }
}
  • Why there is global RadioButton rbChecked ? – UMESH0492 Dec 20 '16 at 21:22
  • To get the reference of the checked radiobutton.. – Chhatrasal Singh Bundela Dec 20 '16 at 21:43
  • 1
    it will be wiser to store the position of the checked radio button and then uncheck or check it (after a check event or view created on the screen after being recycled) – Nilesh Singh Dec 20 '16 at 22:30
  • I can store the position of the checked RadioButton by using getAdapterPosition(). But I dont know how to manipulate that because when the new RadioButton will be checked it will give the current adapters position and I dont know how to manipulate the RadioButton at a specific adapter position.. Can you help me with the codes?? – Chhatrasal Singh Bundela Dec 21 '16 at 00:53

2 Answers2

7

Check this one, hope it will help you

class MyViewHolder extends RecyclerView.ViewHolder{
    LinearLayout linear_row;
    RadioButton rb_room;
    TextView tv_amount, tv_booking_policy;

    public MyViewHolder(View itemView) {
        super(itemView);
        linear_row = (LinearLayout) itemView.findViewById(R.id.linear_row);
        rb_room = (RadioButton) itemView.findViewById(R.id.rb_room);
        tv_booking_policy = (TextView) itemView.findViewById(R.id.tv_booking_policy);
        tv_amount = (TextView) itemView.findViewById(R.id.tv_amount);

        linear_row.setOnClickListener(this);

        // use from here*********************
        View.OnClickListener l = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mSelectedItem = getAdapterPosition();
                notifyItemRangeChanged(0, roomList.size());
            }
        };

        itemView.setOnClickListener(l);
        rb_room.setOnClickListener(l);
        // till here************************
    }

        public void onBindViewHolder(final MyViewHolder holder, final int position) {
    holder.rb_room.setChecked(position == mSelectedItem); // from this you only get one radio button selected in recycler view
}
Savita Sharma
  • 344
  • 3
  • 9
3

Check this out

abstract class RadioAdapter<T> extends RecyclerView.Adapter<RadioAdapter.ViewHolder> {

public int mSelectedItem = -1;
private Context mContext;
private List<T> mItems;

public RadioAdapter(Context context, List<T> items) {
    mContext = context;
    mItems = items;
}

@Override
public void onBindViewHolder(ViewHolder viewHolder, final int i) {
    viewHolder.mRadio.setChecked(i == mSelectedItem);
}

@Override
public int getItemCount() {
    return mItems.size();
}

@Override
public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
    final View view = LayoutInflater.from(mContext).inflate(R.layout.view_radio_item, viewGroup, false);
    return new ViewHolder(view);
}

public class ViewHolder extends RecyclerView.ViewHolder {

    public RadioButton mRadio;
    public TextView mText;

    public ViewHolder(final View inflate) {
        super(inflate);
        mText = (TextView) inflate.findViewById(R.id.text);
        mRadio = (RadioButton) inflate.findViewById(R.id.radio);
        View.OnClickListener l = v -> {
            mSelectedItem = getAdapterPosition();
            notifyItemRangeChanged(0, mItems.size());
        };
        itemView.setOnClickListener(l);
        mRadio.setOnClickListener(l);
    }
}

}

I guess notifyItemRangeChanged() method is probably what you are looking for.

It basically tells the adapter to reload the data in the recyclerView

Source: https://gist.github.com/mutexkid/4fcd3f7ba7d37ee69fee

anAmaka
  • 418
  • 7
  • 19