I'm having difficulty in implementing CoordinatorLayout
as how to implement the Toolbar
if some of my Fragment
s have a different Toolbar
behaviour. Say, that I have 5 fragments that would be shown in just one Activity (one fragment at a time). 2 of them would have a auto-hide Toolbar
when scrolled, 1 with a simple Toolbar
that's always showing even when scrolled, and 2 having Toolbar
with ImageView
inside AppBarLayout
for parallax effect.
This link only cover the auto-hide part, but not the parallax with image.
Some solutions that I could think of :
Have one
AppBarLayout
withToolbar
andImageView
inActivity
. So if it displays a fragment that doesn't need the parallax effect, just set theImageView
's visibility to GONE. But is it okay to do this? Because I got the feeling that it's not an appropiate solution.Define
CoordinatorLayout
andToolbar
in each fragments' layout. But then I have to callsetSupportActionBar
everytime I'm displaying my fragment, when some of them actually have the same Toolbar.
Which is the best approach? Or maybe there is a better one than those two?
UPDATE So, I ended up with number 2. I just add toolbars to each fragments' xml just like usual, nothing special. And in my BaseActivity I create a method to set the toolbar as action bar with some default configuration, so I won't have to duplicate the syntax to each of my fragments. Here's the method:
public void setToolbar(Toolbar toolbar, String title){
if(toolbar != null) {
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowTitleEnabled(title != null);
if (title != null) setActionBarTitle(title);
}
}