Listview适配器设置ImageButton后台资源到回收的错误项目

时间:2015-11-30 03:22:47

标签: android android-listview android-viewholder

创建我的适配器时,我传递多个参数,一个是“1”或“0”,基于是否喜欢特定的帖子。首次通过适配器填充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新手,非常感谢任何帮助。

2 个答案:

答案 0 :(得分:0)

请在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);
}

答案 1 :(得分:0)

解决方案是将customButton背景资源设置为初始状态(未单击),在本例中为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;
}

}