在RecyclerView中单击ImageView时更改图像资源

时间:2016-12-12 19:35:10

标签: java android android-recyclerview recycler-adapter

我正在尝试在每次点击它时在特定位置更改我的设备的图像资源(点击图片,而不是项目)。我试图将setOnClickListener()放在onBindViewHolder()方法中,但只有最后一项受影响。 here is my recycler view

这是我的适配器:

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

我的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);
}

我的数据类:

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;
}

当我点击设备的图像时,如果它处于打开状态(flag = true),则更改为“fan_off”。如果它关闭,请更改为“fan_on”。就像一个开关。任何人都可以帮我解决这个问题吗?提前谢谢......

1 个答案:

答案 0 :(得分:0)

首先,onBindViewHolder()内的循环没有必要,因为这个方法是为列表中的所有项调用的,你不必显式运行循环所有的项目再次。

替换这段代码:

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

用这个:

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

我们在这里检查标志是真还是假,当用户点击按钮时,相应地更改值,以便在调用notifyDataSetChanged()时,使用新更改的标志值填充视图,从而选择相关视图或未选择的。