自定义一个管理器
public class MyLayoutManager extends RecyclerView.LayoutManager {
@Override
public RecyclerView.LayoutParams generateDefaultLayoutParams() {
return new RecyclerView.LayoutParams(
RecyclerView.LayoutParams.WRAP_CONTENT,
RecyclerView.LayoutParams.WRAP_CONTENT);
}
@Override
public boolean canScrollVertically() {
return false;
}
@Override
public void onLayoutChildren(RecyclerView.Recycler recycler, RecyclerView.State state) {
detachAndScrapAttachedViews(recycler);
int sumWidth = getWidth();
int curLineWidth = 0, curLineTop = 0;
int lastLineMaxHeight = 0;
for (int i = 0; i < getItemCount(); i++) {
View view = recycler.getViewForPosition(i);
addView(view);
measureChildWithMargins(view, 0, 0);
int width = getDecoratedMeasuredWidth(view);
int height = getDecoratedMeasuredHeight(view);
curLineWidth += width;
if (curLineWidth <= sumWidth) {
layoutDecorated(view, curLineWidth - width+20, curLineTop+20, curLineWidth, curLineTop + height);
lastLineMaxHeight = Math.max(lastLineMaxHeight, height);
} else {
curLineWidth = width;
if (lastLineMaxHeight == 0) {
lastLineMaxHeight = height;
}
curLineTop += lastLineMaxHeight;
layoutDecorated(view, 20, curLineTop+20, width, curLineTop + height);
lastLineMaxHeight = height;
}
}
}
}
Kotlin写法 ,直接调用即可
card_recycle.layoutManager=MyLayoutManager()
cardAdapter= Carddragadapter(list)
card_recycle.adapter=cardAdapter