0

我正在使用新的 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");
    }
}

谢谢!!

4

5 回答 5

0

当您更换片段时,您可能必须尝试此操作。

transaction.replace(R.id.content_frame, fragment).addToBackStack("Any_String");
于 2015-06-13T05:12:59.470 回答
0

我认为这个解决方案应该像它对我一样有效。在您的活动中进行以下更改......并且不要忘记覆盖活动中的 onbackpressed 按钮......它将片段加载到片段管理器中,并在按下按钮时将其返回(如果有)......否则发送用户到主屏幕(因为它可以由您自定义)

fragmentManager = getSupportFragmentManager();
    ChatWithUs myFragment0 = new ChatWithUs();
    FragmentTransaction ft = fragmentManager.beginTransaction();
    ft.replace(R.id.frameholder, myFragment0);
    ft.addToBackStack("chat_fragment");
    ft.commit();
    Log.i(MainActivity.TAG, "this is create method last line");

@Override
public void onBackPressed() {
    fragmentManager = getSupportFragmentManager();
    int backCount = fragmentManager.getBackStackEntryCount();
    //Log.i(MainActivity.TAG, String.valueOf(backCount));
    if((backCount > 1)){
        //Log.i(MainActivity.TAG, String.valueOf(backCount)) ;
        //Log.i(MainActivity.TAG, "im here") ;
        fragmentManager.popBackStack();
        Log.i(MainActivity.TAG, String.valueOf(fragmentManager.getBackStackEntryCount()));
    }
    else{
        //super.onBackPressed();
        //Log.i(MainActivity.TAG, String.valueOf(fragmentManager.getBackStackEntryCount())) ;
        Intent a = new Intent(Intent.ACTION_MAIN);
        a.addCategory(Intent.CATEGORY_HOME);
        a.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(a);
    }

}`
于 2016-01-11T07:44:47.803 回答
0

不确定是否是同一个问题,这是我的观察和解决方案:

该应用程序有一个NavigationView说 5 个项目来切换 5 个不同的片段(A、B、C、D、E)。最初它正确地切换了片段,但在上面切换了大约 5 次之后,一个片段只是粘在了支架FrameLayout上。所有事务都由 调用replace().commit()

  • 选择一个
  • 选择B
    ……
  • 选择 C
  • 选择 A(C 留在支架上,下面有 A)
  • 选择 B(C 留在支架上,B 在下面)

由于屏幕方向发生了变化,我发现片段 A 实际上被片段 C 覆盖。片段事务已提交,但不知何故片段 C 没有被替换。我什至试图getSupportFragmentManager().findFragmentByTag()找到以前的片段。没有任何效果。

因此,对于解决方案,我不确定它是否合法但有效:```

<android.support.design.widget.CoordinatorLayout
    android:id="@+id/rootLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

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

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:theme="@style/ThemeOverlay.AppCompat.Light"
            app:layout_collapseMode="pin"
            app:layout_scrollFlags="enterAlways"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light">

        </android.support.v7.widget.Toolbar>

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


    <FrameLayout
        android:id="@+id/a_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" />

    <FrameLayout
        android:id="@+id/b_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" />

    <FrameLayout
        android:id="@+id/c_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" />

    <FrameLayout
        android:id="@+id/d_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" />

    <FrameLayout
        android:id="@+id/e_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" />
</android.support.design.widget.CoordinatorLayout>

<android.support.design.widget.NavigationView
    android:id="@+id/navigation"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    app:itemIconTint="#333"
    app:itemTextColor="#333"
    app:menu="@menu/navigation_drawer_items" />

```

如您所见FrameLayout,每个片段有 5 个容器。当导航项选择一个容器设置为VISIBLE其他设置为GONE。并且工作比碎片交易更顺利。希望能给你一些想法。

--Edit 有人说使用附加/分离而不是替换。 https://stackoverflow.com/a/20959779/164005

我会试一试。如果你发现了什么,请告诉我。

于 2015-12-12T05:44:25.177 回答
0

尝试这个

     NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
            if (navigationView != null) {
                setupDrawerContent(navigationView);
            }


     private void setupDrawerContent(NavigationView navigationView) {
            navigationView.setNavigationItemSelectedListener(
                    new NavigationView.OnNavigationItemSelectedListener() {
                @Override
                public boolean onNavigationItemSelected(MenuItem menuItem) {
                    Fragment fragment = null;
                    switch (menuItem.getItemId()) {
                        case R.id.nav_home:
                            Log.i("TAG","HOME");
                            fragment = new CheeseListFragment();
                            break;
                        case R.id.nav_messages:
                            fragment = new MessageFragment();
                            break;
                        case R.id.nav_friends:
                            fragment = new FriendsFragment();
                            break;
                        case R.id.nav_discussion:
                             fragment = new DiscussionFragment();
                            break;

                    }

                    if (fragment != null) {
                        FragmentManager fragmentManager = getSupportFragmentManager();
                        fragmentManager.beginTransaction()
                                .replace(R.id.container, fragment).commit();

                    }
                        menuItem.setChecked(true);
                        mDrawerLayout.closeDrawers();
                        return true;

                }
            });
}

您可以通过这种方式打开片段检查在切换案例中选择了哪个菜单并相应地打开片段

于 2015-07-31T11:28:22.040 回答
0

基于这个答案,解决方案是使用getChildFragmentManager()而不是getSupportFragmentManager()包含其他片段的内部片段,例如 ViewPagers 选项卡。

于 2016-02-22T12:19:40.337 回答