gpt4 book ai didi

android - ArrayAdater getView "caching"转换 View

转载 作者:行者123 更新时间:2023-11-29 17:50:03 25 4
gpt4 key购买 nike

在我的自定义适配器 getView 方法中“缓存”我的 convertView 时遇到问题。当使用像

这样的“缓存”时
if (convertView == null) ...

我的 verifiedView 有时会显示,即使用户未经过验证。当删除 if 结构并仅使用 row = convertView; 时,一切都按预期工作。知道如何解决这个问题吗?

@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row;

if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService( Context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(resource, parent, false);
} else {
row = convertView;
}

UserProfile user = users.get(position);
// get views
TextView usernameView = (TextView) row.findViewById(R.id.username);
ImageView avatarView = (ImageView) row.findViewById(R.id.avatar);
ImageView verifiedView = (ImageView) row.findViewById(R.id.verified);

// check if user has an custom avatar
if (!user.getAvatar().isEmpty()) {
// now check if we already downloaded the avatar
InputStream is = AvatarHandler.getAvatar(this.context, user.getID());
if (is != null) {
Bitmap bmp = BitmapFactory.decodeStream(is);
System.out.println("Avatar already downloaded.");
avatarView.setImageBitmap(bmp);
}
}
else {
// set default avatar
avatarView.setImageResource(R.drawable.avatar);
}

// set username
usernameView.setText(user.getName());

// set verified
if (user.isVerified())
verifiedView.setVisibility(View.VISIBLE);

return row;
}

最佳答案

当用户未验证时,您应该隐藏verifiedView:

// set verified
if (user.isVerified()) {
verifiedView.setVisibility(View.VISIBLE);
} else {
verifiedView.setVisibility(View.GONE);
}

关于android - ArrayAdater getView "caching"转换 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23520218/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com