禁用抽屉式导航栏,切换家庭按钮/向上指示器片段指示器、片段、按钮、家庭

2023-09-12 21:53:08 作者:曾经沵是涐的瘾°

我的内容查看是 DrawerLayout 的实例,其中有一个抽屉式导航,在操作栏中显示的抽屉指标的活动。该活动包含一个片段,姑且称之为 ListFragment ,其中包含一个选项列表。当点击一个选项,我替换 ListFragment DetailFragment

I have an activity whose contentView is an instance of a DrawerLayout, which has a navigation drawer with a drawer indicator displayed in the action bar. The activity contains a Fragment, let's call it ListFragment, which contains a list of options. When an option is clicked, I replace the ListFragment with a DetailFragment.

在这一点上,我想显示一个向上的导航选项,而不是导航抽屉指标。我能够显示的向上图标,如果我禁用抽屉指标通过调用 mDrawerToggle.setDrawerIndicatorEnabled(假),但这只是消除了抽屉里的图标 - 它不删除的功能 - 也就是,当我点击插入符号,导航抽屉仍然打开

At this point, I would like to display an "up" navigation option instead of the navigation drawer indicator. I'm able to display the "up" icon if I disable the drawer indicator by calling mDrawerToggle.setDrawerIndicatorEnabled(false), but this only removes the drawer icon--it does not remove the functionality--that is, when I click the caret, the navigation drawer is still opened.

此外,在这些子视图,我想从屏幕的边缘拖动到关闭抽屉打开。我曾尝试这样做,通过调用 setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED),但它似乎并没有已禁用此功能。

Additionally, in these subviews, I would like to disable the opening of the drawer by dragging from the edge of the screen. I have tried doing this by calling setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED) but it doesn't seem to have disabled this functionality.

我已经试过了 ActionBarDrawerToggle 类延伸到prevent打开指示灯被点击时抽屉 - 但是,所发生的一切是压倒一切的操作( 向上导航)进行,但抽屉里的仍打开

I have tried extending the ActionBarDrawerToggle class to prevent opening the drawer when the indicator is clicked--however, all that happens is that the overriding action (the "up" navigation) is performed, but the drawer still opens.

我还实施Switching Android的导航抽屉的图像和向上插入符号之间在使用碎片的。它的工作原理,只要显示插入符那张,但尽管覆盖了按钮的功能,菜单仍然会打开(应用程序并导航回 - 它也只是打开抽屉)。

I have also implemented the steps in Switching between Android Navigation Drawer image and Up caret when using fragments . It works insofar as displaying the caret goes, but despite overriding the up button functionality, the menu still opens (the app does navigate back--it just also opens the drawer).

所以,长话短说:有没有(preferably干净优雅,但在这一点上,我会去哈克)的方式来实现这些东西的时候,我的布局根是一个 DrawerLayout

So, long story short: is there any (preferably clean and elegant, but at this point I'll go with hacky) way to achieve these things when my layout root is a DrawerLayout:

与更换纸盒指标上符号(通过 mDrawerToggle.setDrawerIndicatorEnabled初步可行的(假)) prevent从打开抽屉时,插入符号被点击,而是覆盖与我自己的向上的功能 prevent从什么时候我从屏幕的边缘拖动打开抽屉。

好吧,它看起来像如果我都覆盖 ActionBarDrawerToggle onOptionsItemSelected ,菜单不开的时候我单击插入符号。但它仍然会打开,如果我从边缘拖动。救命啊!

Edit

All right, it looks like if I both override ActionBarDrawerToggle AND onOptionsItemSelected, the menu does not open when I click the caret. But it still opens if I drag from the edge. Help!

推荐答案

这就是我来到了解决方案的一部分,但它是非常难搞清楚这个bug,所以我在这里离开这个给后人的缘故

This is only part of the solution that I arrived at, but it was quite hard to figure out this bug, so I'm leaving this here for posterity's sake.

这我是如何定义的ListView我的抽屉式导航栏:

This how I was defining the ListView for my navigation drawer:

<ListView
    android:id="@+id/listview_drawer"
    android:layout_width="240dp"
    android:layout_height="match_parent"
    android:layout_gravity="start|bottom"
    android:background="#111"
    android:choiceMode="singleChoice"
    android:divider="@android:color/transparent"
    android:dividerHeight="0dp" />

即使调用 setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED)我依然能滑动抽屉打开。

Even after calling setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED) I was still able to slide the drawer open.

但是,更改后的 layout_gravity 开始这个问题似乎得到解决。

However, after changing the layout_gravity to "start" this problem seems to be resolved.

我能够样品中重现此问题,导航抽屉唯一的应用程序,所以它看起来是一个可重复的问题,并非只有我的情况。

I was able to reproduce this issue in a sample, navigation-drawer-only app, so it does appear to be a reproducible issue not unique to my situation.