I am writing an app using jetpack recommended architecture , NavigationUI, and the navigation graph . So I have one main activity with a Toolbar
, a BottomNavigationView
and the NavHostFragment
.
Everything worked nicely until now: I need to change the Toolbar
to use a CollapsingToolbarLayout
and hide the BottomNavigationView
in one of my fragment.
I tried to add a navigation listener (as described here ) to hide my Toolbar
and BottomNavigationView
, and in my fragment, I inflate the new Toolbar
and call setSupportActionBar()
on the main activity.
// in MainActivity.java
@Override
protected void onCreate(Bundle savedInstanceState) {
// ...
navController.addOnDestinationChangedListener((controller, destination, arguments) -> {
if(destination.getId() == R.id.detailFragment){
bottomBar.setVisibility(View.GONE);
topBar.setVisibility(View.GONE);
}else{
bottomBar.setVisibility(View.VISIBLE);
topBar.setVisibility(View.VISIBLE);
}
});
// ...
}
public void changeToolbar(Toolbar toolbar){
getSupportActionBar().hide();
setSupportActionBar(toolbar);
}
// in DetailFragment.java
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// ...
navController = NavHostFragment.findNavController(this);
AppBarConfiguration.Builder builder = new Builder(
R.id.accuracyFragment,
R.id.dataFragment,
R.id.magnetFragment,
R.id.settingsFragment);
AppBarConfiguration config = builder.build();
NavigationUI.setupWithNavController(toolbarLayout, toolbar, navController);
((MainActivity)getActivity()).changeToolbar(toolbar);
// ...
}
It almost works correctly, but:
BottomNavigationView
is not correctly displayed. It seems to be pushed down by the Toolbar
. So the question is: is there another way to change/hide the navigation elements from the fragment? If not, should I create a new activity?
It's been a wild ride, but I finally found a solution. For the issue number 1, this is due to the way Android manages the fitsSystemWindows
property propagation. For this to work correctly, I made a few changes to my layouts. I created a custom FitSystemWindowLinearLayout
, which is simply a class extending the standard LinearLayout
and overriding onApplyWindowInsets
like this:
@Override
public WindowInsets onApplyWindowInsets(WindowInsets insets) {
int childCount = getChildCount();
for (int index = 0; index < childCount; ++index) {
getChildAt(index).dispatchApplyWindowInsets(insets);
}
return insets;
}
My main activity now looks like this:
+-- CoordinatorLayout, fitsSystemWindows=false
+-- FitSystemWindowLinearLayout, fitsSystemWindows="false"
+-- Toolbar
+-- NavHostFragment, fitsSystemWindows="false"
+-- BottomNavigationView, fitsSystemWindows="false"
For the second issue, namely the transition being ugly, I mitigated that by adding a shared element to the transition.
All in all, I think it's easier to use a new activity for this kind of things, the NavigationUI falls a bit short for now.
Here are some resources that helped me:
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.