如何在CollapsingToolbarLayout中使用RecyclerView。当达到底部时,RecyclerView需要加载更多数据

时间:2018-12-31 06:24:07

标签: android android-recyclerview android-collapsingtoolbarlayout

我的问题是,我想将RecyclerViewCollapsingToolbarLayout一起使用。但是问题是CollapsingToolbarLayout滚动时RecyclerView没有滚动。我无法使用嵌套滚动将RecyclerView放进去,因为我想将更多数据加载到RecyclerView

    

<android.support.design.widget.AppBarLayout
    android:id="@+id/collapsing_toolbar_appbarlayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="?themeColorBack">

    <android.support.design.widget.CollapsingToolbarLayout
        android:id="@+id/collapsing_toolbar_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
        app:contentScrim="?themeColorBack"
        app:layout_scrollFlags="scroll|exitUntilCollapsed">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <RelativeLayout
                android:id="@+id/image_contain_relative"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:visibility="invisible">

                <android.support.constraint.ConstraintLayout
                    android:id="@+id/first_image_container"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content">

                    <ImageView
                        android:id="@+id/first_post"
                        android:layout_width="0dp"
                        android:layout_height="0dp"
                        android:layout_marginBottom="10dp"
                        android:adjustViewBounds="true"
                        android:contentDescription="@string/latest_post"
                        android:scaleType="fitXY"
                        app:layout_constraintDimensionRatio="H,16:9"
                        app:layout_constraintEnd_toEndOf="parent"
                        app:layout_constraintStart_toStartOf="parent"
                        app:layout_constraintTop_toTopOf="parent" />
                </android.support.constraint.ConstraintLayout>

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_alignBottom="@+id/first_image_container"
                    android:background="#bb000000"
                    android:orientation="vertical">

                    <TextView
                        android:id="@+id/first_image_category"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginTop="4dp"
                        android:paddingLeft="8dp"
                        android:paddingStart="8dp"
                        android:textColor="@color/white"
                        android:textSize="14sp"
                        tools:ignore="RtlSymmetry" />

                    <TextView
                        android:id="@+id/first_image_date"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginBottom="4dp"
                        android:paddingLeft="8dp"
                        android:paddingStart="8dp"
                        android:textColor="@color/white"
                        android:textSize="14sp"
                        tools:ignore="RtlSymmetry" />
                </LinearLayout>
            </RelativeLayout>

            <TextView
                android:id="@+id/first_post_title"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:padding="8dp"
                android:textColor="?themeHeadColorText"
                android:textSize="16sp"
                android:textStyle="bold" />
        </LinearLayout>

    </android.support.design.widget.CollapsingToolbarLayout>


</android.support.design.widget.AppBarLayout>

<!-- app:layout_behavior attribute value settings make app tool bar appear while RecyclerView scroll. -->
<android.support.v7.widget.RecyclerView
    android:id="@+id/post_recycle"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginBottom="60dp"
    app:layout_behavior="@string/appbar_scrolling_view_behavior" />

2 个答案:

答案 0 :(得分:1)

您可以这样使用。

<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/myCoordinatorLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true">

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

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

        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/recyclerView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_margin="4dp" />
    </LinearLayout>

    <com.google.android.material.floatingactionbutton.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="end|bottom"
        android:layout_margin="18dp"
        android:src="@drawable/ic_add" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>

答案 1 :(得分:1)

尝试这样,希望它能完全满足您的要求。

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/main_content"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true">

<android.support.design.widget.AppBarLayout
    android:id="@+id/appbar"
    android:layout_width="match_parent"
    android:layout_height="250dp"
    android:fitsSystemWindows="true"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

    <android.support.design.widget.CollapsingToolbarLayout
        android:id="@+id/collapsing_toolbar"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true"
        app:contentScrim="@android:color/transparent"
        app:layout_scrollFlags="scroll|exitUntilCollapsed"
        app:statusBarScrim="@android:color/transparent">

        <ImageView
            android:id="@+id/ivFeature"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:fitsSystemWindows="true"
            android:scaleType="centerCrop"
            app:layout_collapseMode="parallax" />

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="@android:color/transparent"
            app:layout_collapseMode="pin"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />

    </android.support.design.widget.CollapsingToolbarLayout>

</android.support.design.widget.AppBarLayout>

<android.support.v4.widget.NestedScrollView
    android:id="@+id/nestedScrollView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:overScrollMode="never"
    app:layout_behavior="@string/appbar_scrolling_view_behavior">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <android.support.v7.widget.RecyclerView
            android:id="@+id/card_recycler_view"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:nestedScrollingEnabled="false" />

    </LinearLayout>
</android.support.v4.widget.NestedScrollView>