单击后如何在回收站视图中更改特定图像

How to change particular image in recycler view after clicking(单击后如何在回收站视图中更改特定图像)

本文介绍了单击后如何在回收站视图中更改特定图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在回收站视图中更改单击项目的相应图像.现在发生的情况是,如果我单击第 2 项图像,则列表中的随机项位置会发生变化

I want to change respective image of clicked item in recycler view. Now whats happening is if i click on item 2 image is changing at random item position in the list

public class MyRecyclerViewAdapter extends RecyclerView.Adapter<MyRecyclerViewAdapter.DataObjectHolder> {

private static String LOG_TAG = "MyRecyclerViewAdapter";
private ArrayList<CRMbean> mDataset;
private static MyClickListener myClickListener;
static Context mContext;
private List<String> mtList ;


public MyRecyclerViewAdapter(List<String> filteredList, CRMactivity crMactivity) {
    mtList = filteredList;

}

public static class DataObjectHolder extends RecyclerView.ViewHolder
        implements View
        .OnClickListener {
    public static ImageView testImage;
    TextView name;
    TextView regId;
    TextView roomNo;

    public DataObjectHolder(View itemView) {
        super(itemView);
        name = (TextView) itemView.findViewById(R.id.name);
        regId = (TextView)itemView.findViewById(R.id.reg_id);
        roomNo = (TextView)itemView.findViewById(R.id.roomno);
         testImage = (ImageView)itemView.findViewById(R.id.logo);


        Log.i(LOG_TAG, "Adding Listener");
        itemView.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        myClickListener.onItemClick(getAdapterPosition(), v);

    }
}
public void setOnItemClickListener(MyClickListener myClickListener) {
    MyRecyclerViewAdapter.myClickListener = myClickListener;
}
public MyRecyclerViewAdapter(ArrayList<CRMbean> myDataset) {
    mDataset = myDataset;
}





@Override
public MyRecyclerViewAdapter.DataObjectHolder onCreateViewHolder(ViewGroup parent, int viewType) {

    View view = LayoutInflater.from(parent.getContext())
            .inflate(R.layout.card_view_row, parent, false);

    DataObjectHolder dataObjectHolder = new DataObjectHolder(view);
    return dataObjectHolder;
}

@Override
public void onBindViewHolder(MyRecyclerViewAdapter.DataObjectHolder holder, int position) {
    holder.name.setText(mDataset.get(position).getName());
    holder.regId.setText(mDataset.get(position).getRegId());
    holder.roomNo.setText(mDataset.get(position).getRoomNo());

}

public void addItem(CRMbean dataObj, int index) {
    mDataset.add(index, dataObj);
    notifyItemInserted(index);
}

public void deleteItem(int index) {
    mDataset.remove(index);
    notifyItemRemoved(index);
}

@Override
public int getItemCount() {
    return mDataset.size();
}


public interface MyClickListener {
    void onItemClick(int position, View v);
}

}

以上是recyclerview的adpater

the above is for adpater of recyclerview

 ((MyRecyclerViewAdapter) mAdapter).setOnItemClickListener
            (new MyRecyclerViewAdapter.MyClickListener() {
                @Override
                public void onItemClick(int position, View v) {
                    Log.i(LOG_TAG, " Clicked on Item " + position);

                    MyRecyclerViewAdapter.DataObjectHolder.testImage.setImageResource(R.drawable.video_icon_fill);
                    //getInformation(position);

                }
            });

以上代码用于 OnclickListener

the above code is for OnclickListener

推荐答案

试试这个,

class CRMbean{
   public boolean isImageChanged;
  public boolean isImageChanged() {
        return isImageChanged;
    }

    public void setImageChanged(boolean imageChanged) {
        isImageChanged = imageChanged;
    }
}

在 BindHolder 上这样做,

On BindHolder do it like,

@Override
public void onBindViewHolder(MyRecyclerViewAdapter.DataObjectHolder holder, int position) {
    holder.name.setText(mDataset.get(position).getName());
    holder.regId.setText(mDataset.get(position).getRegId());
    holder.roomNo.setText(mDataset.get(position).getRoomNo());
if(mDataset.get(position).isImageChanged()){
    holder.testImage.setImageResource(R.drawable.video_icon_fill);
}else{
     holder.testImage.setImageResource("You Default Image");
}
}

在Adapter上写这个方法

on Adapter write this method

public void changeImage(int index) {
 mDataset.get(position).setImageChanged(true);
   notifyItemChanged(index)

}

在项目上点击 Recyleview

On Item click Of Recyleview

 (MyRecyclerViewAdapter) mAdapter).setOnItemClickListener
            (new MyRecyclerViewAdapter.MyClickListener() {
                @Override
                public void onItemClick(int position, View v) {
                    Log.i(LOG_TAG, " Clicked on Item " + position);

               MyRecyclerViewAdapter.changeImage(position);

                }
            });

这篇关于单击后如何在回收站视图中更改特定图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:单击后如何在回收站视图中更改特定图像

基础教程推荐