【问题标题】:Hide Bottom Navigation On Scroll Up Show On Scroll Down向上滚动时隐藏底部导航 向下滚动时显示
【发布时间】:2020-06-22 19:09:47
【问题描述】:

我有以下代码。如何在向下滚动时隐藏底部导航并在用户向上滚动时显示? 我需要的是当用户向下滚动时隐藏的导航,一旦用户开始向上滚动,导航应该重新出现。 XML

<?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:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
 
    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/toolBar">

        <LinearLayout
           ////////
        </LinearLayout>

    </ScrollView>

    <com.google.android.material.bottomnavigation.BottomNavigationView
        android:id="@+id/navigation"
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:background="@color/colorPrimary"
        app:itemTextColor="@drawable/selector"
        app:itemIconTint="@drawable/selector"
        app:menu="@menu/navigation"
        android:layout_alignParentBottom="true"
        app:labelVisibilityMode="labeled" />

</RelativeLayout>

JAVA

        BottomNavigationView bottomNavigationView = findViewById(R.id.navigation);
        bottomNavigationView.setSelectedItemId(R.id.navigation_one);
        bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                switch (item.getItemId()){
                    case R.id.navigation_one:
                        return true;
                    case R.id.navigation_two:
                        startActivity(new Intent(getApplicationContext() ,Two.class));
                        overridePendingTransition(0,0);
                        finish();
                        return true;
                    case R.id.navigation_three:
                        startActivity(new Intent(getApplicationContext() ,Three.class));
                        overridePendingTransition(0,0);
                        finish();
                        return true;
                    case R.id.navigation_four:
                        startActivity(new Intent(getApplicationContext() ,Four.class));
                        overridePendingTransition(0,0);
                        finish();
                        return true;
                }
                return false;
            }
        });

【问题讨论】:

标签: android bottomnavigationview


【解决方案1】:

如果您不想更改为协调器布局,您可以这样做:

  • setOnScrollChangeListener 用于ScrollView 检测滚动方向(上下)
  • 然后在onScrollChange 方法中为bottomNavigationView 设置动画以上下滑动(基于滚动方向)

【讨论】:

  • 再次感谢 @hamraa 不幸的是 setOnScrollChangeListener 需要 API 23 及更高版本。似乎必须尝试协调员并恢复
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-01-17
  • 1970-01-01
  • 2023-01-17
  • 1970-01-01
  • 2019-06-22
相关资源
最近更新 更多