Android:Recyclerview 滚动并出现折叠工具栏问题

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

在我的应用程序中,我使用 CollapsingtoolbarLayout 和 RecyclerView。两人都在工作。但我在滚动它们时遇到问题。我想要的是,每当我将 RecyclerView 直接滚动到折叠工具栏下方时,我希望 RecyclerView 在折叠时与工具栏一起上升。但每当我首先滚动 recyclerview 时,这种情况就不会发生,它会在 corousal 后面,然后在某个时候 corousal 滚动。但是我的代码无法按我的预期工作。

以下是我的代码:

<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout 
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:fitsSystemWindows="true">


<com.google.android.material.appbar.AppBarLayout
    android:id="@+id/appBarLayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:fitsSystemWindows="true"
    app:layout_behavior="com.google.android.material.appbar.AppBarLayout$Behavior">

    <com.google.android.material.appbar.CollapsingToolbarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/black_theme"
        app:layout_scrollFlags="scroll|exitUntilCollapsed"
        app:title="@string/app_name">

        <androidx.constraintlayout.widget.ConstraintLayout
            android:id="@+id/corousal_view"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent">

            <include
                android:id="@+id/layout_sticky_home"
                layout="@layout/item_sticky_header_rt" />

            <androidx.viewpager2.widget.ViewPager2
                android:id="@+id/rvcarousal"
                android:layout_width="0dp"
                android:layout_height="0dp"
                android:orientation="horizontal"
                android:visibility="visible"
                app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
                app:layout_constraintDimensionRatio="h,3:4"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@id/layout_sticky_home" />

            <androidx.appcompat.widget.AppCompatImageView
                android:layout_width="0dp"
                android:layout_height="80dp"
                android:layout_marginBottom="-8dp"
                android:background="@drawable/bg_gradient"
                app:layout_constraintBottom_toBottomOf="@+id/linlay_pager"
                app:layout_constraintEnd_toEndOf="@id/rv_carousal"
                app:layout_constraintStart_toStartOf="@id/rv_carousal" />

            <LinearLayout
                android:id="@+id/linlay_pager"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_below="@+id/rv_carousal"
                android:gravity="center_horizontal"
                android:orientation="horizontal"
                app:layout_constraintBottom_toBottomOf="@id/rv_carousal"
                app:layout_constraintEnd_toEndOf="@id/rv_carousal"
                app:layout_constraintStart_toStartOf="@id/rv_carousal" />

        </androidx.constraintlayout.widget.ConstraintLayout>


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

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

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior">

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/rv"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="16dp"
        android:layout_marginBottom="@dimen/_20sdp"
        android:clipChildren="false"
        android:orientation="vertical"
        app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager" />
</RelativeLayout>

我尝试了很多解决方案,但大多数情况下我在SO的答案中发现将@string/appbar_scrolling_view_behavior赋予recyclerview,但它不起作用

android kotlin android-layout android-recyclerview android-collapsingtoolbarlayout
1个回答
0
投票

我理解这个问题,我可以建议你一些解决方案,你可以尝试一下。

1。嵌套滚动:

  • 您可以在 RecyclerView 中禁用嵌套滚动并在 RecyclerView 上设置
    android:nestedScrollingEnabled="false"
    。这可能会阻止 RecyclerView 干扰 CollapsingToolbarLayout 的滚动行为,并导致触摸事件无法正确处理。

2。 AppBarLayout配置:

  • 设置 app:layout_scrollFlags 属性:在 CollapsingToolbarLayout 上使用
    app:layout_scrollFlags="scroll|exitUntilCollapsed"
    。这确保 RecyclerView 在折叠时与 CollapsingToolbarLayout 一起滚动。

希望有帮助。

© www.soinside.com 2019 - 2024. All rights reserved.