2

There are large random placed gaps appearing in between the rows of views in my RecyclerView, same like other users noted in bellow questions.

I have looked into suggested solutions from other questions like:

Large gap forms between RecyclerView items when scrolling down

Large gap between RecyclerView items

RecyclerView items with big empty space after 23.2.0

Android RecyclerView Blank Space

RECYCLERVIEW SPACE BETWEEN ITEMS ON ANDROID APPLICATION

RecyclerView items with very large empty space at bottom after updating to support library 23.2.1

but they all seam to suggest the same thing: change match_parent to wrap_content in your layouts. As it can be seen from the layout code bellow, I have tried that, but I still get the same gaps.

I need to note that these gaps are present only in tablets in the landscape.

My layouts:

content_main.xml:

<FrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/fragment_container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    />

content_main.xml in layout-sw600pd-port:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              xmlns:tools="http://schemas.android.com/tools"
              xmlns:app="http://schemas.android.com/apk/res-auto"
              android:id="@+id/llTwoPane"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:baselineAligned="true"
              android:divider="?android:attr/dividerHorizontal"
              android:orientation="horizontal"
              android:paddingTop="@dimen/gap_xl"
              tools:context=".MainActivity">

    <fragment
        android:id="@+id/fragment_main"
        android:name=".MainFragment"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        tools:layout="@android:layout/list_content" />

    <FrameLayout
        android:id="@+id/fragment_container"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="@integer/parts_fd"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        />

</LinearLayout>

content_main.xml in layout-sw600pd

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              xmlns:tools="http://schemas.android.com/tools"
              xmlns:app="http://schemas.android.com/apk/res-auto"
              android:id="@+id/llTwoPane"
              android:layout_width="match_parent"
            android:layout_height="wrap_content"
              android:baselineAligned="true"
              android:orientation="horizontal"
              android:paddingTop="@dimen/gap_xl">

    <fragment
        android:id="@+id/fragment_main"
        android:name=".MainFragment"
        android:layout_width="0dp"
        android:layout_weight="@integer/parts_fm"
      android:layout_height="wrap_content"
        tools:layout="@android:layout/list_content" />

    <FrameLayout
        android:id="@+id/fragment_container"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="@integer/parts_fd"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        />

</LinearLayout>

fragment_main.xml:

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/llFragmentMain"
    android:layout_width="match_parent"
  android:layout_height="wrap_content"
    android:orientation="vertical"
    tools:context=".MainFragment"
    tools:showIn="@layout/activity_main"
    android:background="@color/colorGray"
    >

    <Spinner
        android:id="@+id/spnSort"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textAlignment="center"
        android:paddingTop="@dimen/title_gap"
        android:paddingBottom="@dimen/title_gap"
        android:paddingLeft="@dimen/gap_m"
        android:paddingRight="@dimen/gap_m"
        />

    <android.support.v7.widget.RecyclerView
        android:id="@+id/rvPosters"
        android:layout_width="match_parent"
      android:layout_height="wrap_content"
        android:padding="@dimen/half_poster_spacing"
        android:background="@color/colorLight"
        android:scrollbars="vertical"
        android:animateLayoutChanges="false"
        android:scrollbarThumbHorizontal="@drawable/scrollbar"
        />

</LinearLayout>

item.xml inflated in adapter:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/flPosterItem"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_margin="@dimen/half_poster_spacing"
    android:descendantFocusability="blocksDescendants"
    >
    <ImageView
        android:id="@+id/imvPoster"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:adjustViewBounds="true"
        android:contentDescription="@string/poster_content_description"
        />
    <Button
        android:id="@+id/btnFavouriteInMain"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:minHeight="0dp"
        android:minWidth="@dimen/fav_button_minw"
        android:paddingBottom="@dimen/fav_button_padding"
        android:paddingLeft="@dimen/fav_button_padding"
        android:paddingStart="@dimen/fav_button_padding"
        android:paddingRight="@dimen/fav_button_padding"
        android:paddingEnd="@dimen/fav_button_padding"
        android:background="@color/colorButtonStarBgd"
        android:textStyle="bold"
        android:textAlignment="center"
        android:textSize="@dimen/text_size_m"
        android:layout_gravity="bottom|end"
        />
</FrameLayout>

I set my RecyclerView with:

recyclerView.setLayoutManager(posterAdapter.getGridLayoutManager());
recyclerView.setHasFixedSize(true);
recyclerView.setNestedScrollingEnabled(false);
recyclerView.setAdapter(posterAdapter);

And grid layout manager is set with:

GridLayoutManager getGridLayoutManager() {
    GridLayoutManager gridLayoutManager = new GridLayoutManager(context, mNumberOfColumns);
    gridLayoutManager.setSmoothScrollbarEnabled(true);
    gridLayoutManager.setAutoMeasureEnabled(true);
    gridLayoutManager.setSpanCount(mNumberOfColumns);
    return gridLayoutManager;
}

On phones gaps don't appear in any orientation. On tablets, in portrait there is only one column. Even with match_parent the gaps do not appear. In landscape there are three to four columns and gaps appear even with wrap_content and it's always between full rows.

I have tried to remove

  • recyclerView.setHasFixedSize(true);
  • ridLayoutManager.setAutoMeasureEnabled(true);
  • and to make item.xml image only (make button gone)

but no use.

Does anyone have some other explanation?

Community
  • 1
  • 1
Vlad
  • 820
  • 10
  • 29

1 Answers1

0

As noted in the answers for other questions mentioned above, changing from match_parent to wrap_content should solve the problem. But it does not, and if anyone can explain why I will accept that as the answer.

I have solved it with reversing back to match_parent for RecyclerView and relating layout heights, and setting setAutoMeasureEnabled(false) for GridLayoutManager (third line in the above getGridLayoutManager() function - just changed true to false).

Vlad
  • 820
  • 10
  • 29