简体   繁体   English

RecyclerView中的GridLayoutManager 3列

[英]GridLayoutManager 3 columns in RecyclerView

I'm trying to make simple GridLayout with 3 columns but although I get 3 columns I get strange gap between rows. 我正在尝试使用3列创建简单的GridLayout,但是虽然我得到3列,但行之间会出现奇怪的差距。 So, I get 3 images in row than one row that is empty (which it seems has height matching height of the row abov(that has images), then row of 3 images, than strange gap...etc. I expected not to get than blank gap. How to remove this blank gap? Trying to set wrap_content to layout_height on recycler view, and fragment didn't helped. This is my code: 所以,我得到的行中有3个图像,而不是一行是空的(它看起来高度匹配行abov的高度(有图像),然后是3行图像的行,而不是奇怪的间隙......等等。我预计不会比空白差距。如何删除这个空白?尝试在recycler视图上将wrap_content设置为layout_height,片段没有帮助。这是我的代码:

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

This is xml of the 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>  

Notice that this layout has an important fragment called fragmentImagesGrid which has this layout: 请注意,此布局有一个名为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>

This i the layout of the one element inside recycler view: 这是我在Recycler视图中的一个元素的布局:

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

我不希望你在这张图片上看到这个空白

Looks like it happens because your images have different sizes. 看起来它发生是因为您的图像有不同的大小。 Change your item to LinearLayout and set its layout height and width to match_parent. 将项目更改为LinearLayout并将其布局高度和宽度设置为match_parent。 Set gravity="center" (Of the linear layout). 设置gravity =“center” (线性布局)。 Another issue is android:layout_height="wrap_content" . 另一个问题是android:layout_height =“wrap_content” The RecyclerView doesn't work well when you set the height property to wrap_content. 将height属性设置为wrap_content时,RecyclerView无法正常工作。 If you still want to use RecyclerView with wrap_content, you can ckeck this solution 如果您仍想将RecyclerView与wrap_content一起使用,则可以使用解决方案

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM