📌  相关文章
📜  如何在Android中的Scroll上隐藏显示BottomNavigationView?(1)

📅  最后修改于: 2023-12-03 14:52:39.732000             🧑  作者: Mango

如何在Android中的Scroll上隐藏显示BottomNavigationView?

在Android应用程序中,有些时候需要在Scroll控件中隐藏和显示BottomNavigationView。本文将介绍如何实现此功能。

实现步骤
  1. 在布局文件中添加ScrollView和BottomNavigationView控件,ScrollView控件中添加需要滚动的控件。

    <?xml version="1.0" encoding="utf-8"?>
    <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <ScrollView
            android:id="@+id/scroll_view"
            android:layout_width="match_parent"
            android:layout_height="match_parent">
    
            <LinearLayout
                android:id="@+id/content_layout"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical">
    
                <!--Add your scrollable content here-->
    
                <!--Add your scrollable content here-->
    
                <!--Add your scrollable content here-->
    
            </LinearLayout>
    
        </ScrollView>
    
        <com.google.android.material.bottomnavigation.BottomNavigationView
            android:id="@+id/bottom_nav_view"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom"
            android:background="?android:attr/windowBackground"
            app:menu="@menu/bottom_nav_menu" />
    
    </FrameLayout>
    
  2. 创建一个OnScrollChangeListener监听器,当ScrollView的滚动状态改变时在底部导航栏上显示或隐藏。

    scrollView.setOnScrollChangeListener(new OnScrollChangeListener() {
        @Override
        public void onScrollChange(View v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {
            if (scrollY > oldScrollY) {
                // Scroll Down
                bottomNavigationView.setVisibility(View.GONE);
            }
            if (scrollY < oldScrollY) {
                // Scroll Up
                bottomNavigationView.setVisibility(View.VISIBLE);
            }
    
            if (scrollY == 0) {
                // Top of ScrollView
                bottomNavigationView.setVisibility(View.VISIBLE);
            }
    
            if (scrollY == (v.getChildAt(0).getMeasuredHeight() - v.getMeasuredHeight())) {
                // Bottom of ScrollView
                bottomNavigationView.setVisibility(View.VISIBLE);
            }
        }
    });
    

以上就是如何在Android中的Scroll上隐藏和显示BottomNavigationView的全部步骤。

完整代码
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ScrollView
        android:id="@+id/scroll_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <LinearLayout
            android:id="@+id/content_layout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <!--Add your scrollable content here-->

            <!--Add your scrollable content here-->

            <!--Add your scrollable content here-->

        </LinearLayout>

    </ScrollView>

    <com.google.android.material.bottomnavigation.BottomNavigationView
        android:id="@+id/bottom_nav_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        android:background="?android:attr/windowBackground"
        app:menu="@menu/bottom_nav_menu" />

</FrameLayout>
scrollView.setOnScrollChangeListener(new OnScrollChangeListener() {
    @Override
    public void onScrollChange(View v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {
        if (scrollY > oldScrollY) {
            // Scroll Down
            bottomNavigationView.setVisibility(View.GONE);
        }
        if (scrollY < oldScrollY) {
            // Scroll Up
            bottomNavigationView.setVisibility(View.VISIBLE);
        }

        if (scrollY == 0) {
            // Top of ScrollView
            bottomNavigationView.setVisibility(View.VISIBLE);
        }

        if (scrollY == (v.getChildAt(0).getMeasuredHeight() - v.getMeasuredHeight())) {
            // Bottom of ScrollView
            bottomNavigationView.setVisibility(View.VISIBLE);
        }
    }
});