Skip to content Skip to sidebar Skip to footer

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

Solution 1:

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

Create a model class as below:

publicclassModelDemo
{
   //your declarationboolean isClicked;

  publicvoidsetIsClicked(boolean value) {
            this.value = value ;
        }

publicbooleanisClicked() {
           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

publicvoidonBindViewHolder(final Holder holder, finalint position) {

          final ModelDemo modelDemo= arrayZipModel.get(position);

      //here write your code to inflate the data for button textif (modelDemo.isClicked())
               holder.yourButton.setBackgroundDrawable(ContextCompat.getDrawable(context, R.drawable.selected));
            else
                  holder.yourButton.setBackgroundDrawable(ContextCompat.getDrawable(context, R.drawable.unselected));




            holder.yourButton.setOnClickListener(newView.OnClickListener() {
                @OverridepublicvoidonClick(View v) {
                   if(modelDemo.isClicked())
                      modelDemo.setIsClicked(false)
                  else
                     modelDemo.setIsClicked(true)

                   notifyDataSetChanged();

                }
            });



        }

Post a Comment for "In Recycleview Onclick Item How To Change The Color Of Other Item"