Bwes
Bwes

Reputation: 41

In recycleview onclick item how to change the color of other item

I am trying to create a quiz app. Could not figure out how to change the color of other item when one item in recycleview is clicked.When option 2 is clicked but the correct option is option 1 it should display as given below in picture. Solution please enter image description here

Upvotes: 0

Views: 878

Answers (1)

Neha Tyagi
Neha Tyagi

Reputation: 3821

Create two drawable files for selected and unselected buttons in your drawable folder.

Create a model class as below:

public class ModelDemo
{
   //your declaration

boolean isClicked;

  public void setIsClicked(boolean value) {
            this.value = value ;
        }

public boolean isClicked() {
           return value;
        }
}

//Now create an arraylist of type model like ArrayList<ModelDemo> , with other values you need to infalte your recyclerview. Set the isClicked to false initially. Inflate your recylerview as follows

 public void onBindViewHolder(final Holder holder, final int position) {

          final ModelDemo modelDemo= arrayZipModel.get(position);

      //here write your code to inflate the data for button text




     if (modelDemo.isClicked())
               holder.yourButton.setBackgroundDrawable(ContextCompat.getDrawable(context, R.drawable.selected));
            else
                  holder.yourButton.setBackgroundDrawable(ContextCompat.getDrawable(context, R.drawable.unselected));




            holder.yourButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                   if(modelDemo.isClicked())
                      modelDemo.setIsClicked(false)
                  else
                     modelDemo.setIsClicked(true)

                   notifyDataSetChanged();

                }
            });



        }

Upvotes: 1

Related Questions