Change image resource when clicking on ImageView inside the RecyclerView

不羁岁月 提交于 2019-12-23 20:48:08

问题


I'm trying to change my device's image resource inside the recyclerview at the specific position whenever i click at it (click at the image, not the item). I tried to put setOnClickListener() inside the onBindViewHolder() method but only the last item affected. here is my recycler view

here is my Adapter:

public class Device_RV_Adapter extends RecyclerView.Adapter<Device_View_Holder> {

List<Device_Data> list= Collections.emptyList();
Context context;

public Device_RV_Adapter(List<Device_Data> list,Context context){
    this.list=list;
    this.context = context;
}
@Override
public Device_View_Holder onCreateViewHolder(ViewGroup parent, int viewType) {
    View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.device_row_layout,parent,false);
    Device_View_Holder holder = new Device_View_Holder(v);
    return holder;
}

@Override
public void onBindViewHolder(final Device_View_Holder holder, final int position) {

    holder.device_id.setText(list.get(position).device_id);
    holder.home.setText(list.get(position).home_id);
    holder.room.setText(list.get(position).room);
    holder.current.setText(list.get(position).current);
    holder.switch_key.setImageResource(list.get(position).switch_key);
    holder.device.setTag(position);
    holder.device.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            int pos = (int)v.getTag();
            if(list.get(pos).flag==true){
                list.get(pos).flag = false;
            }
            else if(list.get(pos).flag==false){
                list.get(pos).flag = true;
            }
            notifyDataSetChanged();
        }

    });

    if(list.get(position).flag == true)
        holder.device.setImageResource(R.drawable.fan_off);
    else if(list.get(position).flag==false)
        holder.device.setImageResource(R.drawable.fan_on);
}

@Override
public int getItemCount() {
    return list.size();
}
@Override
public void onAttachedToRecyclerView(RecyclerView recyclerView){
    super.onAttachedToRecyclerView(recyclerView);
}
public void insert(int position,Device_Data device_data){
    list.add(position,device_data);
    notifyItemInserted(position);
}
public void remove(Device_Data device_data){
    int position = list.indexOf(device_data);
    list.remove(position);
    notifyItemRemoved(position);
}

My ViewHolder:

public class Device_View_Holder extends RecyclerView.ViewHolder{

CardView cv;
TextView device_id,home,room,current;
public static ImageView device, switch_key;
boolean flag;
public Device_View_Holder(View itemView) {
    super(itemView);
    cv=(CardView)itemView.findViewById(R.id.device_cv);
    device_id=(TextView)itemView.findViewById(R.id.device_id);
    home=(TextView)itemView.findViewById(R.id.home_id);
    room=(TextView)itemView.findViewById(R.id.room);
    current=(TextView)itemView.findViewById(R.id.current);
    device=(ImageView)itemView.findViewById(R.id.device_img);
    switch_key=(ImageView)itemView.findViewById(R.id.switch_key);
}

And my Data class:

public class Device_Data {
public String device_id,home_id,room,current;
public int device,switch_key;
boolean flag;
public Device_Data(String device_id, String home_id, String room, String current,int device,int switch_key,boolean flag){
    this.device_id=device_id;
    this.home_id=home_id;
    this.room=room;
    this.current=current;
    this.device=device;
    this.switch_key=switch_key;
    this.flag=flag;
}

When I click to the device's image, if it's on (flag=true), changed to "fan_off". If it's off, change to "fan_on". Just like a switch. Can anyone help me with this situation? Thanks in advance...


回答1:


First of all, there's no need of that for loop inside onBindViewHolder(), as this method is called for all the items in the list and you don't have to explicitly run loop for all the items again.

Replace this piece of code:

for(int i=0;i<list.size();i++){
        if(position==i){
                holder.device.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if(list.get(position).flag==true)
                            holder.device.setImageResource(R.drawable.fan_off);
                        else if(list.get(position).flag==false){
                            holder.device.setImageResource(R.drawable.fan_on);
                        }
                    }
                });
            }
    }

with this:

// here setTag is used to attach the position with each view being inflated.

holder.device.setTag(position);
holder.device.setOnClickListener(new View.OnClickListener() {

      @Override
      public void onClick(View v) {
        int pos = (int)v.getTag();
        if(list.get(pos).flag==true){
           list.get(pos).flag = false;
          }
        else{
           list.get(pos).flag = true;
          }
     notifyDataSetChanged();
      }

   });

 if(list.get(position).flag == true)
    holder.device.setImageResource(R.drawable.fan_off);
 else
    holder.device.setImageResource(R.drawable.fan_on);

we check here if the flag is true or false and when user clicks on button, change the values accordingly so that when notifyDataSetChanged() is called it populates the views with newly changed values of flags and hence make the relevant views selected or unselected.



来源:https://stackoverflow.com/questions/41108222/change-image-resource-when-clicking-on-imageview-inside-the-recyclerview

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