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

Android 如何使用GridLayoutManager删除RecyclerView中的行间距,android,android-recyclerview,gridlayoutmanager,Android,Android Recyclerview,Gridlayoutmanager,我正在尝试使用带有RecyclerView和GridLayoutManager的全屏网格,但出于某种原因,在行之间以及我的RecyclerView的顶部和底部添加了一个水平空间。我无法摆脱它。我搜索了解决方案并尝试了itemdecotor,将维度显式设置为0,但没有成功。没有别的办法 这个 myItemDecorator的代码非常标准,包括在内 private class RecyclerViewItemDecorator extends RecyclerView.ItemDecoration

我正在尝试使用带有
RecyclerView
GridLayoutManager
的全屏网格,但出于某种原因,在行之间以及我的
RecyclerView
的顶部和底部添加了一个水平空间。我无法摆脱它。我搜索了解决方案并尝试了
itemdecotor
,将维度显式设置为0,但没有成功。没有别的办法

这个

my
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;
        }
    }
}
这些是我使用的布局文件

/layout/activity\u 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"
    tools:context="com.example.korah.moviesapp.MainActivity">

    <fragment
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/main_fragment"
        android:name="com.example.korah.moviesapp.MainActivityFragment"
        tools:layout="@layout/activity_main_fragment"
        >
    </fragment>

</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>

<android.support.v7.widget.RecyclerView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/movies_recycler"
    xmlns:android="http://schemas.android.com/apk/res/android">    
    </android.support.v7.widget.RecyclerView>
<?xml version="1.0" encoding="utf-8"?>

<ImageView
    android:id="@+id/imageView"
    android:src="@drawable/interst"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    xmlns:android="http://schemas.android.com/apk/res/android" />

/layout/activity\u main\u fragment.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"
    tools:context="com.example.korah.moviesapp.MainActivity">

    <fragment
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/main_fragment"
        android:name="com.example.korah.moviesapp.MainActivityFragment"
        tools:layout="@layout/activity_main_fragment"
        >
    </fragment>

</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>

<android.support.v7.widget.RecyclerView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/movies_recycler"
    xmlns:android="http://schemas.android.com/apk/res/android">    
    </android.support.v7.widget.RecyclerView>
<?xml version="1.0" encoding="utf-8"?>

<ImageView
    android:id="@+id/imageView"
    android:src="@drawable/interst"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    xmlns:android="http://schemas.android.com/apk/res/android" />

/layout/activity\u movies\u grid.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"
    tools:context="com.example.korah.moviesapp.MainActivity">

    <fragment
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/main_fragment"
        android:name="com.example.korah.moviesapp.MainActivityFragment"
        tools:layout="@layout/activity_main_fragment"
        >
    </fragment>

</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>

<android.support.v7.widget.RecyclerView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/movies_recycler"
    xmlns:android="http://schemas.android.com/apk/res/android">    
    </android.support.v7.widget.RecyclerView>
<?xml version="1.0" encoding="utf-8"?>

<ImageView
    android:id="@+id/imageView"
    android:src="@drawable/interst"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    xmlns:android="http://schemas.android.com/apk/res/android" />

应该是因为图像的缩放而不是其他原因。因此,请尝试将ImageView的scaleType设置为fitXY

<ImageView
    android:id="@+id/imageView"
    android:src="@drawable/interst"
    android:layout_width="wrap_content"
    android:scaleType="fitXY"
    android:layout_height="wrap_content"
    xmlns:android="http://schemas.android.com/apk/res/android" />

尝试为ImageView设置scaleType=“fitXY”?是的兄弟!!!!你是救命恩人,回答这个问题,我会接受的。