android - 需要为某些 fragment 禁用 CollapsingToolbarLayout 上的展开

标签 android android-support-library android-toolbar android-appbarlayout coordinator-layout

我有一个控制替换许多 fragment 的 AppCompatActivity。这是我的布局。

activity_main.xml

<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/drawer_layout"
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    android:fitsSystemWindows="true">

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

    <android.support.design.widget.NavigationView
        android:id="@+id/navigation_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:fitsSystemWindows="true"
        android:background="@color/white"
        app:headerLayout="@layout/drawer_header"
        app:menu="@menu/drawer"/>

</android.support.v4.widget.DrawerLayout>

activity_main_frame.xml

<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="wrap_content"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
        android:fitsSystemWindows="true">

        <android.support.design.widget.CollapsingToolbarLayout
            android:id="@+id/collapsing_toolbar"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_scrollFlags="scroll|exitUntilCollapsed"
            android:fitsSystemWindows="true"
            app:contentScrim="?attr/colorPrimary"
            app:expandedTitleMarginStart="48dp"
            app:expandedTitleMarginEnd="64dp">

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

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

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

            <android.support.design.widget.TabLayout
                android:id="@+id/tabs"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:visibility="gone"
                android:layout_gravity="bottom"/>

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

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

    <FrameLayout
        android:id="@+id/content_frame"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" >
    </FrameLayout>

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab1"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        app:layout_anchor="@id/appbar"
        app:layout_anchorGravity="bottom|right|end"
        app:borderWidth="0dp"
        android:src="@drawable/app_ic_slide_wallpaper_dark"
        android:layout_margin="@dimen/big_padding"
        android:clickable="true"/>

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

我的主页 fragment 最初是设置的,这就是我希望折叠工具栏展开的地方,并且工作正常。但是,当我从侧抽屉更改 fragment 时,我想禁用扩展工具栏。

我已经知道如何在选择抽屉项目时折叠它,但我还需要确保它不会展开,除非显示主页 fragment 。这可能吗?

public void collapseToolbar(){
        CoordinatorLayout.LayoutParams params = (CoordinatorLayout.LayoutParams) appbar.getLayoutParams();
        behavior = (AppBarLayout.Behavior) params.getBehavior();
        if(behavior!=null) {
            behavior.onNestedFling(coordinator, appbar, null, 0, 10000, true);
        }
    }

最佳答案

禁用滚动 fragment 内容的嵌套滚动:

recyclerView.setNestedScrollingEnabled(false);

如果您正在使用支持库,请使用它:

ViewCompat.setNestedScrollingEnabled(recyclerView, false);

关于android - 需要为某些 fragment 禁用 CollapsingToolbarLayout 上的展开,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30779123/

相关文章:

java - 如何在 Adob​​e Reader 中从 Android 应用程序打开服务器上的 pdf

android - Android按下按钮可长时间播放声音

android - 防止用户在 BottomSheet 支持库 23.2 上拖动

android - 如何在 Lollipop 之前的 Android 版本中更改工具栏的标题颜色

android - 如何使用亚马逊等搜索框制作滚动工具栏

android - 自定义工具栏中的溢出菜单图标

android - 如何使用 android googledrive sdk 将文件上传到 googledrive 文件夹

android - 如何在 build.gradle 中为版本指定通用字符串?

android - 为什么 fragment 与操作栏重叠?

安卓 : Multi touch and TYPE_SYSTEM_OVERLAY