android - RecyclerView 中的 GridLayoutManager 3 列

标签 android android-recyclerview gridlayoutmanager

我正在尝试制作具有 3 列的简单 GridLayout,但尽管我得到 3 列,但行之间出现奇怪的间隙。所以,我在一行中得到 3 张图像而不是空行(它似乎具有高度匹配的行 abov(有图像)的高度,然后是 3 张图像的行,而不是奇怪的间隙......等等。我预计不会得到比空白间隙。如何删除这个空白间隙?尝试在回收站 View 上将 wrap_content 设置为 layout_height,但 fragment 没有帮助。 这是我的代码:

    final GridLayoutManager layoutManager = new GridLayoutManager(this,3);
    layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
    recyclerView.setLayoutManager(layoutManager);
    recyclerView.setAdapter(adapter);

这是 Activity 的 xml:

   <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" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="com.adriagate.adriagateonlineandroid.activities.ImagesActivity">


<fragment xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:id="@+id/fragmentImagesHeader"
    android:name="com.adriagate.adriagateonlineandroid.fragments.ImagesHeader"
    tools:layout="@layout/fragment_images_header" android:layout_width="match_parent"
    android:layout_height="match_parent" >
</fragment>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:id="@+id/fragmentImagesGrid"
    android:name="com.adriagate.adriagateonlineandroid.fragments.ImagesList"
    tools:layout="@layout/fragment_images_list" android:layout_width="match_parent"
    android:layout_height="wrap_content" >
</fragment>
   </RelativeLayout>  

请注意,此布局有一个名为 fragmentImagesGrid 的重要 fragment ,它具有以下布局:

   <FrameLayout 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.adriagate.adriagateonlineandroid.fragments.ImagesList">

    <android.support.v7.widget.RecyclerView
      android:id="@+id/recycler_view_images"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
     tools:listitem="@layout/recycler_view_images_one_row"
    >

</FrameLayout>

这是回收站 View 中一个元素的布局:

   <?xml version="1.0" encoding="utf-8"?>
     <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
       android:orientation="vertical"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
        xmlns:tools="http://schemas.android.com/tools"
      >

<ImageView
    android:id="@+id/imageViewImagesAllOneRow"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal"
    tools:src="@drawable/slika_200_200"
    />

   </FrameLayout> 

I don't want this blank gap you can see on this image

最佳答案

看起来是因为您的图片尺寸不同。 将您的项目更改为 LinearLayout 并将其布局高度和宽度设置为 match_parent。设置gravity="center"(线性布局)。 另一个问题是 android:layout_height="wrap_content"。当您将高度属性设置为 wrap_content 时,RecyclerView 无法正常工作。如果您仍想将 RecyclerView 与 wrap_content 一起使用,您可以查看 this解决方案

关于android - RecyclerView 中的 GridLayoutManager 3 列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34567871/

相关文章:

java - 获取回收器 View 位置给出 NullPointerException

android - GridLayoutManager 跨度大小 RecycleView

android - 带有 GridLayoutManager 和 Picasso 的 RecyclerView 显示错误图像

android - 为特定口味禁用 R8

android - 在蓝牙关闭的情况下识别 Android/Kotlin 上的 BLE 设备

android - 如何更改 Android Studio 3.0.1 中新项目的默认文件结构?

Java 嵌套布局

android - React-Native 和 Android : How to request for Permissions

android - Recyclerview StaggeredGridLayoutManager - 查找最后一个可见项目

android - RecyclerView Android错误:E/RecyclerView:未连接适配器;跳过布局