Setting image resourse by obtaining tag position for an imageview android

霸气de小男生 提交于 2019-12-08 19:55:29

Proper way to do this is, You have to keep the state of the heart button stored in the model(POJO) which is passed to custom adapter. e.g.

class ModelListItem{
public static final int HEART=1,BROKEN_HEART=2;

int heartButtonState;

}

Now in onClick() of heart button, get that object from adapter using position,cosidering you have already figured it out on how to preserve position from heart button

ModelListItem item = (ModelListItem)adapter.getItem(position)

Change the state of heart button;

item.setHeartButtonState(ModelListItem.BROKEN_HEART);
   adapter.notifyDatasetChanged();

You already know below explaination but just in case To work this properly,in your getView methode of adapter you need to put the check on heartButtonState(); and use appropriate image resource.

getView(BOILERPLATE){
 BOILERPLATE

 switch(item.getheartButtonState()){
 case ModelItemList.HEART:
   heartbutton.setImageResource(heart_image);
 break;
 case ModelItemList.BROKEN_HEART:
   heartbutton.setImageResource(broken_heart_image);
 break;  
 } 

I made a custom click listener and updated the like in the setter getter.But this works only when the view has been moved out of the view (i think it is the scrapeview)

The Setter Getter Class

public class DemoData {

    int background;
    boolean liked;

    public DemoData(int background) {
        this.background = background;
    }

    public int getBackground() {
        return background;
    }

//    public void setBackground(int background) {
//        this.background = background;
//    }

    public boolean isLiked() {
        return liked;
    }

    public void setLiked(boolean liked) {
        this.liked = liked;
    }
}

The onBindViewHolder function of the recycler view

@Override
    public void onBindViewHolder(ViewHolder holder, int position) {


        background = (ImageView) holder.view.findViewById(R.id.image);
        layout = (LinearLayout) holder.view.findViewById(R.id.layout);

        delete = (ImageView) layout.findViewById(R.id.delete);
        lock = (ImageView) layout.findViewById(R.id.lock);


        delete.setTag("delete_"+position);
        lock.setTag("lock_"+position);

        if(Constants.demoDatas.get(position).isLiked()){
            delete.setImageResource(R.drawable.ic_launcher);
        }
        else{
            delete.setImageResource(android.R.drawable.ic_delete);
        }

        delete.setOnClickListener(new CustomClickListener(position));
        lock.setOnClickListener(new CustomClickListener(position));



    }

The custom click listener is as below

public class CustomClickListener implements View.OnClickListener {

    int position;


    public CustomClickListener(int position) {

        this.position = position;
    }

    @Override
    public void onClick(View v) {

        String tag = (String) v.getTag();
        String identifier[] = tag.split("_");

        // this line saves my state in the Setter Getter Class
        Constants.demoDatas.get(position).setLiked(true);

    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!