Android-瀑布流

point1

需要服务器返回图片规格

point2

在adapter的onBindViewHolder中根据服务端返回的图片的规格设置layoutParams

1
2
3
4
5
6
7
8
9
10
11
12
13
@Override
public void onBindViewHolder(@NonNull CusHolder holder, int position) {
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds=true;
BitmapFactory.decodeResource(mContext.getResources(), mData.get(position).score,options);
float f=options.outWidth/(float)options.outHeight;
int h= (int) (650/f);
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, h);
holder.itemView.setLayoutParams(lp);

// holder.mIv.setImageResource(mData.get(position).score);
Glide.with(mContext).load(mData.get(position).score).into(holder.mIv);
}

point3

设置rv的manager为StaggeredGridLayoutManager

1
mRv.setLayoutManager(new StaggeredGridLayoutManager(2,StaggeredGridLayoutManager.VERTICAL));

point4(不用设置)

  • 来回滑动的时候item交换位置:在设置layoutManager的时候,多加一句代码防止item位置交换 layoutManager.setGapStrategy(StaggeredGridLayoutManager.GAP_HANDLING_NONE);
  • 顶部留空的问题,在onScrollStateChanged里再加一句layoutManager.invalidateSpanAssignments();

参考:https://blog.csdn.net/qq_31390699/article/details/53465671