Deno Agüero
Deno Agüero

Reputation: 599

RecyclerView does not show all items inside Fragment

The problem

In my Activity I initialize a Fragment, which contains a RecyclerView. But I've recognized that it is not displaying all items. That means instead of 14 it only shows 11 items. In addition the last visible item (number 12) is cutted off

My Implementation

The Activity contains an empty FrameLayout that acts as the Fragment container:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res./android"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="#e0e0e0">

<LinearLayout
    android:id="@+id/layout_footer"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:background="@drawable/layout_border_white"
    android:paddingBottom="@dimen/footer_padding">

    <include layout="@layout/footer"></include>

</LinearLayout>


<FrameLayout
    android:id="@+id/fragment_start"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_above="@id/layout_footer"/>

</RelativeLayout>

And here is the RecyclerView in the Fragment:

<android.support.constraint.ConstraintLayout   
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#e0e0e0"
android:orientation="vertical">

<LinearLayout
    android:id="@+id/layout_header"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:background="@android:color/white"
    android:padding="@dimen/startpage_padding"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent">

    <include layout="@layout/header"></include>

</LinearLayout>


<android.support.v7.widget.RecyclerView
    android:id="@+id/recycler_view"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_margin="@dimen/startpage_margin"
    android:scrollbars="vertical"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/layout_header"/>

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent">

    <ProgressBar
        android:id="@+id/pb_loading_indicator"
        android:layout_width="@dimen/startpage_progressbar"
        android:layout_height="@dimen/startpage_progressbar"
        android:visibility="invisible"/>

</LinearLayout>


<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent">

    <TextView
        android:id="@+id/tv_error_message_display"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:gravity="center"
        android:padding="@dimen/startpage_text_padding"
        android:text="@string/error_message"
        android:textSize="@dimen/startpage_text"
        android:visibility="invisible"/>
</LinearLayout>

Solving Approaches

I've already tried it with match_parent in the RecyclerView and also added a paddingBottom to it. With paddingBottom="30dp" one more element is visible but that's not a good solution. Furthermore my Activity is using the AlertDialog-Theme which is setted in the Manifest. Removing it shows the same result.

Any help would be greatly appreciated.

Upvotes: 5

Views: 11060

Answers (3)

user13412764
user13412764

Reputation: 1

I used wrap_content for the height but the bottom of the recycleview still scrolled off bottom of the screen so that the last item was not visible. I resorted to using a finagled solution of adding a bottom padding on the recyclerview until the last item was visible

Upvotes: 0

Deno Ag&#252;ero
Deno Ag&#252;ero

Reputation: 599

I finally fixed it. The problem was the wrap_content in my RecyclerView inside ConstraintLayout. By using wrap_content for the height of the RecyclerView the parent cuts the bottom of it off. So you have to use 0dp for the height and a constraintBottom.

    <android.support.v7.widget.RecyclerView
    android:id="@+id/recycler_view"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:layout_margin="@dimen/startpage_margin"
    android:scrollbars="vertical"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/layout_header"/>

Upvotes: 30

user8466383
user8466383

Reputation:

try to change your xml to following, as constraint layout wraps spaces when used in incorrect way:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="#e0e0e0">

    <LinearLayout
        android:id="@+id/layout_header"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@android:color/white">

        <include layout="@layout/support_simple_spinner_dropdown_item"/>
    </LinearLayout>


    <android.support.v7.widget.RecyclerView
        android:id="@+id/recycler_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="@dimen/startpage_margin"
        android:scrollbars="vertical"
        android:layout_below="+id/layout_header"
        />

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="match_parent">

        <ProgressBar
            android:id="@+id/pb_loading_indicator"
            android:layout_width="match_parent"
            android:layout_height="10dp"
            android:layout_centerInParent="true"
            android:visibility="invisible"/>

        <TextView
            android:id="@+id/tv_error_message_display"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:gravity="center"
            android:text="Error"
            android:textSize="20sp"
            android:visibility="invisible"/>

    </RelativeLayout>
</LinearLayout>

Upvotes: 2

Related Questions