Fragment navigation. Toolbar

So, I have an activity with a navigation view. By clicking on my element, I change the fragment to activity. All fragments have the same toolbar. But you have this toolbar and TabLayout. I would like to know that it is better to declare the toolbar once per activity like this

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <include layout="@layout/toolbar" />

    <FrameLayout
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/toolbar" />

</RelativeLayout>

      

or declare it in each fragment.

The disadvantage of the first method is the default toolbar shadow. When I add tabs in the fragment, the shadow looks like

enter image description here

When I tried 2 solutions. My whole toolbar was with a reverse symbol and not an animated logo.

Thank.

+3


source to share


2 answers


I had the same problem. This is how I solved it:

  • Move the toolbars to slices as you suggested (so you don't have a shadow separating the two). This allows for a more flexible way of implementing (different) toolbars in your layouts.
  • Replace the navigation icon in the toolbar with a custom way:

    toolbar.setNavigationIcon(R.drawable.ic_action_menu);
    
          

(I used Android Asset Studio to easily create an icon with my preferred color)



  • Now open the nav menu with a new (home) menu icon. You can do this through MainActivity (the one with the NavigationView). Create a public method in this Control that opens the drawer:

    public void openDrawer(){
        mDrawerLayout.openDrawer(Gravity.LEFT);
    }
    
          

  • Now call this method on OnOptionsItemSelected in your fragments like this:

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // handle item selection
        switch (item.getItemId()) {
            case android.R.id.home: //Menu icon
                ((MainActivity)getActivity()).openDrawer();
                return true;            
            default:
                return super.onOptionsItemSelected(item);
        }
    }
    
          

What is it. Of course, the downside is that you have to implement a toolbar in every fragment. However, this is the only way (which I know of) that allows you to have a toolbar (+ TabLayout) in a fragment and still be able to control your navigation interface.

+4


source


You can use AppBarLayout from design support library like:

<android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <android.support.v7.widget.Toolbar
            ...
            app:layout_scrollFlags="scroll|enterAlways" />

        <android.support.design.widget.TabLayout
            ...
            />
    </android.support.design.widget.AppBarLayout>

      



and then you can change the visibility of the tablayout.

For more information on the layout of the layout library: link

0


source







All Articles