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

标签 android android-recyclerview gridlayoutmanager

我正在尝试使用 RecyclerViewGridLayoutManager 创建一个全屏网格,但出于某种原因,在行之间以及顶部和底部添加了水平空间我的 RecyclerView。我无法摆脱它。我搜索了解决方案并通过将维度显式设置为 0 来尝试 ItemDecorator,但这没有用。没有其他工作。

image .

我的 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_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>

/layout/activity_main_fragment.xml

<?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>

/layout/activity_movies_grid.xml

<?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" />

最佳答案

应该是因为Image的缩放,而不是别的。所以尝试将 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" />

关于android - 如何使用 GridLayoutManager 删除 RecyclerView 中的行间距,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38594468/

相关文章:

java - Android中如何将ArrayList的所有元素显示为Chips

android - 检测 RecyclerView 中呈现项目的列

android - RecyclerView - 屏幕外项目跳过第一次布局

java - 通过导航内容 URI 导航到纬度、经度点?

android - 如何在 Google TV 上安装 Android 应用程序?

android - 应用程序因适配器而崩溃

android - 我对项目中每个项目的适配器定位有疑问

java - 使用 TextView.setText 显示数据时出错 : Resources$NotFoundException: String resource ID #0x0

java - 无法在抽屉导航 fragment 中包含 RecyclerView

android - Android RecyclerView中实现多选