其他分享
首页 > 其他分享> > android – 如何在更新gridview时避免闪烁?

android – 如何在更新gridview时避免闪烁?

作者:互联网

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

aImgAdapterL.notifyDataSetChanged();  

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

解决方法:

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

事实上,这是由于我的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;
}

标签:android-gridview,android,android-imageview
来源: https://codeday.me/bug/20191006/1862587.html