Sourav Kumar Mund
Sourav Kumar Mund

Reputation: 11

How to implement Navigation Drawer inside a Fragment

I have implemented TabLayout with ViewPager on the Activity. On selecting different tabs the Viewpager replace the page with a Fragment. But, Now my use case is to implement a Navigation Drawer inside a Fragment.xml but i am unable to do it.

When I am writing the DrawerLayout as the root layout in the Fragment.xml and child layout are AppBarLayout and FrameLayout.

Structure:

<DrawerLayout>
    <AppBarLayout><ToolBarLayout/><AppBarLayout>
    <FrameLayout/>
    <Navigation Menu/>
</DrawerLayout>

All the toolbar from different tabs are going above screen and it's height is not in actionBarSize height and ToolBar height is shrinked and the TabLayout is shrinked.

Upvotes: 1

Views: 3812

Answers (1)

EvOlaNdLuPiZ
EvOlaNdLuPiZ

Reputation: 600

understanding that the drawer layout will be placed inside the fragment then simply gain access to that fragment through an xml layout and add the drawer insider the layout.

the structure i would follow would be the following (in pseudocode):

<relativelayout>
name:outside-container-for-drawerlyout
height: ... and width: ...
 <relativelayout>
  name:actual-continer-for-drawerlayout
  height: ... and width: ...
  <!-- place drawer layout code here -->
  <drawerayout>
  height: ... and width: ...
   <!-- nest navigation view code here -->
   <navview>
    height: ... and width: ...
   </navview>
  </drawerlayout>
 </relativelayout>
</relativelayout>

peek at the documentation, android documentation reference for navigation drawer it's similar. this will ensure that you place your drawer and it's layouts below the tabbed layout you're already seeing.

then, on your fragment inside the java:

@Override
public View onCreateView(...) {
...
rootView = inflater.inflate(R..., container, false);
handleAccountNavigator();
return rootView;
}
private void handleAccountNavigator(){
this.mDrawerLayout = this.rootView.findViewById(R.id.drawer_layout);
this.mNavigationView = this.rootView.findViewById(R.id.nav_view);
new Handler().post(new Runnable()... to handle drawer actions.
}

enter image description here

Upvotes: 1

Related Questions