在RecyclerView中使用GridLayoutManager实现3列布局

5
我正在尝试使用3列制作简单的GridLayout,但是虽然我得到了3列,但是我得到了行间奇怪的间隔。因此,我在一行中得到了3个图像,然后是一个空行(它似乎具有与上一行(包含图片)高度相匹配的高度),然后是3个图像的行,然后是奇怪的间隔...等等。我期望不会有这样的空白间隙。如何去除这个空白间隙?尝试将RecyclerView和Fragment的layout_height设置为wrap_content并没有帮助。
这是我的代码:
    final GridLayoutManager layoutManager = new GridLayoutManager(this,3);
    layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
    recyclerView.setLayoutManager(layoutManager);
    recyclerView.setAdapter(adapter);

以下是活动的 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,其具有以下布局:
   <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>

这是Recycler 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

1个回答

1

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


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接