Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Radio button with recyclerview in android

Tags:

android

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();
        }
    }
}
like image 821
Chhatrasal Singh Bundela Avatar asked Dec 24 '22 22:12

Chhatrasal Singh Bundela


1 Answers

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
}
like image 89
Savita Sharma Avatar answered Jan 05 '23 16:01

Savita Sharma