【问题标题】:I have a Collapsing toolbar and nested scroll view, I cant make it work我有一个折叠工具栏和嵌套滚动视图,我不能让它工作
【发布时间】:2021-07-12 06:12:08
【问题描述】:
  <?xml version="1.0" encoding="utf-8"?>

<androidx.drawerlayout.widget.DrawerLayout 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:id="@+id/drawerLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".activity.RestaurantMenuActivity">


    <androidx.coordinatorlayout.widget.CoordinatorLayout
        android:id="@+id/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="50dp"
            android:elevation="0dp"
            android:theme="@style/ThemeOverlay.AppCompat.Dark">

            <androidx.appcompat.widget.Toolbar
                android:id="@+id/toolMenuBar"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@color/red"
                android:minHeight="?attr/actionBarSize"
                android:theme="@style/ThemeOverlay.AppCompat.Dark" />
        </com.google.android.material.appbar.AppBarLayout>
        <com.google.android.material.appbar.CollapsingToolbarLayout
            android:layout_width="match_parent"
            android:layout_height="200dp"
            android:layout_marginTop="50dp"
            app:layout_scrollFlags="scroll|enterAlways|enterAlwaysCollapsed"
            >
            <ImageView
                android:id="@+id/rest_image"
                android:layout_width="match_parent"
                android:layout_height="200dp"
                android:src="@drawable/restaurant_image"
                />
        </com.google.android.material.appbar.CollapsingToolbarLayout>


            <androidx.core.widget.NestedScrollView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_marginTop="250dp"
                app:layout_behavior="@string/appbar_scrolling_view_behavior">
                <RelativeLayout
                    android:layout_width="match_parent"
                    android:layout_height="450dp">

            <androidx.recyclerview.widget.RecyclerView
                android:id="@+id/recyclerViewRestaurantMenu"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:nestedScrollingEnabled="false"
                 />

            <RelativeLayout
                android:id="@+id/relativeLayoutProceedToCart"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="bottom"
                android:layout_alignParentBottom="true"
                android:visibility="invisible">

                <Button
                    android:id="@+id/btnProceedToCart"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:background="@color/red"
                    android:text="Proceed to Cart"
                    android:textColor="@color/white" />

            </RelativeLayout>
    </RelativeLayout>
            </androidx.core.widget.NestedScrollView>

    </androidx.coordinatorlayout.widget.CoordinatorLayout>


    <RelativeLayout
        android:id="@+id/progressMenu"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/white">

        <ProgressBar
            android:theme="@style/CircularProgress"
            android:id="@+id/progressBar"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true" />


    </RelativeLayout>
</androidx.drawerlayout.widget.DrawerLayout>

这是 Xml 文件,我有一个折叠工具栏和一个嵌套滚动视图,但它不起作用,当我滚动页面时,折叠工具栏不起作用。 我在折叠工具栏中有一个图像,在嵌套滚动视图中有一个回收器视图,当我滚动回收视图时,图像视图应该折叠。但这并没有发生,而且表现得就像正常的事情。

【问题讨论】:

    标签: android xml android-studio kotlin


    【解决方案1】:

    将回收器视图作为嵌套滚动视图的唯一子级是一种不好的做法,因为您实际上以这种方式剥夺了回收器视图的一个强大方面。而是让布局的其余部分也成为嵌套布局的一部分,并制作回收站视图android:nestedScrollingEnabled=false

    然后将app:layout_behavior="@string/appbar_scrolling_view_behavior" 添加到嵌套滚动视图中。

    编辑:-

    尝试在 collapsingToolbar 中添加工具栏

    app:layout_collapseMode="pin"
    app:layout_scrollFlags="scroll|enterAlways"
    

    由于折叠工具栏是一种框架布局,您可能需要调整一些边距以使其看起来像您现在的样子

    【讨论】:

    • 我用最新更改编辑了 qn,但它仍然无法正常工作 @RahulRawat
    • 将折叠工具栏布局放入应用栏布局
    • 是的。它正在滚动而不折叠
    • 你能不能在将折叠工具栏放在 appbarlayout 中时尝试删除工具栏,看看是否可行
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-19
    • 1970-01-01
    • 1970-01-01
    • 2017-09-27
    • 1970-01-01
    相关资源
    最近更新 更多