BottomNavigationView hides when scrolling up instead of down

本小妞迷上赌 提交于 2019-12-08 00:25:45

问题


The new BottomNavigationView from support library v25.0.0 is supposed to hide when scrolling down, in order to see all the items from a list. However, in my testing scenario, the view hides when scrolling up. Any ideas what can cause this reverse behavior?

The inner_fragment is set up as a Fragment inserted inside the activity_main_framelayout_content Framelayout. XML layouts below:

main_activity.xml:

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

    <android.support.design.widget.AppBarLayout
            android:id="@+id/activity_main_appbarlayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

        <android.support.v7.widget.Toolbar
                android:id="@+id/activity_main_toolbar"
                android:layout_height="?attr/actionBarSize"
                android:layout_width="match_parent"
                android:background="?attr/colorPrimary"
                app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
                app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
                app:layout_scrollFlags="scroll|enterAlways">

            <include layout="@layout/activity_main_spinner_layout"/>
        </android.support.v7.widget.Toolbar>
    </android.support.design.widget.AppBarLayout>

    <FrameLayout
            android:id="@+id/activity_main_framelayout_content"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_behavior="@string/appbar_scrolling_view_behavior"
            android:fitsSystemWindows="true"/>
</android.support.design.widget.CoordinatorLayout>

<android.support.design.widget.NavigationView
        android:id="@+id/activity_main_framelayout_navigation_drawer"
        android:layout_width="@dimen/drawer_width"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:fitsSystemWindows="true"
        android:background="@color/color_black_700"/>

inner_fragment.xml:

<FrameLayout 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/inner_fragment_framelayout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>

    <android.support.design.widget.BottomNavigationView
            android:id="@+id/inner_fragment_bottom_navigation_view"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom"
            app:menu="@menu/inner_fragment"
            app:itemBackground="@drawable/bg_bottom_navigation"
            app:itemIconTint="@color/ic_bottom_navigation"
            app:itemTextColor="@color/ic_bottom_navigation"/>
</FrameLayout>

回答1:


This release of BottomNavigationView is missing scrolling behavior to work out of the box as specified in the guidelines.

I wrote an article on what's missing and how you can fix it. This includes implementing scrolling behavior of the BottomNavigationView in CoordinatorLayout.




回答2:


A simple solution is to just add an offset listener to appbarlayout. Works perfectly for me.

So something like this:

((AppBarLayout)toolbar.getParent()).addOnOffsetChangedListener(new AppBarLayout.OnOffsetChangedListener() {
    @Override
    public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {
        mNavigationBar.setTranslationY(verticalOffset*-1);
    }
});



回答3:


My solution was to replace the FrameLayout with a NestedCoordinatorLayout from here https://stackoverflow.com/a/37660246/2233621 and then add the BottomNavigationBehavior from Nikola's blog post https://medium.com/@nullthemall/bottomnavigationview-missing-pearls-eaa950f9ad4e#.p1i01wwui that way your bottom navigation behaviour can listen for nested scrolling of the fragment inside the NestedCoordinatorLayout

I believe you could use another view that implements NestedScrollParent + NestedScrollChild for the same behaviour.



来源:https://stackoverflow.com/questions/40171801/bottomnavigationview-hides-when-scrolling-up-instead-of-down

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!