RecyclerView间距过大
时间: 2023-05-20 12:00:49 浏览: 169
RecyclerView加载不同布局(有间距)
5星 · 资源好评率100%
您可以通过设置RecyclerView的ItemDecoration来调整RecyclerView的间距。您可以创建一个类来实现ItemDecoration,然后在RecyclerView中设置它。以下是一个示例代码:
```
public class MyItemDecoration extends RecyclerView.ItemDecoration {
private int mSpacing;
public MyItemDecoration(int spacing) {
mSpacing = spacing;
}
@Override
public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
outRect.left = mSpacing;
outRect.right = mSpacing;
outRect.bottom = mSpacing;
// Add top margin only for the first item to avoid double space between items
if (parent.getChildAdapterPosition(view) == 0) {
outRect.top = mSpacing;
} else {
outRect.top = 0;
}
}
}
```
然后在RecyclerView中设置它:
```
int spacingInPixels = getResources().getDimensionPixelSize(R.dimen.spacing);
recyclerView.addItemDecoration(new MyItemDecoration(spacingInPixels));
```
这将在RecyclerView的每个项目周围添加指定的间距。
阅读全文