Nanda Gopal February 2016

How to implement RecycerView With Items having Radio buttons with single choice mode

I need to show a recyclerview with single choice item. but Recyclerview doesn't have the choice mode.

Does any one know how to resolve this?

Thanks in advance.

Answers


Vaibhav Sharma February 2016

Displaying already checks-

You can maintain a boolean array of size of your list. On bind view handle the boolean(true/ false) logic, like setting check true/false of a radio button.

Handling new clicks-

Then on the onclick of the radio button, set the value of every array element to false and setting the current array position to true after that. Now call notifyDatatSetChanged().


keith February 2016

You could use something like this, it stores position for selected item, you could add a bit more code to store the item itself if your adapter reorders itself

public class SingleChoiceAdapter extends RecyclerView.Adapter<SingleChoiceAdapter.ItemViewHolder> {

    private static final int NO_POSITION = -1;
    private static final String SELECTED_ITEM_POSITION = "SELECTED_ITEM_POSITION";

    private final LayoutInflater inflater;
    private final List<Item> items;

    private int selectedItemPosition = NO_POSITION;

    public SingleChoiceAdapter(Context context, Bundle savedInstanceState) {
        inflater = LayoutInflater.from(context);
        items = new ArrayList<>();
        if (savedInstanceState != null) {
            selectedItemPosition = savedInstanceState.getInt(SELECTED_ITEM_POSITION, NO_POSITION);
        }
    }

    @Override
    public ItemViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        final View view = inflater.inflate(R.layout.item, parent, false);
        return new ItemViewHolder(view);
    }

    @Override
    public void onBindViewHolder(final ItemViewHolder holder, int position) {
        final Item item = items.get(position);
        holder.textView.setText(item.name);
        holder.textView.setBackgroundColor(position == selectedItemPosition
                ? Color.LTGRAY
                : Color.TRANSPARENT);
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                updateSelectedItem(holder.getAdapterPosition());
            }
        });
    }

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

    public void setItems(List<Item> items) {
        this.items.clear();
        this.items.addAll(items);
        notifyDataSetChanged();
    }

    public Item getSelectedItem() {
        if (selectedItemPosition == NO_POSITION) {
            return nu 


Ravi Teja March 2016

Store the states of the checked items in a SparseBooleanArray with the position as key.As you change the state of the button,update it's status in the booleanArray and call notifyItemChanged(position).And in BindviewHolder(ViewHolder viewholder,int position) method load the state from booleanArray like viewholder.radioButton.setChecked(boolean.get(position)).Have a look at this for basic idea


Vinod Kollipara March 2016

You can even do it with just one single integer variable instead of maintaining an array which is costly in devices like android where space is a constraint.

So, just maintain a single integer variable, lets say int selectedPosition=-1 initially. In onBind check if the position==selectedPosition, if true check the button else uncheck. Whenever user checks/unchecks the button, just update the selectedPosition

Something like this,

if(selectedPosition==position)
   selectedPosition=-1
else{
   selectedPosition=position
   notifyItemChanged(selectedPosition);
}
notifyItemChanged(position);


Liem Vo March 2016

Please set the id for the position in onBindViewHolder then you can process action in this method too. Update data set and call notifyDataSetChanged

The result as below

Here is my example code

@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
    ItemObject itemObject = mDataset.get(position);
    ((ViewHolder)holder).mTextView.setText(itemObject.getTitle());
    Button mButton = ((ViewHolder) holder).mButton;


    mButton.setSelected(itemObject.isSelected());
    if(itemObject.isSelected()){

        ((ViewHolder)holder).mTextView.setText("OK");
    }
    mButton.setTag(position);
    mButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            updateDatset((Integer) v.getTag());
        }
    });
}

In the updateDataset method, I update information of this dataset and call notifyDataSetChanged()

Good luck

Post Status

Asked in February 2016
Viewed 3,982 times
Voted 13
Answered 5 times

Search




Leave an answer