当前位置: 首页 > 知识库问答 >
问题:

Android碎片管理器改变导航抽屉的行为

邵骏喆
2023-03-14

详细信息
1。我有导航抽屉在我的项目。它是通过标准的Android studio项目(带有导航抽屉)创建的。
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()和add()时-导航抽屉切换按钮不响应轻击。我还能按--但抽屉此时没有打开。FragmentManager中的代码:

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

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

注意-我没有重写片断类中的任何菜单方法。
可能是我使用了错误的代码来处理片断?

编辑
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中创建新项目-选择“project with navigation Drawer”。
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个答案

慕容星晖
2023-03-14

当您调用add()remove()时,将添加/删除新片段。如果我们想给这个片段赋予导航抽屉选项,我们需要从代码调用SetHasOptionsMenu(布尔值)

我们将在add()之后调用sethasoptionsmenu(true)并且将在remove()之前调用sethasoptionsmenu(false)

这会解决你的问题

 类似资料:
  • 我有一个关于片段管理的问题。 我已经编写了一个基于碎片的抽屉活动。每次点击菜单项后,您都会看到一个空片段。 我如何实现这一点?

  • 在阅读了关于片段和活动的文档和讨论后,我想我已经把情况弄清楚了。 活动应该为用户提供一个屏幕,用户可以在其中工作。导航到其他屏幕通常会破坏活动。 片段应该提供很少或单个功能组件,可以在活动中再次使用。片段的内容应该相互独立地工作,因此可以在多个活动中重用。 虽然这一切似乎都有道理,但有一件事我无法理解: 如果您创建一个新的Android应用程序,并选择创建Android导航抽屉应用程序的选项,为什

  • 我已经在我的项目中添加了一个导航抽屉活动,并尝试将项目作为片段添加。这就是我在主要活动中所做的。 默认片段是Home,它工作正常,正如预期的那样,但其他片段与Home片段重叠。(HomeFragment是唯一工作正常的)。我在每个片段类中都做过这样的事情: 我看不出错误在哪里。一切看起来都很好,但所有其他片段都显示在HomeFragment上。

  • 我有一些问题切换导航抽屉功能到较低级别片段上的功能。我读了这个线程来显示向上的指示器。但当按下按钮时,它将打开导航抽屉,而不是回到以前的片段。并且我不能将EditUserFragment中的操作栏标题设置为“编辑配置文件”。我正在使用Android Studio中提供的导航抽屉模板。 我有三个层次: 包含主页和配置文件项的导航抽屉的主活动 标题为“配置文件”的UserFragment EditUs

  • 我开始创建一个应用程序,它使用一个活动(导航抽屉)和许多片段。但我无法使用工具栏上的“后退”按钮从片段中导航回来。硬件后退按钮工作正常。我知道我需要覆盖选项ItemSelected,捕捉android。R、 id.home,检查后堆栈中是否有内容,然后弹出它。在更改片段后,“burger”按钮变为“back arrow”,但当我单击它时,选项ItemSelected将打开NavigationDra

  • 当我在导航抽屉中的不同片段之间切换时,我试图保存导航抽屉片段上的状态。例如:我从片段A开始触发一些事件,然后切换到片段B。然后我想在从片段B切换回片段A时看到片段A的相同状态。 我尝试使用onSavedInstanceState(Bundle savedInstanceState),但只有当片段生命周期中的方向发生变化时才会调用它。每当我切换到新片段时,都会创建一个新片段,我不知道如何从片段中保存