gpt4 book ai didi

android - 图像在滚动 ListView 上刷新

转载 作者:行者123 更新时间:2023-11-29 20:21:26 24 4
gpt4 key购买 nike

我使用的 ListView 有 3 个字段,1 个 ImageView 和 2 个 TextView 。我正在解析 ListView 的 json 数据。当我滚动 ListView 时, ImageView 中的图像发生变化,然后当我滚动回顶部时,它再次发生变化。我正在显示来自 URL 的图像。

下面是部分代码:

public ArrayList<HashMap> dataToList(List<CatalogTopCategory> detailsList) {
list = new ArrayList<HashMap>();

for (CatalogTopCategory catalogTopCategory : detailsList) {
HashMap temp = new HashMap();
temp.put(FIRST_COLUMN, catalogTopCategory.getCategoryName());
temp.put(SECOND_COLUMN, catalogTopCategory.getCategoryDesc());
temp.put(THIRD_COLUMN, catalogTopCategory.getCategoryBannner());
temp.put(FIFTH_COLUMN, catalogTopCategory.getCategoryThumbnail());
temp.put(FORTH_COLUMN, catalogTopCategory.getCategoryId());
list.add(temp);
}
return list;
}



final ArrayList<HashMap> hashMaps = dataToList(storeCatList);
catalogTopAdapter = new CatalogTopAdapter(StoreLanding.this,
hashMaps, getApplicationContext());
listView.setAdapter(catalogTopAdapter);

适配器类代码如下:

 public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
LayoutInflater inflater = activity.getLayoutInflater();
if (convertView == null)
{
convertView = inflater.inflate(R.layout.catalog_listview, null);
holder = new ViewHolder();
holder.imageView = (ImageView)convertView.findViewById(R.id.imageView2);
holder.txtFirst = (TextView) convertView.findViewById(R.id.FirstText);
holder.txtSecond = (TextView) convertView.findViewById(R.id.SecondText);
convertView.setTag(holder);
}
else
{
holder = (ViewHolder) convertView.getTag();
}
Typeface type = Typeface.createFromAsset(mContext.getAssets(),"fonts/FeedSpeedFonts.otf");
holder.txtFirst.setTypeface(type);
holder.txtSecond.setTypeface(type);
HashMap map = list.get(position);
new LoadImage(holder.imageView).execute(String.valueOf(map.get(FIFTH_COLUMN)));
holder.txtFirst.setText((CharSequence) map.get(FIRST_COLUMN));
holder.txtSecond.setText((CharSequence) map.get(SECOND_COLUMN));
convertView.setTag(holder);
return convertView;
}

加载图片:

private class LoadImage extends AsyncTask<String, Void, Bitmap> {
ImageView bmImage;
public LoadImage(ImageView bmImage) {
this.bmImage = bmImage;
}

protected Bitmap doInBackground(String... urls) {
String urldisplay = urls[0];
Bitmap mIcon = null;
try {
mIcon = BitmapFactory.decodeResource(mContext.getResources(), R.drawable.default_image);
InputStream in = new java.net.URL(urldisplay).openStream();
mIcon = BitmapFactory.decodeStream(in);
} catch (Exception e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
return mIcon;
}
protected void onPostExecute(Bitmap result) {
bmImage.setImageBitmap(Bitmap.createScaledBitmap(result, 100, 100,
false));
}
}

最佳答案

new LoadImage(holder.imageView).execute(String.valueOf(map.get(FIFTH_COLUMN))); 导致刷新图像时出现问题。刷新 View 时,您每次都会启动 asynctask。当您的 View 将被重用时,您的异步任务会在位置上显示不良图像,因此您的异步任务会再次启动并显示良好的图像。您不能以这种方式设置图像。 Android 社​​区使用

  • 毕加索
  • 通用图像加载器
  • Volley 图像加载器
  • 滑行

关于android - 图像在滚动 ListView 上刷新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33099866/

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