GridLayoutManager 3 columns in RecyclerView

I am trying to make a simple GridLayout with 3 columns, but although I get 3 columns, I have a strange line break. So, I get 3 images per line, than one line that is empty (which apparently has a height matching abov line height (which has images), then a line of 3 images, than a weird space ... and so on. e. I expected to get an empty space. How to remove this empty space? Trying to set wrap_content on layout_height to view recycler, but the fragment did not help. This is my code:

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

This is the xml of activity:

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

Note that this layout has an important fragment, called the ImagesGrid fragment, which has this layout:

   <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 is the location of one element in the 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 do not need this blank space, which you can see in this image

+4
1

, , . LinearLayout match_parent. gravity = "center" ( ). : android: layout_height = "wrap_content" . RecyclerView , height wrap_content. RecyclerView wrap_content, ckeck

0

Source: https://habr.com/ru/post/1622525/


All Articles