0

I'm trying to make a RecyclerView with buttons, so that when user clicks on one then others become deselected. How to achieve this?

I tried searching Google and StackOverflow, but couldn't find answers to my case.

Adapter

public class LocationAreaAdapter extends RecyclerView.Adapter<LocationAreaAdapter.ViewHolder> {

        private int mSelectedItem = -1;
        private ArrayList<String> mNames = new ArrayList<>();
        private Context mContext;



        public LocationAreaAdapter(Context context,ArrayList<String> mNames ){

            this.mContext=context;
            this.mNames=mNames;
        }

        @NonNull
        @Override
        public LocationAreaAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
            View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.locatin_area_cardview, viewGroup, false);
            return new LocationAreaAdapter.ViewHolder(view);
        }

        @Override
        public void onBindViewHolder(@NonNull ViewHolder holder, int i) {

            holder.name.setText(mNames.get(i));
            holder.name.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    holder.name.setTextColor(Color.WHITE);
                    holder.name.setBackgroundResource(R.drawable.clicked_button);
                    onClickedLocationArea =  mNames.get(i);
                    loadhostelList();

                }
            });


        }


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

        public class ViewHolder extends RecyclerView.ViewHolder {


            Button name;

            public ViewHolder(View itemView) {
                super(itemView);
                name = itemView.findViewById(R.id.location_area_button);

            }
        }

    }

Could you please advise me about the right approach for my case?

Kate Orlova
  • 3,225
  • 5
  • 11
  • 35
Chirag Gupta
  • 469
  • 1
  • 7
  • 16
  • I hope this will help you : https://stackoverflow.com/questions/28972049/single-selection-in-recyclerview Of course you've to change few lines of code. – Modi Harsh Jul 09 '19 at 18:01

2 Answers2

0

The check/select status is properly implemented with state attribute inside the model which is passed to ViewHolder. Let me explain in this case what you should do. You have list of Strings called names, you should create model data called User( or whatever) with "name" and "selected" status. And pass the list of it to adapter. Then proper binding should be that you pass instance of User to each ViewHolder and bind info accordingly.. OnClick you will change "selected" state and every time call notifydatasetchanged.

After you will be able to get selected User from data even out of your adapter etc..

And reference to ViewHolder like @Bilal said is not good idea, since ViewHolders are reusing.

0

Set your click listener inside your viewholder class like this:

public class ViewHolder extends RecyclerView.ViewHolder {


    Button name, surname;

    public ViewHolder(View itemView) {
        super(itemView);
        name = itemView.findViewById(R.id.location_area_button);
        surname = itemView.findViewById(R.id.location_surname);

        name.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                surname.setEnabled(false);
                notifyItemChanged(getAdapterPosition());
            }
        });

        surname.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                name.setEnabled(false);
                notifyItemChanged(getAdapterPosition());
            }
        });
    }
}

I have taken surname as second button!!

Sumit Shukla
  • 4,116
  • 5
  • 38
  • 57