问题
I was having a look at the Bottom App Bar and there is a nice feature to hide it on scroll app:hideOnScroll="true"
. The problem it that I cannot figure it out how to slide it up or down progmatically when the fragments are being navigated to and from.
For example, overview of a setup: on MainActivity
has the navigation host fragment and it is hosting a MainFragment
and a DetailFragment
.
The MainFragment
has a RecyclerView
, which on scroll, will hide the Bottom App Bar. On clicking any one of the item of the RecyclerView
, it navigates to DetailFragment
. But the problem is that the Bottom App Bar is still hidden and I want it to be shown.
And again, let's say that:
- the Start Destination has a
RecyclerView
; the bottom app bar is visible. - on clicking one of the item in recycler-view, it navigates to another fragment, where it also has a recycler-view. Scroll the recycler-view and the bottom app bar slides down.
- Press the up button to get back to start destination and you will find that the bottom app bar is still hidden.
Aren't there any methods for bottom app bar to slide up or down with navigation component?
In
MainActivity
I'm using the following codes in order to get the Up Button, in onCreate
:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
mBottomAppBar = findViewById(R.id.bottom_appbar);
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
NavigationUI.setupActionBarWithNavController(this, navController);
// ...
}
@Override
public boolean onSupportNavigateUp() {
return Navigation.findNavController(this, R.id.nav_host_fragment).navigateUp();
}
In activity_main.xml
, to hide the Bottom App Bar I've enabled app:hideOnScroll="true"
:
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
//...
<fragment
android:id="@+id/nav_host_fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:defaultNavHost="true"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
app:navGraph="@navigation/nav_graph" />
<com.google.android.material.bottomappbar.BottomAppBar
android:id="@+id/bottom_appbar"
android:layout_width="match_parent"
android:layout_height="?android:attr/actionBarSize"
app:hideOnScroll="true"
app:layout_anchor="@+id/nav_host_fragment"
app:layout_anchorGravity="center|bottom" />
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_anchor="@id/bottom_appbar" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>
Dependencies:
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'androidx.appcompat:appcompat:1.0.0'
implementation 'android.arch.navigation:navigation-fragment:1.0.0-alpha06'
implementation 'android.arch.navigation:navigation-ui:1.0.0-alpha06'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
implementation 'com.google.android.material:material:1.0.0'
implementation 'androidx.recyclerview:recyclerview:1.0.0'
// ...
回答1:
The problem has nothing to do with nav component, since it's behaving correctly. Just call bottom_appbar.show()
along with calling your navigation code.
来源:https://stackoverflow.com/questions/52575148/bottom-app-bar-slide-up-down-on-navigating-with-fragments-navigation-architectu