繁体   English   中英

Android:当已经在列表视图中加载Imageview时,不要重新加载它

[英]Android : Do not reload Imageview when it has been already loaded in listview

我想对此行为做一些解释:

我创建了一个列表视图。 列表视图的每个项目都有一个imageview,其中填充了来自URL的图像。

当我向下滚动到列表时,当出现该项目时会加载每个图像=>确定

但是,当我向上滚动时,先前已加载的图像将再次下载。

如何停止这种行为?

这是我的下载器的代码:

public class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
        ImageView bmImage;

        public DownloadImageTask(ImageView bmImage) {
            this.bmImage = bmImage;
        }

        protected Bitmap doInBackground(String... urls) {
            String urldisplay = urls[0];
            Bitmap mIcon11 = null;
            try {
                InputStream in = new java.net.URL(urldisplay).openStream();
                mIcon11 = BitmapFactory.decodeStream(in);
            } catch (Exception e) {
                Log.e("Error", e.getMessage());
                e.printStackTrace();
            }
            return mIcon11;
        }

        protected void onPostExecute(Bitmap result) {
            bmImage.setImageBitmap(result);
        }
    }

这是我的适配器的一段代码:

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

        Activity activity = (Activity) getContext();

        LayoutInflater inflater = activity.getLayoutInflater();

        View rowView;
        ArticleHome article = getItem(position);


        if (position == 0) {
            rowView = inflater.inflate(R.layout.item_ligne_home_premier, null);
            new DownloadImageTask((ImageView) rowView.findViewById(R.id.imgimg)).execute(article.getImage());
            TextView textView = (TextView) rowView.findViewById(R.id.titlenew);
            textView.setText(article.getTitle());
            textView.setTypeface(faceLight);
        }
        else {
            rowView = inflater.inflate(R.layout.item_ligne_home, null);
            new DownloadImageTask((ImageView) rowView.findViewById(R.id.imgimg)).execute(article.getImage());
            TextView title = (TextView) rowView.findViewById(R.id.titlearticleothers);
            title.setText(article.getTitle());
            title.setTypeface(faceBold);
            TextView desc = (TextView) rowView.findViewById(R.id.descriptionarticleothers);
            // Add test si < a une certaine longeuryr
            desc.setText(article.getDescription().substring(0, 100));
            desc.setTypeface(faceLight);
        }


//      img.setImageAlpha(1680);
//      img.setImageAlpha(1880); // Pasmal plus haut plus foncé


        return rowView;

    } 

实际执行此操作的方法是缓存下载的每个图像,并使用Cache中的图像更新ListView项,并且仅当图像不在缓存中时才下载。

这样可以避免下载。

检查Pramod J George的答案,尤其是ImageLoader类。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM