Android:向上滚动时显示工具栏(向上拖动),向下滚动时隐藏工具栏(向下拖动)

时间:2019-04-24 20:19:13

标签: android android-actionbar android-toolbar android-motionlayout

我遇到一种情况,即活动中的工具栏/操作栏的行为与常规工具栏的行为相反。当片段中的 motion layout滚动 UP 时应隐藏,而当motion layout滚动 DOWN 时显示。 em> ,这与常规滚动行为相反。

我试图完全隐藏Support Action Bar和工具栏布局,但是它没有任何动画并且预示效果不好,因为“活动”包含一个BottomNavigation视图,因此不断隐藏和显示“动作”栏的效果不佳

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>

有没有办法实现这一目标?

1 个答案:

答案 0 :(得分:0)

您可以尝试制作折叠工具栏并根据需要进行管理。如果您无法从片段管理活动工具栏,则可以在片段发生事件时使用回调/或事件/或可观察对象来调用活动动画。因此,例如,您在“活动”中有一个使用动画方法的视图,如此处Show and hide a View with a slide up/down animation,只需从片段中调用它即可。