我正在使用新的 NavigationView 来显示带有 Android 设计支持库的导航菜单。我还在 DrawerLayout 中使用 FrameLayout 将片段替换到其中。
当我在片段之间进行更改时会出现问题。我的意思是,当我使用 NavigationView 单击另一个 Fragment 并再次返回到第一个 Fragment 时,该视图已被“清理”(来自 ViewPagers 选项卡的 fe 内容刚刚消失)。我不知道如何“刷新” FrameLayout,或者我是否必须重用 Fragments 而不是使用transaction.replace()。
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context=".MainActivity">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="@+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="?attr/actionBarSize" />
<include layout="@layout/toolbar" />
</RelativeLayout>
<android.support.design.widget.NavigationView
android:id="@+id/navigation_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
app:headerLayout="@layout/drawer_header"
app:menu="@menu/menu_drawer" />
</android.support.v4.widget.DrawerLayout>
我也使用这种方法在 Fragment 之间切换到 FrameLayout。
private void displayFragment(int position) {
Fragment fragment = null;
String title = "";
switch (position) {
case 0:
fragment = new FragmentA();
title = getString(R.string.fragment_a);
break;
case 1:
fragment = new FragmentB();
title = getString(R.string.fragment_b);
break;
case 2:
fragment = new FragmentC();
title = getString(R.string.fragment_c);
break;
default:
fragment = new HomeFragment();
title = getString(R.string.app_name);
break;
}
if (fragment != null) {
fragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.replace(R.id.content_frame, fragment);
transaction.commit();
drawerLayout.closeDrawers();
setTitle(title);
} else {
Log.w(TAG, "Error creating fragment");
}
}
谢谢!!