为什么ViewPager不能与listview中的位图一起使用

时间:2012-10-23 07:45:34

标签: android-listview android-arrayadapter

我有一些自定义列表视图适配器,在适配器中我使用ViewHolder模式,它可以正常工作,但不适用于位图。

该代码是有效的,但是使用位图的工作不使用viewholder。我评论它进行测试,所以现在我看到,视图持有者无法使用位图。

如果有人知道为什么或有任何想法,请告诉我如何解决。

有我的代码:

public class FrontListAdapter extends ArrayAdapter<GoodObject> {

    private Context context;
    private AdapterLoadNotifier notifier;
    private List<GoodObject> list;
    public HashMap<String, Bitmap> avatars = new HashMap<String, Bitmap>();

    public FrontListAdapter(MainActivity activity, List<GoodObject> objects) {
        super(activity, R.layout.row_front_layout, objects);
        this.context = activity;
        this.list = objects;
        this.notifier = activity;
    }

    static class ViewHolder {
        static TextView shopTitle;
        static TextView time;
        static TextView description;
        static ImageView goodsImage;
        static ProgressBar loadingGoodsImageDialogue;
        static ImageView shopAvatar;
        static ImageView loadingAvatarHolder;
        static ProgressBar loadingAvatarDialogue;
        static TextView commentAuthor;
        static TextView commentText;
        static Button likeButton;
        static Button commentButton;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (convertView == null) {
            LayoutInflater inflater = LayoutInflater.from(context);
            convertView = inflater.inflate(R.layout.row_front_layout, null);

            viewHolder = new ViewHolder();
            viewHolder.shopTitle = (TextView) convertView.findViewById(R.id.row_front_layout_shop_title);
            viewHolder.time = (TextView) convertView.findViewById(R.id.row_front_layout_time);
            viewHolder.description= (TextView) convertView.findViewById(R.id.row_front_layout_description_label);
            viewHolder.goodsImage = (ImageView) convertView.findViewById(R.id.row_front_layout_good_image);
            viewHolder.loadingGoodsImageDialogue = (ProgressBar) convertView.findViewById(R.id.row_front_layout_good_image_progress);
            viewHolder.shopAvatar = (ImageView) convertView.findViewById(R.id.row_front_layout_logo);
            viewHolder.loadingAvatarDialogue = (ProgressBar) convertView.findViewById(R.id.row_front_layout_avatar_image_progress);
            viewHolder.loadingAvatarHolder = (ImageView) convertView.findViewById(R.id.row_front_layout_logo_back);
            viewHolder.commentAuthor = (TextView) convertView.findViewById(R.id.row_front_layout_first_comment_author);
            viewHolder.commentText = (TextView) convertView.findViewById(R.id.row_front_layout_first_comment_text);
            viewHolder.likeButton = (Button) convertView.findViewById(R.id.row_front_layout_like_button);
            viewHolder.commentButton = (Button) convertView.findViewById(R.id.row_front_layout_comment_button);

            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        GoodObject goodObject = list.get(position);
        String shopTitle = goodObject.getShopTitle();
        Bitmap avatar = goodObject.getAvatar();
        Bitmap image = goodObject.getGoods_logo();

        viewHolder.shopTitle.setText(shopTitle);
        viewHolder.time.setText(goodObject.getTime());
        viewHolder.description.setText(goodObject.getDescription());

        if (image != null) {
            //viewHolder.goodsImage.setImageBitmap(image);
            //viewHolder.loadingGoodsImageDialogue.setVisibility(View.GONE);
            ((ImageView) convertView.findViewById(R.id.row_front_layout_good_image)).setImageBitmap(image);
            convertView.findViewById(R.id.row_front_layout_good_image_progress).setVisibility(View.GONE);
        }
        if (avatar != null) {
            convertView.findViewById(R.id.row_front_layout_avatar_image_progress).setVisibility(View.GONE);
            convertView.findViewById(R.id.row_front_layout_logo_back).setVisibility(View.GONE);

            //viewHolder.loadingAvatarDialogue.setVisibility(View.GONE);
            //viewHolder.loadingAvatarHolder.setVisibility(View.GONE);
        }
        ((ImageView) convertView.findViewById(R.id.row_front_layout_logo)).setImageBitmap(avatars.get(shopTitle));

        //viewHolder.shopAvatar.setImageBitmap(avatars.get(shopTitle));

        viewHolder.commentAuthor.setText(goodObject.getLast_comment().getUser() + ":");
        viewHolder.commentText.setText(goodObject.getLast_comment().getComment());
        viewHolder.likeButton.setText(Integer.toString(goodObject.getLikesAmount()));
        viewHolder.commentButton.setText(Integer.toString(goodObject.getCommentsAmount()));

        if (position == list.size() - 2 && !notifier.isEnded()) {
            notifier.loadNextPage();
        }
        return convertView;
    }
}

1 个答案:

答案 0 :(得分:0)

对于修复bug,ViewHolder类中的字段不是静态的:

static class ViewHolder {
    TextView shopTitle;
    TextView time;
    TextView description;
    ImageView goodsImage;
    ProgressBar loadingGoodsImageDialogue;
    ImageView shopAvatar;
    ImageView loadingAvatarHolder;
    ProgressBar loadingAvatarDialogue;
    TextView commentAuthor;
    TextView commentText;
    Button likeButton;
    Button commentButton;
}

在这种情况下,所有工作都非常正确。