更新gridview时如何避免闪烁?

Vig*_*esh 7 android android-imageview android-gridview

我有一个gridview.我显示来自10个图像阵列的图像.1分钟后我又添加了5张图片.要更新gridview,我使用以下代码.

aImgAdapterL.notifyDataSetChanged();  
Run Code Online (Sandbox Code Playgroud)

aImgAdapterL是我的ImgaeAdapter.新图像正在显示.
我的问题是在更新网格视图时,在图像更新期间发生一次闪烁或闪烁.是否有可能隐藏闪烁?

kal*_*lvn 7

我有同样的问题,并能够像这样解决它:

事实上,这是由于我ListAdapter在整个列表刷新时没有正确管理的情况.如果是这样,您要做的是保持已经在屏幕上显示的项目.

为此,在getView获得回收物品时,在适配器的方法中,您必须检查它是否已经是您想要显示的物品.如果是这种情况,请直接退回.

@Override
public View getView(int position, View convertView, ViewGroup container) {
    ImageView imageView;
    String src = this.getItem(position).getSrcThumbnail();
    if (convertView == null) {
        imageView = new ImageView(getContext());
        imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);

        int height = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 120, getResources().getDisplayMetrics());

        imageView.setLayoutParams(new GridView.LayoutParams(GridView.LayoutParams.MATCH_PARENT, height));

    } else {
        imageView = (ImageView) convertView;

        // When you get a recycled item, check if it's not already the one you want to display.
        String newSrc = (String) imageView.getTag();

        if(newSrc.equals(src)){
            // If so, return it directly.
            return imageView;
        }
    }

    loadBitmap(this.getItem(position).getSrcThumbnail(), imageView);

    // Here you set the unique tag that allow to identify this item.
    imageView.setTag(src);

    return imageView;
}
Run Code Online (Sandbox Code Playgroud)


SK9*_*SK9 5

如果您的适配器具有稳定的ID,则覆盖hasStableIds以返回true.

这里 Grep android.GridView源代码,它允许网格视图重用相同数据的视图,而不是完全重绘.这可能有助于OP的情况,因为默认返回.hasStableIdsfalse


小智 0

使用nostra13 的通用图像加载器来显示图像,在显示时设置一些动画。

...DisplayImageOptions.displayer(FadeInBitmapDisplayer)...
Run Code Online (Sandbox Code Playgroud)