我尝试为底部导航视图添加隐藏/显示行为,但它不起作用
我指的是这个答案
根据该答案,我添加了
app:layout_behavior="com.google.android.material.behavior.HideBottomViewOnScrollBehavior"
但它不起作用,可能是因为我拥有的 reyclerview 位于片段中,底部导航位于主要活动中,但我不确定
这里是
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout 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/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#e7e7e7"
tools:context=".MainActivity">
<TextView
android:id="@+id/goodbyetext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:fontFamily="@font/calibriregular"
android:text="Goodbye"
android:textAlignment="center"
android:textColor="#36332c"
android:textSize="50sp" />
<com.google.android.material.appbar.AppBarLayout
android:id="@+id/appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<androidx.appcompat.widget.Toolbar
android:id="@+id/appToolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="#e7e7e7"
android:minHeight="?attr/actionBarSize"
android:theme="?attr/actionBarTheme"
app:layout_scrollFlags="scroll|enterAlways">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:fontFamily="@font/calibriregular"
android:text="That Button Again"
android:textAlignment="center"
android:textColor="#36332c"
android:textSize="25dp" />
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/aboutPage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center|end"
android:layout_margin="10dp"
android:background="@drawable/border"
android:orientation="vertical">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="#fffdf6"
android:orientation="vertical">
<com.indiedev91.thatbuttonagain.DotsTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center" />
</LinearLayout>
</LinearLayout>
</androidx.appcompat.widget.Toolbar>
</com.google.android.material.appbar.AppBarLayout>
<include
android:id="@+id/fragment_container"
layout="@layout/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
/>
<androidx.coordinatorlayout.widget.CoordinatorLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:layout_margin="10dp"
android:background="@drawable/border">
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/bottomNavigationView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/bottom_navigation_style"
app:itemIconSize="30dp"
app:labelVisibilityMode="labeled"
app:itemTextColor="@color/buttonText"
app:layout_behavior="com.google.android.material.behavior.HideBottomViewOnScrollBehavior"
app:menu="@menu/bottom_nav_menu" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
这是包含 reyclerview 的片段
buttons
fragment_buttons.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout
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/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#e7e7e7"
tools:context=".ButtonsFragment">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/btnRecyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginStart="10dp"
android:layout_marginTop="10dp"
android:layout_marginEnd="10dp"
android:fitsSystemWindows="true"
android:overScrollMode="never"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
</androidx.recyclerview.widget.RecyclerView>
<androidx.viewpager.widget.ViewPager
android:id="@+id/viewPager"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<include
android:id="@+id/fragment_container"
layout="@layout/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>
请注意,我在主活动 xml 底部导航上方使用的协调器布局是应用边框
而且由于这不起作用,我以编程方式尝试了,所以在片段按钮 java
ButtonsFragment.java
bottomNavigationView = getActivity().findViewById(R.id.bottomNavigationView);
recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
if (dy > 0 && bottomNavigationView.isShown()) {
bottomNavigationView.setVisibility(View.GONE);
} else if (dy < 0) {
bottomNavigationView.setVisibility(View.VISIBLE);
}
}
});
但是你可以猜到,这并没有给出我可以从
HideBottomViewOnScrollBehavior
的行为中获得的向下滑动和向上滑动的动画样式
好吧,我发现如果我从主活动中的底部导航视图上方删除 CoordinatorLayout ,它就可以完美工作
但是问题是关于应用于底部导航的边框,所以为此我使用了图层列表
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Border -->
<item>
<shape android:shape="rectangle">
<solid android:color="@color/buttonBorder"/>
<corners android:radius="6dp" />
</shape>
</item>
<!--Bottom Nav Corner Radius -->
<item android:left="2.5dp" android:right="2.5dp" android:top="2.5dp" android:bottom="2.5dp">
<shape android:shape="rectangle">
<solid android:color="@color/buttonColor"/>
<corners android:radius="5dp" />
</shape>
</item>
</layer-list>
这是修改后的底部导航视图
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/bottomNavigationView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/combined_shape"
app:itemIconSize="30dp"
android:layout_margin="10dp"
android:layout_gravity="bottom"
app:labelVisibilityMode="labeled"
app:itemTextColor="@color/buttonText"
app:layout_behavior="com.google.android.material.behavior.HideBottomViewOnScrollBehavior"
app:menu="@menu/bottom_nav_menu" />