在Android中滚动“突出的顶部应用程序栏”的应用程序中的延迟滚动行为

nfs0ujit  于 2023-04-10  发布在  Android
关注(0)|答案(1)|浏览(138)

当前非预期行为:

所需的滚动行为:

我在一个AndroidKotlin应用程序中遇到了一些意外的滚动行为,滚动“突出的顶部应用程序栏”。所需的行为是针对内部片段的整个内容(布局定义在fragment_trains. xml中),以便用户在屏幕的该部分上向下滚动时立即滚动。突出的顶部应用栏立即按预期开始滚动。意外的滚动行为是内部片段内容的滚动有延迟。它们直到突出的顶部应用栏完成滚动并达到其“压缩”较小的尺寸才开始滚动。如何解决这个问题?我尝试过的解决方案包括在布局的各个部分添加焦点标签,但没有找到与此问题相关的任何其他答案。和显示问题的屏幕记录。内部片段应该立即开始滚动,而不是延迟到“突出的顶部应用程序栏”已经停留在它的较小的位置。即使它不可能提供一个解决方案来直接解决这个问题,我还没有能够找到这样的应用程序布局的例子与材料设计与滚动“突出的顶部应用程序栏”和底部导航栏和一个片段之间的滚动行为。谢谢。

activity_main.xml:

<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

<fragment
    android:id="@+id/nav_host_fragment"
    android:name="androidx.navigation.fragment.NavHostFragment"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:defaultNavHost="true"
    app:layout_constraintBottom_toTopOf="@id/nav_view"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/appbar"
    app:navGraph="@navigation/mobile_navigation" />

    <com.google.android.material.appbar.AppBarLayout
    android:id="@+id/appbar"
    android:layout_width="match_parent"
    android:layout_height="128dp"
    app:liftOnScroll="true"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    android:focusable="false"
    app:liftOnScrollTargetViewId="@id/nav_host_fragment">

<com.google.android.material.appbar.CollapsingToolbarLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:expandedTitleMarginStart="28dp"
    app:expandedTitleMarginBottom="28dp"
    app:layout_scrollFlags="scroll|exitUntilCollapsed|snap"
    app:contentScrim="?attr/colorPrimary"
    app:statusBarScrim="?attr/colorPrimaryVariant"
            app:expandedTitleTextAppearance="@style/Trains.TextAppearance.MaterialComponents.Headline6"
            app:collapsedTitleTextAppearance="@style/Trains.TextAppearance.MaterialComponents.Headline6">

<com.google.android.material.appbar.MaterialToolbar
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    app:layout_scrollFlags="scroll|enterAlways|snap"
    android:minHeight="80dp"
    android:elevation="0dp"
    android:title="@string/app_name"
    app:layout_collapseMode="pin"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:menu="@menu/top_app_bar"
    style="@style/Widget.MaterialComponents.Toolbar.Primary"/>

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

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

<com.google.android.material.bottomnavigation.BottomNavigationView
    android:id="@+id/nav_view"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/colorPrimary"
    app:itemIconTint="@color/bnv_tab_item_foreground"
    app:itemTextColor="@color/bnv_tab_item_foreground"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    android:layout_gravity="bottom"
    app:menu="@menu/bottom_nav_menu" />

</androidx.coordinatorlayout.widget.CoordinatorLayout>

fragment_trains.xml:

<androidx.core.widget.NestedScrollView xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:android="http://schemas.android.com/apk/res/android"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_constraintBottom_toTopOf="@+id/nav_view"
    android:background="@color/colorBackground">

<LinearLayout
    android:id="@+id/temp"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginTop="128dp"
    android:orientation="vertical">
    
<!-- Travel info section -->

<TextView

    android:id="@+id/text_travelinfoheader"
    style="@style/TrainMediumCaption"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginStart="29dp"
    android:layout_marginLeft="29dp"
    android:layout_marginTop="29dp"
    android:layout_marginEnd="29dp"
    android:layout_marginRight="29dp"
    android:gravity="start"
    android:textAlignment="gravity"
    app:layout_constraintEnd_toEndOf="parent"

    />

<com.google.android.material.card.MaterialCardView
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:id="@+id/travelinfocard"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="9dp"
    android:layout_marginBottom="9dp"
    android:layout_marginLeft="12dp"
    android:layout_marginRight="12dp"
    card_view:cardCornerRadius="4dp"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:cardElevation="1dp"
    app:layout_constraintTop_toBottomOf="@+id/text_travelinfoheader">

<TextView
    android:id="@+id/text_travelinfo"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_margin="19dp" />

</com.google.android.material.card.MaterialCardView>

<!-- Next trains section -->

