0

I have created a horizontal recyclerview list. below is the image I have attached. problem is that I have to change bulb state when everytime user clicked that row from off state to on state and on state to off state. How do I implement this logic? please help me to find out the solution. i want to change light color according to user clicks, if he clicks on then it should change its color for that row and vise versa

horizontal recyclerview

public void onClick(View view, int position) {
            //toast("clicked" + position );
            if (!lightClicked){
                ImageView lightPopUp = view.findViewById(R.id.row_light_thumbnail);
                DrawableCompat.setTint(lightPopUp.getDrawable().mutate(), ContextCompat.getColor(getContext(), R.color.white));
                //toast("light on");
                lightClicked = true;
            }else {
                ImageView lightPopUp = view.findViewById(R.id.row_light_thumbnail);
                //toast("light off");
                DrawableCompat.setTint(lightPopUp.getDrawable().mutate(), ContextCompat.getColor(getContext(), R.color.colorAccent));
                lightClicked = false;
            }
            //View view1 = mLayoutManager.findViewByPosition(position);


        }

2 Answers2

0

Dont directly do this change as the recycler view's cells are reused and it wont work as expected, so instead apply the change in the list you are using. You can add a boolean variable in the model class of the list you use to populate the recycler view, and than on its click you can change the boolean's value and call notifydatasetchange on the adapter, and in bind view you should keep an If else condition based on that boolean for the Bulb's image ie. if true set one image if false set another

Max
  • 469
  • 4
  • 14
0

declare this :

int selectedPosition=-1;

inside onBindViewHolder:

public void onBindViewHolder(FiltersAdapter.ViewHolder holder, int position) {
if(selectedPosition==position)
  holder.itemView.setImageResource(R.drawable.higlihgt_image);
else
   holder.itemView.setImageResource(R.drawable.normal_image);

holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                selectedPosition=position;
                notifyDataSetChanged();

            }
        });
}