Recyclerview 自定义适配FlexboxLayoutManager可设置间距的ItemDecoration
时间: 2024-01-25 17:05:06 浏览: 265
RecyclerViewBug
您可以使用RecyclerView的ItemDecoration来实现自定义间距。以下是一个自定义的ItemDecoration示例,可以在FlexboxLayoutManager中使用:
```java
public class CustomItemDecoration extends RecyclerView.ItemDecoration {
private int mSpacing;
public CustomItemDecoration(int spacing) {
this.mSpacing = spacing;
}
@Override
public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
super.getItemOffsets(outRect, view, parent, state);
outRect.top = mSpacing;
outRect.bottom = mSpacing;
outRect.left = mSpacing;
outRect.right = mSpacing;
}
}
```
在FlexboxLayoutManager中使用:
```java
FlexboxLayoutManager layoutManager = new FlexboxLayoutManager(context);
recyclerView.setLayoutManager(layoutManager);
CustomItemDecoration itemDecoration = new CustomItemDecoration(10); // 设置间距
recyclerView.addItemDecoration(itemDecoration);
```
这样就可以设置自定义的间距了。
阅读全文