我有一些自定义列表视图适配器,在适配器中我使用 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;
}
}