防止NestedScrollView下的片段中的工具栏滚动到屏幕之外

时间:2019-09-10 17:58:26

标签: android-toolbar android-coordinatorlayout android-appbarlayout android-nestedscrollview

我有一个包含工具栏的片段,该片段托管在NestedScrollView下,我希望该片段中的工具栏不滚动到屏幕之外。

我的MainActivity使用的布局如下:

<CoordinatorLayout>
    <AppBarLayout>
        <Toolbar/>
    </AppBarLayout>
    <NestedScrollView>
        <Fragment/>
    </NestedScrollView>
    <BottomNavigationView/>
</CoordinatorLayout>

我正在使用它来折叠此布局中的工具栏,一切正常。但是,我现在有一个要求,在我希望托管在NestedScrollView下面的一个片段中,包括一个附加的工具栏(该工具栏不会折叠,并且固定在屏幕顶部)。当我这样做时,工具栏将作为Fragment内容的一部分进行滚动(从逻辑上来说,这是我所期望的),但是我不确定如何解决该问题?

我不确定在这种情况下应该怎么做,如果有帮助的话,我目前在mainactivity布局中还有另一个工具栏,其中包含另一个用于宿主该工具栏的片段宿主。从理论上讲,我根本不需要工具栏,我只希望屏幕的一部分不会滚动到可以使用MotionLayout设置动画的顶部。

MainActivityLayout:

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

    <com.google.android.material.appbar.AppBarLayout
        android:id="@+id/appBar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">

        <androidx.appcompat.widget.Toolbar
            android:id="@+id/toolbar"
            app:contentInsetEnd="0dp"
            app:contentInsetLeft="0dp"
            app:contentInsetRight="0dp"
            app:contentInsetStart="0dp"
            app:contentInsetStartWithNavigation="0dp"
            app:layout_scrollFlags="scroll|enterAlways|snap"
            app:popupTheme="@style/AppTheme.PopupOverlay"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary" />

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

    <androidx.core.widget.NestedScrollView
        android:id="@+id/content_main_scroller"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:fillViewport="true">

        <fragment
            android:id="@+id/nav_host_fragment"
            android:name="androidx.navigation.fragment.NavHostFragment"
            app:defaultNavHost="true"
            app:navGraph="@navigation/navigation_main"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    </androidx.core.widget.NestedScrollView>

    <com.google.android.material.bottomnavigation.BottomNavigationView
        android:id="@+id/nav_view"
        app:itemIconSize="30dp"
        app:itemIconTint="@drawable/bottombar_selector"
        app:labelVisibilityMode="unlabeled"
        app:layout_behavior="@string/hide_bottom_view_on_scroll_behavior"
        app:menu="@menu/bottom_nav_menu"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        android:layout_marginStart="0dp"
        android:layout_marginEnd="0dp"
        android:background="@color/colorSecondaryLight" />

</androidx.coordinatorlayout.widget.CoordinatorLayout>

FragmentLayout:

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:card_view="http://schemas.android.com/apk/res-auto">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <com.google.android.material.appbar.AppBarLayout
            android:id="@+id/appBarProfile"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:theme="@style/AppTheme.AppBarOverlay"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent">

            <androidx.appcompat.widget.Toolbar
                android:id="@+id/toolbarfragment"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@color/colorSecondary"
                app:contentInsetEnd="0dp"
                app:contentInsetLeft="0dp"
                app:contentInsetRight="0dp"
                app:contentInsetStart="0dp"
                app:contentInsetStartWithNavigation="0dp"
                app:popupTheme="@style/AppTheme.PopupOverlay">


                <!-- removed to make easier reading... -->

            </androidx.appcompat.widget.Toolbar>

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

        <androidx.cardview.widget.CardView
            android:id="@+id/profile_friends_card"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            app:layout_constraintTop_toBottomOf="@id/appBarProfile"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            card_view:cardCornerRadius="0dp"
            card_view:contentPadding="@dimen/profile_page_padding"
            card_view:elevation="0dp">

            <!-- removed to make easier reading... -->

        </androidx.cardview.widget.CardView>

    <data>
        <variable
            name="ProfileViewModel"
            type="com.topper.topper.ui.profile.ProfileViewModel" />
    </data>
</layout>

0 个答案:

没有答案