让无尽的 RecyclerView 内容包裹在 CoordinatorLayout 中

问题描述 投票:0回答:0

我想显示一个工具栏、详细布局和带有分页/无限列表的回收器视图。我最终使用 CoordinatorLayout 来包含细节布局和 RecyclerView 在里面,ReyclerView 有 appbar 滚动视图行为。

无限滚动可以,但是当列表内容少的时候,还是会占满整个屏幕。将 ReyclerView 高度设置为

wrap_content
只会缩小滚动区域。我希望 RecyclerView 只占用必要的空间。我该怎么做?

(我的代码在截图之后)

期望:

现实:

布局代码,或者你可以访问它在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>
android android-layout android-recyclerview view android-coordinatorlayout
© www.soinside.com 2019 - 2024. All rights reserved.