如何在片段内实现导航抽屉

时间:2018-07-06 18:10:55

标签: android navigation android-viewpager fragment navigation-drawer

我已在“活动”中使用TabLayout实现了ViewPager。选择不同的选项卡后,Viewpager将页面替换为片段。但是,现在我的用例是在Fragment.xml内实现导航抽屉,但是我无法做到这一点。

当我将DrawerLayout用作Fragment.xml的根布局时,子布局分别为AppBarLayoutFrameLayout

结构:

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

来自不同选项卡的所有工具栏都位于屏幕上方,并且高度不在actionBarSize高度内,ToolBar高度缩小,TabLayout缩小。

1 个答案:

答案 0 :(得分:0)

了解到抽屉布局将放置在片段内,然后只需通过xml布局访问该片段,然后将抽屉添加到布局内即可。

我要遵循的结构如下(用伪代码表示):

<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>

浏览文档,android documentation reference for navigation drawer与此类似。 这将确保您将抽屉及其布局放在已经看到的选项卡式布局的下方。

然后,在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