当隐藏在另一个片段中时,以编程方式在 CoordinatorLayout 中显示 BottomNavigationView
Programmatically show BottomNavigationView in CoordinatorLayout when it's hidden in another fragment
我在 hide_bottom_view_on_scroll_behavior
中使用底部导航视图。当用户从第一个片段列表中选择项目时,应用会转到详细信息 fragment.I 如果它隐藏在该(详细信息)片段中,则希望显示底部导航视图。
主要activityxml:
<?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:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:actionBarNavMode="standard"
tools:context=".MainActivity">
<com.google.android.material.appbar.AppBarLayout
android:id="@+id/app_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay"
app:elevation="0dp">
<com.google.android.material.appbar.CollapsingToolbarLayout
android:id="@+id/toolbar_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
app:contentScrim="?attr/colorPrimary"
app:layout_scrollFlags="scroll|exitUntilCollapsed">
<!--app:layout_scrollFlags="scroll|exitUntilCollapsed"-->
<!--app:layout_scrollFlags="scroll|enterAlways|snap"-->
<ImageView
android:id="@+id/expandedImage"
android:layout_width="match_parent"
android:layout_height="200dp"
android:scaleType="centerCrop"
android:src="@drawable/hrt1"
android:visibility="gone"
app:layout_collapseMode="parallax"
app:layout_collapseParallaxMultiplier="0.7" />
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:layout_collapseMode="pin"
app:layout_scrollFlags="scroll|enterAlways|snap" />
</com.google.android.material.appbar.CollapsingToolbarLayout>
</com.google.android.material.appbar.AppBarLayout>
<com.aero51.moviedatabase.ui.CustomViewPager
android:id="@+id/view_pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:isScrollContainer="true"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/bottom_navigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:background="@color/colorPrimary"
app:itemBackground="@color/colorPrimary"
app:itemIconTint="@drawable/bottom_navigation_color_selector"
app:itemTextColor="@drawable/bottom_navigation_color_selector"
app:layout_behavior="@string/hide_bottom_view_on_scroll_behavior"
app:menu="@menu/menu_bottom_navigation" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>
我尝试在详细信息片段中使用以下代码 onCreateView
但它不起作用:
bottomNavigationView = (BottomNavigationView) getActivity().findViewById(R.id.bottom_navigation);
bottomNavigationView.setVisibility(View.VISIBLE);
bottomNavigationView.animate().alpha(1f).setDuration(1000).start();
感谢帮助
要显示 BottomNavigationView
您需要按以下方式膨胀行为:
然后调用slideDown()
to hide or slideUp
显示BottomNavigationView
BottomNavigationView bottomNavigationView = (BottomNavigationView) getActivity().findViewById(R.id.bottom_navigation);
ViewGroup.LayoutParams layoutParams = bottomNavigationView.getLayoutParams();
if (layoutParams instanceof CoordinatorLayout.LayoutParams) {
CoordinatorLayout.Behavior behavior =
((CoordinatorLayout.LayoutParams) layoutParams).getBehavior();
if (behavior instanceof HideBottomViewOnScrollBehavior) {
HideBottomViewOnScrollBehavior<BottomNavigationView> hideShowBehavior =
(HideBottomViewOnScrollBehavior<BottomNavigationView>) behavior;
hideShowBehavior.slideUp(bottomNavigationView);
}
}
我在 hide_bottom_view_on_scroll_behavior
中使用底部导航视图。当用户从第一个片段列表中选择项目时,应用会转到详细信息 fragment.I 如果它隐藏在该(详细信息)片段中,则希望显示底部导航视图。
主要activityxml:
<?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:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:actionBarNavMode="standard"
tools:context=".MainActivity">
<com.google.android.material.appbar.AppBarLayout
android:id="@+id/app_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay"
app:elevation="0dp">
<com.google.android.material.appbar.CollapsingToolbarLayout
android:id="@+id/toolbar_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
app:contentScrim="?attr/colorPrimary"
app:layout_scrollFlags="scroll|exitUntilCollapsed">
<!--app:layout_scrollFlags="scroll|exitUntilCollapsed"-->
<!--app:layout_scrollFlags="scroll|enterAlways|snap"-->
<ImageView
android:id="@+id/expandedImage"
android:layout_width="match_parent"
android:layout_height="200dp"
android:scaleType="centerCrop"
android:src="@drawable/hrt1"
android:visibility="gone"
app:layout_collapseMode="parallax"
app:layout_collapseParallaxMultiplier="0.7" />
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:layout_collapseMode="pin"
app:layout_scrollFlags="scroll|enterAlways|snap" />
</com.google.android.material.appbar.CollapsingToolbarLayout>
</com.google.android.material.appbar.AppBarLayout>
<com.aero51.moviedatabase.ui.CustomViewPager
android:id="@+id/view_pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:isScrollContainer="true"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/bottom_navigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:background="@color/colorPrimary"
app:itemBackground="@color/colorPrimary"
app:itemIconTint="@drawable/bottom_navigation_color_selector"
app:itemTextColor="@drawable/bottom_navigation_color_selector"
app:layout_behavior="@string/hide_bottom_view_on_scroll_behavior"
app:menu="@menu/menu_bottom_navigation" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>
我尝试在详细信息片段中使用以下代码 onCreateView
但它不起作用:
bottomNavigationView = (BottomNavigationView) getActivity().findViewById(R.id.bottom_navigation);
bottomNavigationView.setVisibility(View.VISIBLE);
bottomNavigationView.animate().alpha(1f).setDuration(1000).start();
感谢帮助
要显示 BottomNavigationView
您需要按以下方式膨胀行为:
然后调用slideDown()
to hide or slideUp
显示BottomNavigationView
BottomNavigationView bottomNavigationView = (BottomNavigationView) getActivity().findViewById(R.id.bottom_navigation);
ViewGroup.LayoutParams layoutParams = bottomNavigationView.getLayoutParams();
if (layoutParams instanceof CoordinatorLayout.LayoutParams) {
CoordinatorLayout.Behavior behavior =
((CoordinatorLayout.LayoutParams) layoutParams).getBehavior();
if (behavior instanceof HideBottomViewOnScrollBehavior) {
HideBottomViewOnScrollBehavior<BottomNavigationView> hideShowBehavior =
(HideBottomViewOnScrollBehavior<BottomNavigationView>) behavior;
hideShowBehavior.slideUp(bottomNavigationView);
}
}