android - GridLayoutManager 间距不一致

标签 android android-layout layout android-recyclerview recyclerview-layout

所以我使用 RecyclerViewGridLayoutManager 项目之间的间距在项目周围不一致

这是它在 4 x 4 网格中的样子

这是它在 5 x 5 网格中的样子

我已经尝试了很多来自 this 的解决方案和 thisthis

但对我没有任何作用

这是我如何设置我的适配器

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/

相关文章:

Android:标题栏颜色不改变

android - Flutter C++ 内存分配导致光栅线程卡顿 - Android NDK Dart FFI

Android 在 ScrollView 中添加 ListView 并从 ListView 中删除滚动

android - 在应用程序启动时关闭抽屉导航

android - View 绑定(bind)迁移导致的代码重复

html - 在 IE9 中可以使用 3 个 DIVS 的响应式布局吗?

android - 在 viewpager 中托管多个 fragment

java - 配置 SAX 解析器

Android 通过 UDP 发送字符串

android - 如何在 ScrollView 中设置 onScrollStop 事件