问题
I've been trying to animate some BottomNavigationView depdending on com.sothree.slidinguppanel.SlidingUpPanelLayout mouvements. here is my layout :
<?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>
I have also been trying to incorporate a CoordinatorLayout as top view together with BottomNavigationViewBehavior extending CoordinatorLayout.Behavior but without success.
I have also been tryin to listen to events on the panel (slidingUpPanelLayout.setOnScrollChangeListener & slidingUpPanelLayout.setOnDragListener) but strangly they never get called.
I am using the following versions :
- android support lib 25.3.1
- com.sothree.slidinguppanel:library:3.3.0
The idea is to have the exact same behaviour of the bottom bar as in soundcloud app. (and soundcloud is using com.sothree.slidinguppanel lib).
Thank you.
回答1:
I finally used BottomSheetBehavior + its callback to animate the BotomNavigationBar. here is the layout :
<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>
and the related code: I had the possibility to use a customBehaviour extending BottomSheetBehavior or simply listen to BottomSheetCallback, i went for the 2nd solution:
callback :
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);
}
});
smooth animation (the BottomNavigationBar goes down while scrolling the BottomSheet up and vice versa):
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();
}
来源:https://stackoverflow.com/questions/51965107/android-bottomnavigationview-react-to-slidinguppanellayout-mouvements