RecyclerView网络图像刷新闪烁,Item View重新绘制导致

原文转载自https://www.jianshu.com/p/3a1c9699c190

Item View重新绘制导致

用到RecyclerView瀑布流加载并展示大批量图片,但一开始单纯使用RecyclerView直接加载图片,使得显示上出现了滑动到顶端时闪烁,Item自动切换位置(切换后数据与展示的画面并不一致),顶端出现空白等等问题,体验上十分差劲,于是开始了优化之旅。现在把优化过程和方法记录下来,供有用者参考。

问题描述

① 在网上查阅资料时,有网友提供了一个解决方案

layoutManager.setGapStrategy(StaggeredGridLayoutManager.GAP_HANDLING_NONE);

这种方法确实可以解决滑动到顶端时Item左右切换的问题,但远远不够。加载瀑布流时仍然存在列的跳动、闪烁、顶端有空白等问题,需要进一步优化。

为什么会出现这样的问题?

② 为什么会出现这种列跳动、item闪烁、空白的问题呢?经过分析,应该是由于我们加载的图片高度不确定(宽度确定因为可以根据屏幕宽度和每行Item数目进行等分),而当我们向RecyclerView下方滑动一段距离后,由于ViewHolder的回收机制,item的尺寸并不确定,滑回到上方时Item需要重新自行绘制,于是这个又导致重绘,所以会有闪烁、跳动、空白等问题。说到底,只要我们在重绘前确定了Item的尺寸,那么就可以避免Item去重新计算自己的尺寸,就可以避免重绘导致的诸多问题。

这个时候有同学会说了,那我不让RecyclerView回收不就完了,需要你搞这些七拐八弯的门道吗?对于这些同学我只能说:OOM了解一下。

解决方案

我们从后台请求到图片后,先将其下载下来,再使用一个IntentService,根据Url获取Bitmap

首先成功从后台拉取到图片后,启动IntentService,处理图片.

ImageService.startService(MainActivity.this, data, mSubtype);  

处理过程:使用IntentService根据url获取Bitmap,在子线程中处理图片,用完后Service自行结束,再使用EventBus通知主线程

public class ImageService extends IntentService {  

public DataService() {  
    super("");  
 }  

public static void startService(Context context, List datas, String subtype) {  

  Intent intent =new Intent(context, ImageService.class);  

  intent.putParcelableArrayListExtra("data", (ArrayList) datas);  

  intent.putExtra("subtype", subtype);  

  context.startService(intent);  
  }  

@Override
protected void onHandleIntent(Intent intent) {  
    if (intent == null) {  
        return;  
     }  
    List datas = intent.getParcelableArrayListExtra("data");  

    String subtype = intent.getStringExtra("subtype");  

    handleGirlItemData(datas, subtype);  

    }  

private void handleGirlItemData(List datas, String subtype) {  

        if (datas.size() == 0) {  

        EventBus.getDefault().post("finish");  

        return;  
      }  

        for (GirlItemData data : datas) {  

        Bitmap bitmap = ImageLoader.load(this, data.getUrl());  

        if (bitmap != null) {  

                data.setWidth(bitmap.getWidth());  

                data.setHeight(bitmap.getHeight());  

            }  

            data.setSubtype(subtype);  

        }  

        EventBus.getDefault().post(datas);  

    }  
} 

处理完再在Adapter中加载:

public class GirlAdapter extends BaseQuickAdapter {  

public GirlAdapter(){  
    super(R.layout.item_girl_layout);  
 }  

@Override  

protected void convert(BaseViewHolder helper, GirlItemData item) {  

        ScaleImageView imageView = helper.getView(R.id.girl_item_iv);  

        imageView.setInitSize(item.getWidth(), item.getHeight());  

        ImageLoader.load(BaseApplication.getContext(),  

                item.getUrl(), imageView);  

    }  

public void deleteItem(int position){  

        remove(position);  

        notifyDataSetChanged();  
    }  

}

这个时候我们可以发现:瀑布流确实也不闪烁了,也不突然切换列了,空白现象好像也消失了。

但是还是有不对的地方:瀑布流加载的速度慢了许多。。。这个问题可能比较严重了,用户打开5s还看到的是一片空白,于是回到桌面把我们app卸了。。。

为什么会出现这个问题呢?因为在优化以前,我们从后台得到Json文件(包括图片id,url,owner等),瀑布流二话不说就开始加载了,Glide再根据url去下载图片,下载完一张就在瀑布流中展示出一张,下载之前展示的是占位图。

而优化之后呢?比如我们一次性拉取到10张照片的json数据,我们需要完整下载10张图片,处理完长宽信息,才能展示出来,这个时间就久了。

所以,这个时候只能给后台同学提需求了:下放的Json数据需要包含图片的长宽信息,这样我们就不用在客户端处理了。

最后,我们在测试中发现,在瀑布流中删除某个Item之后,滑回到首页仍然有小概率出现顶方存在空白的情况。对于这种问题,只需要给RecyclerView设置监听,假如删除过Item且滑回到首页,就再刷新一次Adapter。

mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {  

@Override  
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {  

        super.onScrollStateChanged(recyclerView, newState);  

        }  

@Override  
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {  

        super.onScrolled(recyclerView, dx, dy);  

        if (isItemDeleted){  

        StaggeredGridLayoutManager layoutManager = (StaggeredGridLayoutManager) recyclerView.getLayoutManager();  

        int[] firstVisibleItem = null;  

         firstVisibleItem = layoutManager.findFirstVisibleItemPositions(firstVisibleItem);  

        if (firstVisibleItem != null && firstVisibleItem[0] == 0) {  

        if (mAdapter!=null) {  

        isItemDeleted =false;  

        mAdapter.notifyDataSetChanged();  
               }  

        }  
     }  
   }  
 }); 

基本上解决方案可以应对瀑布流中Item错乱的大多数情况了

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值