Я пытаюсь добавить интервал ниже последней строки элемента в RecyclerView
с помощью GridLayoutManager
. Для этой цели я использовал пользовательский ItemDecoration
с нижним отступом, когда его последний элемент выглядит следующим образом:
public class SpaceItemDecoration extends RecyclerView.ItemDecoration {
private int space;
private int bottomSpace = 0;
public SpaceItemDecoration(int space, int bottomSpace) {
this.space = space;
this.bottomSpace = bottomSpace;
}
public SpaceItemDecoration(int space) {
this.space = space;
this.bottomSpace = 0;
}
@Override
public void getItemOffsets(Rect outRect, View view,
RecyclerView parent, RecyclerView.State state) {
int childCount = parent.getChildCount();
final int itemPosition = parent.getChildAdapterPosition(view);
final int itemCount = state.getItemCount();
outRect.left = space;
outRect.right = space;
outRect.bottom = space;
outRect.top = space;
if (itemCount > 0 && itemPosition == itemCount - 1) {
outRect.bottom = bottomSpace;
}
}
}
Но проблема с этим методом заключается в том, что он перепутал высоты элементов в сетке в последней строке. Я предполагаю, что GridLayoutManager
изменяет высоту для элементов на основе расстояния слева. Каков правильный способ достижения этого?
Это будет корректно работать для LinearLayoutManager
. Просто в случае GridLayoutManager
его проблематично.
Это очень полезно, если у вас есть FAB
внизу и нужны элементы в последней строке для прокрутки над FAB
, чтобы они могли быть видны.