How to change particular image in recycler view after clicking

前端 未结 4 1965
别那么骄傲
别那么骄傲 2021-02-03 16:04

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

<         


        
相关标签:
4条回答
  • 2021-02-03 16:27

    in onBindViewHolder add following code:

    testImage.setTag(holder);

    then, in onClick add this code to change image:

    MyRecyclerViewAdapter.DataObjectHolder  yourHolderObj = (MyRecyclerViewAdapter.DataObjectHolder)((ImageView)itemView.findViewById(R.id.logo)).getTag();
    
    yourHolderObj.testImage.setImageResource(R.drawable.video_icon_fill);
    
    0 讨论(0)
  • 2021-02-03 16:29

    try this,

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

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

    on Adapter write this method

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

    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);
    
                    }
                });
    
    0 讨论(0)
  • 2021-02-03 16:36

    Change the image value in the object at the clicked position and Call notifyItemChanged(position).Also handle the image change in Bindviewholder(ViewHolder holder,position) method of the Adapter.

    0 讨论(0)
  • 2021-02-03 16:36

    Try this:

     @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());
    holder.testImage = (ImageView)itemView.findViewById(R.id.log)
    holder.testImage.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v) {
             name.setImageResource(Anything you want);
    
    
    }
    
    0 讨论(0)
提交回复
热议问题