Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

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

Notice that this layout has an important fragment called fragmentImagesGrid 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 i the layout of the one element inside 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

like image 217
Vlado Pandžić Avatar asked Nov 08 '22 22:11

Vlado Pandžić


1 Answers

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. Set gravity="center" (Of the linear layout). Another issue is android:layout_height="wrap_content". The RecyclerView doesn't work well when you set the height property to wrap_content. If you still want to use RecyclerView with wrap_content, you can ckeck this solution

like image 87
Georgy Avatar answered Nov 14 '22 22:11

Georgy