android – 如何在单击后更改回收器视图中的特定图像

android – 如何在单击后更改回收器视图中的特定图像,第1张

概述我想在Recycler视图更改单击项目的相应图像.现在发生的事情是,如果我点击第2项图像正在改变列表中的随机项目位置 public class MyRecyclerViewAdapter extends RecyclerView.Adapter<MyRecyclerViewAdapter.DataObjectHolder> {private static String LOG_TAG = " 我想在Recycler视图中更改单击项目的相应图像.现在发生的事情是,如果我点击第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;}@OverrIDepublic 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;}@OverrIDepublic 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);}@OverrIDepublic 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

解决方法 试试这个,

class CRMbean{   public boolean isImageChanged;  public boolean isImageChanged() {        return isImageChanged;    }    public voID setimageChanged(boolean imageChanged) {        isImageChanged = imageChanged;    }}

在BindHolder上做它,

@OverrIDepublic 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上写这个方法

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," Clicked on Item " + position);               MyRecyclerVIEwAdapter.changeImage(position);                }            });
总结

以上是内存溢出为你收集整理的android – 如何在单击后更改回收器视图中的特定图像全部内容,希望文章能够帮你解决android – 如何在单击后更改回收器视图中的特定图像所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1122747.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-29
下一篇 2022-05-29

发表评论

登录后才能评论

评论列表(0条)

保存