单击

时间:2016-06-22 05:36:19

标签: android uiimageview android-recyclerview

我想在回收站视图中更改已点击商品的相应图片。现在发生的事情是,如果我点击第2项图像正在更改列表中的随机项目位置

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

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

4 个答案:

答案 0 :(得分:9)

试试这个,

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

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

On BindHolder就像,

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

on Adapter写这个方法

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

}

在项目上点击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);

                }
            });

答案 1 :(得分:1)

更改单击位置处对象中的图像值 请致电notifyItemChanged(position)。同时处理适配器的Bindviewholder(ViewHolder holder,position)方法中的图像更改。

答案 2 :(得分:1)

onBindViewHolder中添加以下代码:

testImage.setTag(保持器);

然后,在onClick上添加此代码以更改图像:

MyRecyclerViewAdapter.DataObjectHolder  yourHolderObj = (MyRecyclerViewAdapter.DataObjectHolder)((ImageView)itemView.findViewById(R.id.logo)).getTag();

yourHolderObj.testImage.setImageResource(R.drawable.video_icon_fill);

答案 3 :(得分:1)

试试这个:

    public DateTime?  LastRequestedDate { get; set; }

    public string LastRequestedDateString => String.Format("{0: MMMM dd, yyyy}", LastRequestedDate);