Android BottomNavigationView 对 SlidingUpPanelLayout 运动作出反应

Android BottomNavigationView react to SlidingUpPanelLayout mouvements

我一直在尝试根据 com.sothree.slidinguppanel.SlidingUpPanelLayout 运动为一些 BottomNavigationView 制作动画。 这是我的布局:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    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">
    <com.sothree.slidinguppanel.SlidingUpPanelLayout
        android:id="@+id/slidingUpPanel"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context="com.test.activity.MainActivity"
        tools:showIn="@layout/activity_main"
        android:gravity="bottom"
        android:layout_above="@+id/bottom_nav_view"
        app:umanoPanelHeight="50dp">
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">
            <FrameLayout
                android:id="@+id/fragment_container"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_marginLeft="@dimen/activity_horizontal_margin"
                android:layout_marginRight="@dimen/activity_horizontal_margin"/>
        </RelativeLayout>
        <fragment
            android:id="@+id/invitationFragment"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:name="com.test.fragment.CustomListFragment" />
    </com.sothree.slidinguppanel.SlidingUpPanelLayout>
    <android.support.design.widget.BottomNavigationView
        android:id="@+id/bottom_nav_view"
        android:layout_width="match_parent"
        android:layout_height="56dp"
        android:layout_alignParentBottom="true"
        app:itemIconTint="@drawable/item_bottom_bar_bg"
        app:itemTextColor="@drawable/item_bottom_bar_bg"
        app:elevation="0dp"
        app:menu="@menu/bottom_navigation" />
</RelativeLayout>

我也一直在尝试将 CoordinatorLayout 作为顶视图与扩展 CoordinatorLayout.Behavior 的 BottomNavigationViewBehavior 合并,但没有成功。

我也一直在尝试监听面板上的事件 (slidingUpPanelLayout.setOnScrollChangeListener & slidingUpPanelLayout.setOnDragListener) 但奇怪的是他们从来没有被调用过。

我正在使用以下版本:

我们的想法是让底栏的行为与 soundcloud 应用程序中的行为完全相同。 (并且 soundcloud 正在使用 com.sothree.slidinguppanel 库)。

谢谢。

我终于使用 BottomSheetBehavior 及其回调来为 BotomNavigationBar 设置动画。 这是布局:

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <FrameLayout
        android:id="@+id/fragment_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginLeft="@dimen/activity_horizontal_margin"
        android:layout_marginRight="@dimen/activity_horizontal_margin">
    </FrameLayout>

    <android.support.design.widget.CoordinatorLayout
        android:id="@+id/coordinator_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <LinearLayout
            android:background="@color/light_grey"
            android:id="@+id/bottom_sheet"
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:fitsSystemWindows="true"
            app:layout_behavior="android.support.design.widget.BottomSheetBehavior"
            app:behavior_hideable="true"
            app:behavior_peekHeight="120dp">
                <fragment
                    android:id="@+id/invitationFragment"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:name="com.test.view.fragment.InvitationListFragment" />
        </LinearLayout>

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

    <android.support.design.widget.BottomNavigationView
        android:background="@color/white"
        android:id="@+id/bottom_nav_view"
        android:layout_width="match_parent"
        android:layout_height="56dp"
        android:layout_alignParentBottom="true"
        app:itemIconTint="@drawable/item_bottom_bar_bg"
        app:itemTextColor="@drawable/item_bottom_bar_bg"
        app:elevation="0dp"
        app:menu="@menu/bottom_navigation"
        android:visibility="visible"/>
</RelativeLayout>

及相关代码: 我有可能使用扩展 BottomSheetBehavior 的 customBehaviour 或简单地听 BottomSheetCallback,我选择了第二个解决方案:

回调:

        bottomSheetBehavior.setBottomSheetCallback(new BottomSheetBehavior.BottomSheetCallback() {
        @Override
        public void onStateChanged(@NonNull View bottomSheet, int newState) {
        }

        @Override
        public void onSlide(@NonNull View bottomSheet, float slideOffset) {
            slideDown(bottomNavigationView,slideOffset);
        }
    });

流畅的动画(向上滚动 BottomSheet 时 BottomNavigationBar 下降,反之亦然):

    private void slideDown(BottomNavigationView child, float slideOffset) {
    float heigh_to_animate = slideOffset * child.getHeight();
    ViewPropertyAnimator animator = child.animate();
    animator
            .translationY(heigh_to_animate)
            .setInterpolator(new DecelerateInterpolator())
            .setDuration(0)
            .start();
}