防止RecyclerView瀑布流Item闪烁和位置乱动
需求:瀑布流展示图片列表,但是没有给出图片大小比例,需要下载图片后计算出图片比例在设置Item高度,已经做了部分处理,但是还会出现Item位置乱动在刷新时出现图片闪烁。
已使用网上的部分解决方案:
1. layout.setGapStrategy(StaggeredGridLayoutManager.GAP_HANDLING_NONE);
2. mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
super.onScrollStateChanged(recyclerView, newState);
layout.invalidateSpanAssignments();
}
});
3.在适配器中添加Map<Integer,Integer>并在的getView()方法中将对应位置VIew的高度存储到map中
int viewHeight = map.get(position);
if(viewHeight != 0){
ViewGroup.LayoutParams layoutParams = imageView.getLayoutParams();
layoutParams.height = imageHeight;
imageView.setLayoutParams(layoutParams);
}else{
//网络请求获取到图片的Drawable或者bitmap,得到图片宽高比例,并得到View高度viewHeight
map.put(position,viewHeight);
ViewGroup.LayoutParams layoutParams = imageView.getLayoutParams();
layoutParams.height = imageHeight;
imageView.setLayoutParams(layoutParams);
}
即便这样操作之后还会出现问题,请大神们帮忙解决一下