新设计中的Android交换机碎片支持导航抽屉
如何在新的设计支持导航抽屉中切换片段?我在Cheesesquare Github上找到了关于如何使用TabLayout(而不是导航抽屉)切换片段的示例代码。是一样的吗?我也不想在切换时重新创建片段,而是像TabLayout一样保留片段实例,片段的内容就是用户离开它的方式。编写如下代码:新设计中的Android交换机碎片支持导航抽屉,android,android-fragments,navigation-drawer,androiddesignsupport,Android,Android Fragments,Navigation Drawer,Androiddesignsupport,如何在新的设计支持导航抽屉中切换片段?我在Cheesesquare Github上找到了关于如何使用TabLayout(而不是导航抽屉)切换片段的示例代码。是一样的吗?我也不想在切换时重新创建片段,而是像TabLayout一样保留片段实例,片段的内容就是用户离开它的方式。编写如下代码: navigationView.setNavigationItemSelectedListener( new NavigationView.OnNavigationItemSelectedListe
navigationView.setNavigationItemSelectedListener(
new NavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(MenuItem menuItem) {
menuItem.setChecked(true);
mDrawerLayout.closeDrawers();
switch (menuItem.getItemId()) {
case R.id.your_menu_id:
getSupportFragmentManager().beginTransaction().replace(R.id.fragment, getFragment(), "SET_A_TAG").addToBackStack("SET_A_TAG").commit();
break;
}
return true;
}
});
private YourFragment getFragment() {
YourFragment f = getSupportFragmentManager().findFragmentByTag("SET_A_TAG");
if (f == null) {
f = new YourFragment();
}
return f;
}
类似这样的东西?但每次我单击该项目时,都会创建一个新片段。我不想那样。我希望它保持不变,因此当我切换到另一个片段并返回时,它仍然具有相同的视图,我在EditText中输入的所有文本samei刚才注意到,此方法删除了工具栏?@qwertz否,它只是替换了片段。现在我注意到,当我切换回片段时,所有动画都重新启动,所以看起来好像每次都要重新加载time@qwertz你在你的片段中做了什么吗?我想你不明白我的问题,这也有一个答案,以为你是另一个人。是的,不过你必须把listpager和framelayout放在一个视图中,就像线性布局一样,谢谢因为我不知道如何显示另一个片段,其中也有一个表格。问题是,当我将新片段放入Fragmentcontainer时,没有显示工具栏。因为它被新片段隐藏
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/drawer_layout"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:fitsSystemWindows="true">
<include layout="@layout/include_list_viewpager"/>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@id/fragmentContainer"/>
<android.support.design.widget.NavigationView
android:id="@+id/nav_view"
android:layout_height="match_parent"
android:layout_width="wrap_content"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="@layout/nav_header"
app:menu="@menu/drawer_view"/>