gpt4 book ai didi

android - ListView 图像在滚动期间发生变化

转载 作者:太空狗 更新时间:2023-10-29 15:26:52 28 4
gpt4 key购买 nike

我尝试用动态图像制作 ListView ,使用 asynctask 下载图像并设置到 ListView 中。我的问题是向下滚动时图像会随机更改..

class ps1 extends ArrayAdapter<String> {
Context context;
String[] images1;
List mList;
String[] namearray;
String[] rating;

static class ViewHolder {
ImageView localImageView1;
ImageView localImageView2;
ImageView localImageView3;
}

ps1(Context paramContext, String[] paramArrayOfString1, String[] paramArrayOfString2, String[] paramArrayOfString3) {
super(paramContext, R.layout.list2, R.id.imageView1, paramArrayOfString1);
this.context = paramContext;
this.images1 = paramArrayOfString3;
this.namearray = paramArrayOfString1;
this.rating = paramArrayOfString2;
}

public View getView(int paramInt, View paramView, ViewGroup paramViewGroup) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(context.LAYOUT_INFLATER_SERVICE);

ViewHolder viewHolder = new ViewHolder();
if (paramView == null) {
paramView = inflater.inflate(R.layout.list2, paramViewGroup, false);

}


viewHolder.localImageView1 = (ImageView) paramView
.findViewById(R.id.imageView1);
viewHolder.localImageView2 = (ImageView) paramView
.findViewById(R.id.imageView2);
viewHolder.localImageView3 = (ImageView) paramView
.findViewById(R.id.imageView3);


viewHolder.localImageView1.setScaleType(ImageView.ScaleType.FIT_XY);
viewHolder.localImageView2.setScaleType(ImageView.ScaleType.FIT_XY);
viewHolder.localImageView3.setScaleType(ImageView.ScaleType.FIT_XY);

viewHolder.localImageView1.setTag(this.namearray[paramInt]);
new LoadImage().execute(viewHolder.localImageView1);
viewHolder.localImageView2.setTag(this.rating[paramInt]);
new LoadImage().execute(viewHolder.localImageView2);
viewHolder.localImageView3.setTag(this.images1[paramInt]);
new LoadImage().execute(viewHolder.localImageView3);


return paramView;
}
}

class LoadImage extends AsyncTask<Object, Void, Bitmap> {
private ImageView imv;

private Bitmap download_Image(String paramString) {
Bitmap localBitmap = null;
try {
Object localObject = null;
localBitmap = BitmapFactory
.decodeStream(((HttpURLConnection) new URL(paramString)
.openConnection()).getInputStream());
localObject = localBitmap;
if (localObject != null) {
return localBitmap;
}
} catch (Exception e) {

}
return localBitmap;
}

protected Bitmap doInBackground(Object... paramVarArgs) {
this.imv = ((ImageView) paramVarArgs[0]);
Log.d("fsdf", (String) this.imv.getTag());
return download_Image((String) this.imv.getTag());
}

protected void onPostExecute(Bitmap paramBitmap) {
this.imv.setImageBitmap(paramBitmap);
}
}

最佳答案

我也有过同样的经历。我也在寻找合适的解决方案。据我搜索,我了解到 ListView 会在向下滚动时清除先前的 View ,并在您向后滚动时重新加载它。因此,在上下滚动时,您的图像可能会重新循环和错位。 (我也在等待正确的解决方案)。

但我已经使用 SmartImageView 解决了它,这是一个直接下载图像并将其设置到 ImageView 的库。它将在缓存中维护图像,因此您可以获得正确的图像。

相比之下,这也更快。

关于android - ListView 图像在滚动期间发生变化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21155162/

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