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

泪湿孤枕 提交于 2020-01-06 06:41:12

问题


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


回答1:


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();

                }
            });



        }


来源:https://stackoverflow.com/questions/44798421/in-recycleview-onclick-item-how-to-change-the-color-of-other-item

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!