我正在使用 Recyclerview 的 GridLayoutManager 在我的应用程序中显示自定义图库。我已经实现了画廊等所有功能。但是我坚持了一件小事。在一行中,我有 3 张图片。但我需要减少图像之间的空间。这样做时,我不想连续显示超过 3 张图像,但图像大小(如果需要)可以增加。
最佳答案
您可以使用自定义 RecyclerViewItemDecorator
:
public class RecyclerViewItemDecorator extends RecyclerView.ItemDecoration {
private int spaceInPixels;
public RecyclerViewItemDecorator(int spaceInPixels) {
this.spaceInPixels = spaceInPixels;
}
@Override
public void getItemOffsets(Rect outRect, View view,
RecyclerView parent, RecyclerView.State state) {
outRect.left = spaceInPixels;
outRect.right = spaceInPixels;
outRect.bottom = spaceInPixels;
if (parent.getChildLayoutPosition(view) == 0) {
outRect.top = spaceInPixels;
} else {
outRect.top = 0;
}
}
}
然后将它添加到您的RecyclerView
:
// For example 10 pixels
int spaceInPixels = 10;
mRecyclerView.addItemDecoration(new RecyclerViewItemDecorator(spaceInPixels));
希望对您有所帮助!
关于android - 如何在 android 中的回收器 View (GridLayoutManager)中减少列之间的空间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36257126/