Android Fragment Manager更改了Navigation Drawer的行为

时间:2015-04-23 09:00:08

标签: android navigation-drawer fragmentmanager



我想知道下一个 - 片段管理器移除并添加新片段时 - 导航切换按钮在点击时没有响应。

详情
我的项目中有导航抽屉。它是通过标准的Android工作室项目(带导航抽屉)创建的 2.我使用片段和片段管理器来处理屏幕内容。它发生在onNavigationDrawerItemSelected()中 3.以下是MainActivity中onCreateOptionsMenu()的实现:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    if (!mNavigationDrawerFragment.isDrawerOpen()) {
        // Only show items in the action bar relevant to this screen
        // if the drawer is not showing. Otherwise, let the drawer
        // decide what to show in the action bar.
        getMenuInflater().inflate(R.menu.main_activity, menu);
        MenuItem item = menu.findItem(R.id.right_item);
        getSupportActionBar().setTitle(mTitle);
        return true;
    }
    return super.onCreateOptionsMenu(menu);
}

4。当我使用FragmentManager.replace()时 - 一切正常。 当我使用remove()然后添加() - 导航抽屉切换按钮不响应点击。我仍然可以按它 - 但此时没有打开抽屉。 FragmentManager中的代码:

.beginTransaction()
.remove(fragmentManager.getFragments().get(0))
.commit();

.beginTransaction()
.add(container, replacingFragment)
.commit();

注意 - 我不会覆盖片段类中的任何菜单方法 可能是我使用错误的代码处理片段?

我之所以更喜欢删除/添加而不是替换 - 当导航抽屉中的用户选择项目使用相同的片段 - 片段的内容时应该更新;和replace()不会碎片化。

但对我来说真的很奇怪,片段管理器中的操作会破坏切换按钮。

更多。在这种情况下,onOptionsItemSelected()在MainActivity中调用,而不是在NavigationDrawerFragment片段中调用。

修改
xml文件是标准的,由Android studio

创建
    <!-- A DrawerLayout is intended to be used as the top-level content view using match_parent for both width and height to consume the full space available. -->
<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <!-- As the main content view, the view below consumes the entire
         space available using match_parent in both dimensions. -->
    <FrameLayout
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/bg1" />

    <!-- android:layout_gravity="start" tells DrawerLayout to treat
         this as a sliding drawer on the left side for left-to-right
         languages and on the right side for right-to-left languages.
         If you're not building against API 17 or higher, use
         android:layout_gravity="left" instead. -->
    <!-- The drawer is given a fixed width in dp and extends the full height of
         the container. -->
    <fragment
        android:id="@+id/navigation_drawer"
        android:layout_width="@dimen/navigation_drawer_width"
        android:layout_height="match_parent"
        android:layout_gravity="start|left"
        android:name="com.datingappanrdoid.NavigationDrawerFragment"
        tools:layout="@layout/fragment_navigation_drawer" >
    </fragment>
</android.support.v4.widget.DrawerLayout>

重现奇怪行为的步骤:
1.在Android studio中创建新项目 - 选择“带导航抽屉的项目” 2.将onNavigationDrawerItemSelected方法从replace()更改为remove()和add()。这就是我的意思:

 @Override
    public void onNavigationDrawerItemSelected(int position) {
        // update the main content by replacing fragments
        FragmentManager fragmentManager = getSupportFragmentManager();
//        fragmentManager.beginTransaction()
//                .replace(R.id.container, PlaceholderFragment.newInstance(position + 1))
//                .commit();

        if (tempFragment != null)
            tempFragment.setHasOptionsMenu(false);
        fragmentManager.beginTransaction()
                .remove(fragmentManager.getFragments().get(0))
                .commit();

        tempFragment = PlaceholderFragment.newInstance(position + 1);
        tempFragment.setHasOptionsMenu(true);

        fragmentManager.beginTransaction()
                .add(R.id.container, tempFragment)
                .commit();
    }

我还添加了hasOptionsMenu的用法,正如评论中所建议的那样 3.就绪 - 现在导航切换按钮无法点击(但仍然可以点击)。

仍然没有理解,为什么会这样。

1 个答案:

答案 0 :(得分:0)

当您致电add()remove()时,会添加/删除新片段。如果我们想要navigation drawer options给这个片段,我们需要从代码

中调用setHasOptionsMenu(boolean value)

我们将在 setHasOptionsMenu(true)之后致电add() ,并在 setHasOptionsMenu(false)之前致电remove()

这将解决您的问题