簡體   English   中英

Android:向上滾動時顯示工具欄(向上拖動),向下滾動時隱藏(向下拖動)

[英]Android: Show toolbar when scrolling UP(Drag UP) and hide when scrolling down(Drag down)

我有一個場景,其中一個Activity中的工具欄/操作欄具有與一般工具欄行為相反的行為。 片段內motion layout 向上滾動時它應該隱藏,並且motion layout 向下滾動時顯示 ,這與一般滾動行為相反。

我完全隱藏了Support Action Bar和工具欄布局,但它沒有任何動畫並且不好預兆,因為Activity包含一個BottomNavigation View,因此Action欄的常量隱藏和顯示效果不佳。

supportActionBar?.hide()
containerToolbar.visibility = View.GONE

AppBarLayout.LayoutParams scroll-flags顯然添加了一般行為。

fragment.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.motion.widget.MotionLayout
    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"
    app:layoutDescription="@xml/collapsing_header_arc"
    app:showPaths="false"
    android:background="@color/white"
    tools:showPaths="true">

    <com.github.florent37.shapeofview.shapes.ArcView
        android:id="@+id/header"
        android:layout_width="0dp"
        android:layout_height="200dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:shape_arc_height="26dp"
        android:background="@color/yellow_dark"
        app:shape_arc_position="bottom">


        <View
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@drawable/bg_actionbar_gradient" />
        <ScrollView
            android:id="@+id/scrollview_counts_container2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
            <include
                android:id="@+id/counts_container"
                layout="@layout/layout_card_count"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"/>
        </ScrollView>
    </com.github.florent37.shapeofview.shapes.ArcView>


    <View
        android:id="@+id/guideline_anchor"
        android:layout_width="wrap_content"
        android:layout_height="5dp"
        android:orientation="horizontal"
        android:background="@color/red_dark"
        app:layout_constraintTop_toBottomOf="@id/header"/>
    <View
        android:id="@+id/recyclerView"
        android:layout_width="0dp"
        android:layout_height="210dp"
        app:layout_constraintBottom_toBottomOf="@+id/guideline_anchor"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="@+id/guideline_anchor"
        android:background="@color/btnRedAlpha" />
    <androidx.core.widget.NestedScrollView
        android:id="@+id/parent_parent_dashboard"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="@+id/recyclerView"
        android:background="@color/transparent"
        android:layout_marginTop="@dimen/marginNormal">
        <androidx.constraintlayout.widget.ConstraintLayout
            android:id="@+id/parent_dashboard"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <ScrollView
                android:id="@+id/scrollview_counts_container"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                app:layout_constraintBottom_toTopOf="@+id/calendar_container"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent"
                app:layout_constraintVertical_chainStyle="packed"
                app:layout_constraintVertical_bias="0.0"
                android:nestedScrollingEnabled="true"
                android:visibility="gone">
                <include
                    android:id="@+id/counts_container"
                    layout="@layout/layout_card_count"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:clickable="true"/>
            </ScrollView>

            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:orientation="vertical"
                android:background="@color/white"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                android:layout_margin="@dimen/marginBig"
                app:layout_constraintTop_toBottomOf="@id/scrollview_counts_container"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintVertical_bias="0.0">
                <View
                    android:layout_width="match_parent"
                    android:layout_height="400dp"
                    android:background="@color/yellow_dark" />

                <View
                    android:layout_width="match_parent"
                    android:layout_height="400dp"
                    android:layout_marginTop="@dimen/marginBig"
                    android:background="@color/yellow_dark"/>

                <View
                    android:layout_width="match_parent"
                    android:layout_height="400dp"
                    android:layout_marginTop="@dimen/marginBig"
                    android:background="@color/yellow_dark"/>

            </LinearLayout>
        </androidx.constraintlayout.widget.ConstraintLayout>
    </androidx.core.widget.NestedScrollView>
    <!--<androidx.appcompat.widget.AppCompatTextView-->
        <!--android:id="@+id/headerText"-->
        <!--android:layout_width="wrap_content"-->
        <!--android:layout_height="wrap_content"-->
        <!--android:layout_gravity="center_vertical"-->
        <!--android:layout_marginLeft="23sp"-->
        <!--android:elevation="4dp"-->
        <!--android:gravity="center_vertical|left"-->
        <!--android:text="I love paris"-->
        <!--android:shadowColor="#3E3E3E"-->
        <!--android:shadowDx="2"-->
        <!--android:shadowDy="2"-->
        <!--android:shadowRadius="4"-->
        <!--android:textColor="@android:color/holo_blue_dark"/>-->

</androidx.constraintlayout.motion.widget.MotionLayout>

collapsing_header_arc.xml

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

    <Transition
        app:constraintSetEnd="@id/end"
        app:constraintSetStart="@id/start">

        <OnSwipe
            app:dragDirection="dragUp"
            app:touchAnchorId="@id/guideline_anchor"
            app:touchAnchorSide="top" />

    </Transition>

    <ConstraintSet android:id="@+id/start">
        <Constraint
            android:id="@id/header"
            android:layout_height="240dp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent">
            <CustomAttribute
                app:attributeName="arcHeightDp"
                app:customFloatValue="60" />
        </Constraint>

        <Constraint
            android:id="@id/scrollview_counts_container2">
            <CustomAttribute
                app:attributeName="visibility"
                app:customStringValue="visible" />
        </Constraint>
    </ConstraintSet>

    <ConstraintSet android:id="@+id/end">
        <Constraint
            android:id="@id/header"
            android:layout_height="0dp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent">
            <CustomAttribute
                app:attributeName="arcHeightDp"
                app:customFloatValue="0" />
        </Constraint>
        <Constraint
            android:id="@id/scrollview_counts_container2">
            <CustomAttribute
                app:attributeName="visibility"
                app:customStringValue="gone" />
        </Constraint>

    </ConstraintSet>

</MotionScene>

有沒有辦法實現這個?

您可以嘗試制作折疊工具欄並根據需要進行管理。 如果從片段管理活動工具欄時遇到問題,可以使用callback /或events /或observables在片段發生某些事件時調用活動動畫。 因此,例如,您在Activity中使用動畫方法查看類似於此處的視圖使用向上/向下滑動動畫顯示和隱藏視圖,然后從片段中調用它。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM