Make endless RecyclerView content wrap inside CoordinatorLayout

39 Views Asked by At

I want to display a toolbar, detail layout, and recycler view with paging/endless list. I end up using CoordinatorLayout to contains the detail layout and RecyclerView inside it, with the ReyclerView having appbar scrolling view behavior.

The endless scroll is working, but when the list content is few, it still occupy the whole screen. Setting the ReyclerView height to wrap_content only shrink the scrolling area. I want the RecyclerView to only occupy the necessary space. How do i do that?

(My code is after screenshot)

Expectation:

enter image description here

Reality:

enter image description here

Layout code, or you can access it in Github.

<?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"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <com.google.android.material.appbar.MaterialToolbar
        android:id="@+id/layoutToolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="@color/design_default_color_primary"
        app:title="@string/app_name"
        app:titleTextColor="@color/design_default_color_on_primary" />

    <androidx.coordinatorlayout.widget.CoordinatorLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <com.google.android.material.appbar.AppBarLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="#00FFFFFF"
            app:elevation="0dp">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical"
                app:layout_scrollFlags="scroll">

                <com.google.android.material.progressindicator.LinearProgressIndicator
                    android:id="@+id/pbLoading"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content" />

                <ImageView
                    android:id="@+id/ivAvatar"
                    android:layout_width="160dp"
                    android:layout_height="160dp"
                    android:layout_gravity="center_horizontal"
                    android:layout_marginTop="16dp"
                    tools:src="@tools:sample/avatars" />

                <TextView
                    android:id="@+id/tvName"
                    style="@style/TextAppearance.MaterialComponents.Headline6"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginHorizontal="16dp"
                    android:layout_marginTop="8dp"
                    android:textAlignment="center"
                    tools:text="John Doe" />

                <TextView
                    android:id="@+id/tvBio"
                    style="@style/TextAppearance.MaterialComponents.Caption"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginHorizontal="16dp"
                    android:layout_marginTop="16dp"
                    android:textAlignment="center"
                    tools:text="Bio" />

                <TextView
                    android:id="@+id/tvLocation"
                    style="@style/TextAppearance.MaterialComponents.Body1"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginHorizontal="16dp"
                    android:layout_marginTop="16dp"
                    android:textAlignment="center"
                    tools:text="Bio" />

                <TextView
                    android:id="@+id/tvCompany"
                    style="@style/TextAppearance.MaterialComponents.Body1"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginHorizontal="16dp"
                    android:layout_marginTop="16dp"
                    android:textAlignment="center"
                    tools:text="Bio" />

                <com.google.android.material.divider.MaterialDivider
                    android:layout_width="match_parent"
                    android:layout_height="6dp"
                    android:layout_marginTop="16dp"
                    app:dividerColor="@color/design_default_color_primary" />

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginHorizontal="16dp"
                    android:layout_marginTop="16dp"
                    android:orientation="horizontal">

                    <TextView
                        style="@style/TextAppearance.MaterialComponents.Body2"
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:layout_marginHorizontal="16dp"
                        android:layout_marginTop="8dp"
                        android:layout_weight="1"
                        android:text="Following"
                        android:textAlignment="center" />


                    <TextView
                        style="@style/TextAppearance.MaterialComponents.Body2"
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:layout_marginHorizontal="16dp"
                        android:layout_marginTop="8dp"
                        android:layout_weight="1"
                        android:text="Follower"
                        android:textAlignment="center" />

                </LinearLayout>

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginHorizontal="16dp"
                    android:layout_marginTop="4dp"
                    android:orientation="horizontal">

                    <TextView
                        android:id="@+id/tvFollowing"
                        style="@style/TextAppearance.MaterialComponents.Subtitle2"
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:layout_marginHorizontal="16dp"
                        android:layout_marginTop="8dp"
                        android:layout_weight="1"
                        android:textAlignment="center"
                        tools:text="John Doe" />


                    <TextView
                        android:id="@+id/tvFollower"
                        style="@style/TextAppearance.MaterialComponents.Subtitle2"
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:layout_marginHorizontal="16dp"
                        android:layout_marginTop="8dp"
                        android:layout_weight="1"
                        android:textAlignment="center"
                        tools:text="John Doe" />

                </LinearLayout>


                <com.google.android.material.divider.MaterialDivider
                    android:layout_width="match_parent"
                    android:layout_height="6dp"
                    android:layout_marginTop="16dp"
                    app:dividerColor="@color/design_default_color_primary" />

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginHorizontal="16dp"
                    android:layout_marginTop="16dp"
                    android:orientation="horizontal">

                    <TextView
                        style="@style/TextAppearance.MaterialComponents.Body2"
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:layout_marginHorizontal="16dp"
                        android:layout_marginTop="8dp"
                        android:layout_weight="1"
                        android:text="Repository"
                        android:textAlignment="center" />


                    <TextView
                        style="@style/TextAppearance.MaterialComponents.Body2"
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:layout_marginHorizontal="16dp"
                        android:layout_marginTop="8dp"
                        android:layout_weight="1"
                        android:text="Gists"
                        android:textAlignment="center" />

                </LinearLayout>

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginHorizontal="16dp"
                    android:layout_marginTop="4dp"
                    android:orientation="horizontal">

                    <TextView
                        android:id="@+id/tvRepos"
                        style="@style/TextAppearance.MaterialComponents.Subtitle2"
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:layout_marginHorizontal="16dp"
                        android:layout_marginTop="8dp"
                        android:layout_weight="1"
                        android:textAlignment="center"
                        tools:text="John Doe" />


                    <TextView
                        android:id="@+id/tvGists"
                        style="@style/TextAppearance.MaterialComponents.Subtitle2"
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:layout_marginHorizontal="16dp"
                        android:layout_marginTop="8dp"
                        android:layout_weight="1"
                        android:textAlignment="center"
                        tools:text="John Doe" />

                </LinearLayout>

                <com.google.android.material.divider.MaterialDivider
                    android:layout_width="match_parent"
                    android:layout_height="6dp"
                    android:layout_marginTop="16dp"
                    app:dividerColor="@color/design_default_color_primary" />


            </LinearLayout>

        </com.google.android.material.appbar.AppBarLayout>

        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/rvFollower"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:layout_behavior="@string/appbar_scrolling_view_behavior" />

    </androidx.coordinatorlayout.widget.CoordinatorLayout>

</LinearLayout>
0

There are 0 best solutions below