gpt4 book ai didi

android Listview 图片加载

转载 作者:搜寻专家 更新时间:2023-11-01 09:15:29 27 4
gpt4 key购买 nike

嗨,我有一个包含图像和 TextView 的自定义 ListView ,我在适配器的 ListView 的 getview 中使用处理程序来加载图像,当所有图像都加载后,然后我改变手机的方向图像再次开始下载,我想阻止这个,即当所有图像都加载时,当方向改变时它不起作用。

public View getView(final int position, View convertView, ViewGroup parent) {
final String imageurl = imageUrls.get(position);
LayoutInflater inflate = LayoutInflater.from(context);
View v = inflate.inflate(rowResId, parent, false);
final ImageView image = (ImageView) v.findViewById(R.id.image);
final ProgressBar progress = (ProgressBar) v
.findViewById(R.id.progress);

image.setVisibility(View.GONE);
progress.setVisibility(View.VISIBLE);
final Handler handler = new Handler() {
@Override
public void handleMessage(Message message) {
System.out.println("Set image at" + position
+ " Image Name :: " + imageurl);
progress.setVisibility(View.GONE);
image.setImageBitmap((Bitmap) message.obj);
image.setVisibility(View.VISIBLE);
}
};

Thread thread = new Thread() {
@Override
public void run() {
Bitmap drawable = LoadImageFromWebOperations(imageurl);
Message message = handler.obtainMessage(1, drawable);
handler.sendMessage(message);

}
};
thread.start();
return v;
}

最佳答案

您可以使用位图图像列表。首先将列表中的所有位图设置为空。并在 getView 方法中检查 Bitmap 列表位置的 Bitmap 是否为 null。如果为 null,则显示进度条并下载图像并将图像保存在列表中。

例如图像列表名称是图像。那么getView方法中的代码将如下所示:

public View getView(final int position, View convertView, ViewGroup parent) {
final String imageurl = imageUrls.get(position);
LayoutInflater inflate = LayoutInflater.from(context);
View v = inflate.inflate(rowResId, parent, false);
final ImageView image = (ImageView) v.findViewById(R.id.image);
final ProgressBar progress = (ProgressBar) v
.findViewById(R.id.progress);
if(images.get(position) != null) {
progress.setVisibility(View.GONE);
image.setImageBitmap(images.get(position));
image.setVisibility(View.VISIBLE);
} else {
image.setVisibility(View.GONE);
progress.setVisibility(View.VISIBLE);
final Handler handler = new Handler() {
@Override
public void handleMessage(Message message) {
System.out.println("Set image at" + position
+ " Image Name :: " + imageurl);
progress.setVisibility(View.GONE);
image.setImageBitmap((Bitmap) message.obj);
image.setVisibility(View.VISIBLE);
}
};

Thread thread = new Thread() {
@Override
public void run() {
Bitmap drawable = LoadImageFromWebOperations(imageurl);
images.add(position, drawable);
Message message = handler.obtainMessage(1, drawable);
handler.sendMessage(message);
}
};
thread.start();
}

return v;
}

希望您已经理解我的意思。 :)

关于android Listview 图片加载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5000431/

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