<TextView
    android:id="@+id/text_nexttrainheader"
    style="@style/TrainMediumCaption"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginStart="29dp"
    android:layout_marginLeft="29dp"
    android:layout_marginTop="9dp"
    android:layout_marginEnd="29dp"
    android:layout_marginRight="29dp"
    android:gravity="start"
    android:textAlignment="gravity"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/travelinfocard" />

<androidx.recyclerview.widget.RecyclerView
    android:id="@+id/nextrains_recyclerview"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="9dp"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/text_nexttrainheader" />

</LinearLayout>
</androidx.core.widget.NestedScrollView>
mspsb9vt

mspsb9vt1#

要解决此问题,使用滚动行为,此代码:

app:layout_behavior="@string/appbar_scrolling_view_behavior"

需要从fragment_trains.xml文件中的NestedScrollView元素移动到activity_main.xml文件中的fragment元素。以下是解决此问题的新xml布局文件:
新建activity_main.xml:

<?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"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <com.google.android.material.appbar.AppBarLayout
        android:id="@+id/appbar"
        android:layout_width="match_parent"
        android:layout_height="128dp"
        app:liftOnScroll="true"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        android:focusable="false"
        app:liftOnScrollTargetViewId="@id/nav_host_fragment"
        >

        <com.google.android.material.appbar.CollapsingToolbarLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:expandedTitleMarginStart="28dp"
            app:expandedTitleMarginBottom="28dp"
            app:layout_scrollFlags="scroll|exitUntilCollapsed|snap"
            app:contentScrim="?attr/colorPrimary"
            app:statusBarScrim="?attr/colorPrimaryVariant"
            app:expandedTitleTextAppearance="@style/Train.TextAppearance.MaterialComponents.Headline6"
            app:collapsedTitleTextAppearance="@style/Train.TextAppearance.MaterialComponents.Headline6">

            <com.google.android.material.appbar.MaterialToolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                app:layout_scrollFlags="scroll|enterAlways|snap"
                android:minHeight="80dp"
                android:elevation="0dp"
                android:title="@string/app_name"
                app:layout_collapseMode="pin"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintRight_toRightOf="parent"
                app:menu="@menu/top_app_bar"
                style="@style/Widget.MaterialComponents.Toolbar.Primary"
                />

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

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

    <fragment
        android:id="@+id/nav_host_fragment"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        android:name="androidx.navigation.fragment.NavHostFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:defaultNavHost="true"
        android:layout_marginBottom="110dp"
        app:navGraph="@navigation/mobile_navigation" />

    <com.google.android.material.bottomnavigation.BottomNavigationView
        android:id="@+id/nav_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/colorPrimary"
        app:itemIconTint="@color/bnv_tab_item_foreground"
        app:itemTextColor="@color/bnv_tab_item_foreground"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        android:layout_gravity="bottom"
        app:menu="@menu/bottom_nav_menu" />

</androidx.coordinatorlayout.widget.CoordinatorLayout>

新建fragment_trains.xml:

<?xml version="1.0" encoding="utf-8"?>

<androidx.core.widget.NestedScrollView xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/colorBackground">

        <LinearLayout
            android:id="@+id/temp"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">
        <!-- Travel info section -->

        <TextView

            android:id="@+id/text_travelinfoheader"
            style="@style/TrainMediumCaption"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginStart="29dp"
            android:layout_marginLeft="29dp"
            android:layout_marginTop="29dp"
            android:layout_marginEnd="29dp"
            android:layout_marginRight="29dp"
            android:gravity="start"
            android:textAlignment="gravity"
            app:layout_constraintEnd_toEndOf="parent"

             />

        <com.google.android.material.card.MaterialCardView
            xmlns:card_view="http://schemas.android.com/apk/res-auto"
            android:id="@+id/travelinfocard"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="9dp"
            android:layout_marginBottom="9dp"
            android:layout_marginLeft="12dp"
            android:layout_marginRight="12dp"
            card_view:cardCornerRadius="4dp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:cardElevation="1dp"
            app:layout_constraintTop_toBottomOf="@+id/text_travelinfoheader">

            <TextView
                    android:id="@+id/text_travelinfo"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_margin="19dp" />

        </com.google.android.material.card.MaterialCardView>

        <!-- Next trains section -->

        <TextView
            android:id="@+id/text_nexttrainheader"
            style="@style/TrainMediumCaption"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginStart="29dp"
            android:layout_marginLeft="29dp"
            android:layout_marginTop="9dp"
            android:layout_marginEnd="29dp"
            android:layout_marginRight="29dp"
            android:gravity="start"
            android:textAlignment="gravity"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/travelinfocard" />

        <androidx.recyclerview.widget.RecyclerView

            android:id="@+id/nextrains_recyclerview"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="9dp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/text_nexttrainheader" />

        </LinearLayout>

    </androidx.core.widget.NestedScrollView>

以下是正确滚动行为的结果:

相关问题