我使用以下分隔符进行垂直线性布局,它很好地工作:
public class VerticalSpaceItemDecoration extends RecyclerView.ItemDecoration {
private final int mVerticalSpaceHeight;
public VerticalSpaceItemDecoration(int mVerticalSpaceHeight) {
this.mVerticalSpaceHeight = mVerticalSpaceHeight;
}
@Override
public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
if (parent.getChildAdapterPosition(view) != parent.getAdapter().getItemCount() - 1)
outRect.bottom = mVerticalSpaceHeight;
}
}
基于此,我尝试为GridLayout
编写相同的装饰器,但问题是,它正在切断我的项目。它看起来如下:
public class GridSpaceItemDecoration extends RecyclerView.ItemDecoration {
private final int mSpaceSize;
private final int mCols;
public GridSpaceItemDecoration(int spaceSize, int cols) {
this.mSpaceSize = spaceSize;
mCols = cols;
}
@Override
public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
int rows = (int)Math.floor(parent.getAdapter().getItemCount() / mCols) + 1;
int pos = parent.getChildAdapterPosition(view);
int col = pos % mCols;
int row = (int)Math.floor(pos / mCols);
final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) view.getLayoutParams();
if (col < mCols - 1)
outRect.right = /*params.rightMargin + view.getPaddingRight() +*/ mSpaceSize;
if (row < rows - 1)
outRect.bottom = /*params.leftMargin + view.getPaddingLeft() +*/ mSpaceSize;
}
}
两个Decorators
的结果如下(添加调试的颜色):
图像显示,GridLayout
项目被切断(在项目下方的文字处很容易看到)。我应该提一下,两者的适配器是相同的,我只使用另一个LayoutManager
。这些物品是固定尺寸,这是我需要的东西!
答案 0 :(得分:0)
在setClipChildren(false)
上调用RecyclerView
,它应该修复您看到的由装饰器创建的间距引起的剪辑。