android 使用Glide加载图片闪动问题

1、RecyclerView 有自己默认的动画,去除默认动画

((SimpleItemAnimator) recyclerView.getItemAnimator()).setSupportsChangeAnimations(false);
并且设置对应的adapter,设置
mRecyclerAdapter.setHasStableIds(true);
2、Glide同步加载图片的时候,给对应的imageview设置一个Tag
 
  
if (!album.getCoverUrlSmall().equals(viewHolder.pic.getTag(R.id.aiItemPlaylistAlbum_iv_albumPic))) {
    GlideUtil.setRoundImage(mContext, viewHolder.pic, album.getCoverUrlSmall());
    viewHolder.pic.setTag(R.id.aiItemPlaylistAlbum_iv_albumPic, album.getCoverUrlSmall());
} else {

}
3、Glide去除其本身自带的动画
GlideApp.with(context).load(resId).dontAnimate().centerCrop()
.apply(RequestOptions.bitmapTransform(new GlideRoundTransformation(radius))).into(imageView);
4、Gldie异步加载图片的时候,recycleView滑动太快,会使图片发生错乱,此时最好设置两个Tag
viewHolder.mIvPic.setTag(R.id.aiItemPlaylistAlbum_iv_customPic, mIAiMediaEntry.getEntryUniqueId());
mIAiMediaEntry.getPic(IAiMediaEntry.PIC_SMALL, new IAiMediaEntry.IPictureCallback() {
    @Override
    public void onGetPictureSuccess(IAiMediaEntry entry, String imageUrl) {
        if (entry.getEntryUniqueId().equals(viewHolder.mIvPic.getTag(R.id.aiItemPlaylistAlbum_iv_customPic))) {
            if (!TextUtils.equals((String) (viewHolder.mIvPic.getTag(R.id.item_pic_view_tag)), imageUrl)) {
                if (!TextUtils.isEmpty(imageUrl)) {
                    GlideUtil.setRoundImage(mContext, viewHolder.mIvPic, imageUrl);
                } else {
                    GlideUtil.setRoundImage(mContext, viewHolder.mIvPic, R.drawable.label_demand_default, 7);
                }
                viewHolder.mIvPic.setTag(R.id.item_pic_view_tag, imageUrl);
            }
        }
    }

    @Override
    public void onGetPictureError(IAiMediaEntry entry, String errMsg) {
        if (entry.getEntryUniqueId().equals(viewHolder.mIvPic.getTag(R.id.aiItemPlaylistAlbum_iv_customPic))) {
            GlideUtil.setRoundImage(mContext, viewHolder.mIvPic, R.drawable.label_demand_default, 7);
        }
    }
});
5、在RecyclerView 停止滑动的时候刷新列表,加载图片
 
  
recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
    @Override
    public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
        if(newState == SCROLL_STATE_IDLE){
            mRecyclerAdapter.notifyDataSetChanged();
        }
    }
});


你可能感兴趣的:(android)