ListView中的图像闪烁

时间:2015-05-19 07:49:40

标签: android listview flicker

我正在尝试将图片下载到网址列表视图中。当我滚动或列表填充图像时闪烁并且不在右侧..

这是我的适配器代码:

public class RecipesAdapter extends ArrayAdapter<XmlDataCollected> {
    // View lookup cache
    private static class ViewHolder {
        static ImageView img;
        TextView name;
        TextView info;
        ImageDownloader controller;
    }

    public RecipesAdapter(Context context, List<XmlDataCollected> recipes) {
        super(context, R.layout.fragment_search_result, recipes);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // Get the data item for this position
        XmlDataCollected recipe = getItem(position);
        // Check if an existing view is being reused, otherwise inflate the view
        ViewHolder viewHolder; // view lookup cache stored in tag
        if (convertView == null) {
            viewHolder = new ViewHolder();
            LayoutInflater inflater = LayoutInflater.from(getContext());
            convertView = inflater.inflate(R.layout.fragment_search_result, parent, false);
            viewHolder.name = (TextView) convertView.findViewById(R.id.label);
            viewHolder.info = (TextView) convertView.findViewById(R.id.info);
            ViewHolder.img = (ImageView) convertView.findViewById(R.id.icon);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
            viewHolder.img.setImageBitmap(null);
        }

        if (viewHolder.controller != null) {
            viewHolder.controller.cancel(false);
        }

        viewHolder.controller = new ImageDownloader(viewHolder.img);

        if (viewHolder.controller != null) {
            viewHolder.controller.execute(recipe.getImgUrl());
        }

        // Populate the data into the template view using the data object
        viewHolder.name.setText(recipe.getRecipeName());
        if(recipe.getType() != null) {
            viewHolder.info.setText(recipe.getType());
        }
        else {
            viewHolder.info.setText("Unknown category");
        }

        //new ImageDownloader(viewHolder.img).execute(recipe.getImgUrl());
        // Return the completed view to render on screen
        return convertView;
    }

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

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

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

        }

        protected void onPostExecute(Bitmap result) {
            if(result != null) {
                bmImage.setImageBitmap(result);
            }
            else{
                bmImage.setImageResource(R.drawable.ic_launcher);
            }
        }
    }
}

3 个答案:

答案 0 :(得分:1)

我也有这个问题。在你的getView()方法中添加以下代码:

if (viewHolder.controller != null) {
    viewHolder.controller.cancel(false);
}

viewHolder.controller = new ImageDownloader(viewHolder.img);

if (viewHolder.controller != null) {
    viewHolder.controller.execute(recipe.getImgUrl());
}

答案 1 :(得分:0)

列表视图中的

xml
添加这个

android:cacheColorHint="#00000000"

或代码

 listView.setCacheColorHint(Color.TRANSPARENT);

并删除

viewHolder.img.setImageBitmap(null);

来自else条件

答案 2 :(得分:0)

除了上述建议之外,我还有一些其他建议可能有所帮助。

你必须使你的getView()方法更加优化,因为它会被调用进入可见区域的每一行。将if / else条件更改为方法调用也可能有所帮助。

首先,我建议您查看此网站:

http://leftshift.io/6-ways-to-make-your-lists-scroll-faster-than-the-wind

如果使用wrap_content,更改listview的高度以匹配包装内容中的父级也会对优化产生巨大影响。您也可以尝试将其设为0dp。它会产生相同的结果。

如果您需要其他信息,请查看此主题以获取“match_parent”优化。

Why is 0dp considered a performance enhancement?