小编Cou*_*ney的帖子

如何使用GridLayoutManager删除RecyclerView中行之间的间距

我想有一个完整的网格格栅RecyclerViewGridLayoutManager出于某种原因,行之间,并在我的顶部和底部增加了一个水平的空间,但RecyclerView.我无法摆脱它.我搜索了解决方案并尝试ItemDecorator通过将维度明确设置为0,但这不起作用.没有其他工作.

图像.

我的代码ItemDecorator非常标准,包括在内.

private 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;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

这些是我使用的布局文件.

/layout/activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" …
Run Code Online (Sandbox Code Playgroud)

android gridlayoutmanager android-recyclerview

5
推荐指数
1
解决办法
4109
查看次数