Listview 适配器将 ImageButton 背景资源设置为回收时的错误项目

Listview adapter setting ImageButton background resource to wrong item on recycle

在创建我的适配器时,我传递了多个参数,其中一个是“1”或“0”,具体取决于某个 post 是否受到喜欢。当首次通过适配器填充 ListView 时,ImageButton 背景资源会根据收藏夹变量是 0 还是 1 进行正确设置。但是,当用户向下滚动然后向上滚动时,填充了错误的列表视图项,通常倍重复。

我将包含一些图片作为参考:

ListView when first loaded

ListView after scrolling to bottom and back up

public class TestAdapter extends ArrayAdapter<postFeedPopulator>{
public TestAdapter(Context context, ArrayList<postFeedPopulator> posts){
    super(context, R.layout.listview_postfeedfragment, posts);
}

View customView;
ViewHolder holder;
@Override
public View getView(int position, View customView, ViewGroup parent){
    final postFeedPopulator post = getItem(position);
    if(customView == null){
        LayoutInflater inflater = LayoutInflater.from(getContext());
        customView = inflater.inflate(R.layout.listview_postfeedfragment, parent, false);
        holder = new ViewHolder();

        holder.favoriteButton = (ImageButton) customView.findViewById(R.id.favoriteButton);

        customView.setTag(holder);
    } else {
        holder = (ViewHolder) customView.getTag();
        //holder.favoriteButton.setTag("0");
    }

    if (post.favorited.equals("0")){
        holder.favoriteButton.setTag("0");
    } else if(post.favorited.equals("1")){
        holder.favoriteButton.setTag("1");
        holder.favoriteButton.setBackgroundResource(R.drawable.favorite_2);
    }

    return customView;
}

static class ViewHolder{
    ImageButton favoriteButton;
}

}

我四处寻找解决这个问题的办法,但一直没能找到。我是新人 Android,非常感谢任何帮助。

当 post.favorited 1 和 0 都不同时请覆盖大小写

if (post.favorited.equals("0")){
    holder.favoriteButton.setTag("0");
    holder.favoriteButton.setBackgroundResource(R.drawable.favorite_1);
} else if(post.favorited.equals("1")){
    holder.favoriteButton.setTag("1");
    holder.favoriteButton.setBackgroundResource(R.drawable.favorite_2);
} else {
    holder.favoriteButton.setTag(" ? ")
    holder.favoriteButton.setBackgroundResource(R.drawable.favorite_2);
}

解决方案是将 ImageButton 背景资源设置为初始状态(未单击),在本例中为 favorite_1,此时 customView 已经膨胀。如果需要,if/else 语句将重新更改后台资源。如此简单的解决方案,但我把它写在这里才弄明白。

public class TestAdapter extends ArrayAdapter<postFeedPopulator>{
public TestAdapter(Context context, ArrayList<postFeedPopulator> posts){
    super(context, R.layout.listview_postfeedfragment, posts);
}

View customView;
ViewHolder holder;
@Override
public View getView(int position, View customView, ViewGroup parent){
    final postFeedPopulator post = getItem(position);
    if(customView == null){
        LayoutInflater inflater = LayoutInflater.from(getContext());
        customView = inflater.inflate(R.layout.listview_postfeedfragment, parent, false);
        holder = new ViewHolder();

        holder.favoriteButton = (ImageButton) customView.findViewById(R.id.favoriteButton);

        customView.setTag(holder);
    } else {
        holder = (ViewHolder) customView.getTag();
        holder.favoriteButton.setBackgroundResource(R.drawable.favorite_1);
        //holder.favoriteButton.setTag("0");
    }

    if (post.favorited.equals("0")){
        holder.favoriteButton.setTag("0");
    } else if(qlustr.favorited.equals("1")){
        holder.favoriteButton.setTag("1");
        holder.favoriteButton.setBackgroundResource(R.drawable.favorite_2);
    }

    return customView;
}

static class ViewHolder{
    ImageButton favoriteButton;
}

}