如何摆脱 GridLayoutManager 项目之间的差距

How to get rid of gaps between GridLayoutManager items

我正在使用 RecyclerViewGridLayoutManager 布局管理器。

recyclerView.setLayoutManager(new GridLayoutManager(context, 3))

但是由于我的项目布局的高度是 wrap_content,因此项目之间存在一些间隙。 我该如何摆脱它们?

我认为 GridLayoutManager 不可能(或很难)。但是您可以使用: StaggeredGridLayoutManager

A LayoutManager that lays out children in a staggered grid formation. It supports horizontal & vertical layout as well as an ability to layout children in reverse.
Staggered grids are likely to have gaps at the edges of the layout. To avoid these gaps, StaggeredGridLayoutManager can offset spans independently or move items between spans. You can control this behavior via setGapStrategy(int).

Code example