所以我使用 RecyclerView
和 GridLayoutManager
项目之间的间距在项目周围不一致
这是它在 4 x 4 网格中的样子
这是它在 5 x 5 网格中的样子
我已经尝试了很多来自 this 的解决方案和 this和 this
但对我没有任何作用
这是我如何设置我的适配器
mLayoutManager = new GridLayoutManager(mContext, 5);
mRecyclerView.setLayoutManager(mLayoutManager);
int spacingInPixels = getResources().getDimensionPixelSize(R.dimen.spacing);
mRecyclerView.addItemDecoration(new SpacesItemDecoration(spacingInPixels));
这是我的 SpacesItemDecoration
类
public class SpacesItemDecoration extends RecyclerView.ItemDecoration {
private int space;
public SpacesItemDecoration(int space) {
this.space = space;
}
@Override
public void getItemOffsets(Rect outRect, View view,RecyclerView parent, RecyclerView.State state)
{
outRect.left = space;
outRect.right = space;
outRect.top = space;
outRect.bottom = space;
// Add top margin only for the first item to avoid double space between items
if (parent.getChildLayoutPosition(view) == 0)
{
outRect.top = space;
}
else
{
outRect.top = 0;
}
}
}
我不知道为什么它对我不起作用
There is no margin or padding to my recyclerview & item
最佳答案
当您将适配器绑定(bind)到回收器 View 时。 在适配器中制作布局,在 View 控件大小中提供布局。 像 adapter_row.xml
<ImageView
android:layout_width="@dimen/_100sdp"
android:layout_height="@dimen/_100sdp"
android:id="@+id/amIvImage"
android:src="@drawable/a"/>
并且如果您希望在依赖项下使用的所有设备中的空间相同。 添加应用程序级别的 gradle 文件..
implementation 'com.intuit.sdp:sdp-android:1.0.4'
关于android - GridLayoutManager 间距不一致,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49812498/