Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/xamarin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何防止NavigationDrawer通过手势打开,但允许从Support Design Library 23.1.1.1中的汉堡图标打开_Android_Xamarin_Xamarin.android_Navigation Drawer - Fatal编程技术网

Android 如何防止NavigationDrawer通过手势打开,但允许从Support Design Library 23.1.1.1中的汉堡图标打开

Android 如何防止NavigationDrawer通过手势打开,但允许从Support Design Library 23.1.1.1中的汉堡图标打开,android,xamarin,xamarin.android,navigation-drawer,Android,Xamarin,Xamarin.android,Navigation Drawer,我有一个ViewPager,其中页面包含对滑动运动作出反应的图表视图。因此,我求助于通过从屏幕边缘滑动来更改页面。但这给我留下了一个问题,这也是打开NavigationDrawer的手势 到目前为止,我使用以下代码来实现这一点: protected override void OnCreate(Bundle savedInstanceState) { base.OnCreate(savedInstanceState); SetContentView(GetLayoutId());

我有一个ViewPager,其中页面包含对滑动运动作出反应的图表视图。因此,我求助于通过从屏幕边缘滑动来更改页面。但这给我留下了一个问题,这也是打开NavigationDrawer的手势

到目前为止,我使用以下代码来实现这一点:

protected override void OnCreate(Bundle savedInstanceState)
{
  base.OnCreate(savedInstanceState);

  SetContentView(GetLayoutId());

  Toolbar = FindViewById<Toolbar>(Resource.Id.toolbar);
  if (Toolbar != null)
  {
    // set this flag so the colors colorPrimaryDark and android:statusBarColor have an effect
    // setting android:statusBarColor to transparent causes the drawer to be dran underneath a translucent status bar
    Window.AddFlags(WindowManagerFlags.DrawsSystemBarBackgrounds);

    // make the toolbar the replacement of the action bar
    SetSupportActionBar(Toolbar);
  }

  // add the hamburger icon
  m_DrawerLayout = FindViewById<DrawerLayout>(Resource.Id.drawer_layout);
  var actionBarDrawerToggle = new ActionBarDrawerToggle(this, m_DrawerLayout, Toolbar, Resource.String.empty, Resource.String.empty);
  m_DrawerLayout.AddDrawerListener(actionBarDrawerToggle);

  // make sure the drawer can't be opened by swiping, to do this we set the lock mode to closed
  // but if we just do this, it can't be closed by swiping either, so set the lock mode to unlocked when the drawer is opened, and locked again when it's closed
  m_DrawerLayout.DrawerOpened += (object sender, DrawerLayout.DrawerOpenedEventArgs e) =>
  {
    m_DrawerLayout.SetDrawerLockMode(DrawerLayout.LockModeUnlocked);
  };
  m_DrawerLayout.DrawerClosed += (object sender, DrawerLayout.DrawerClosedEventArgs e) =>
  {
    m_DrawerLayout.SetDrawerLockMode(DrawerLayout.LockModeLockedClosed);
  };
  m_DrawerLayout.SetDrawerLockMode(DrawerLayout.LockModeLockedClosed);

  //calling sync state is necessay or else the hamburger icon wont show up
  actionBarDrawerToggle.SyncState();
}
protectedoverride void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
SetContentView(GetLayoutId());
Toolbar=findviewbyd(Resource.Id.Toolbar);
如果(工具栏!=null)
{
//设置此标志,使颜色colorPrimaryDark和android:statusBarColor生效
//将android:statusBarColor设置为transparent会导致抽屉位于半透明状态栏下
添加标志(WindowManagerFlags.DrawsSystemBarBackgrounds);
//使工具栏成为操作栏的替代品
设置支持操作栏(工具栏);
}
//添加汉堡图标
m_DrawerLayout=findviewbyd(Resource.Id.DrawerLayout);
var actionBarDrawerToggle=new actionBarDrawerToggle(这个,m_抽屉布局,工具栏,Resource.String.empty,Resource.String.empty);
m_DrawerLayout.AddDrawerListener(actionBarDrawerToggle);
//确保抽屉不能通过刷卡打开,为此,我们将锁定模式设置为关闭
//但如果我们只是这样做,它也不能通过刷卡来关闭,所以当抽屉打开时,将锁定模式设置为解锁,当抽屉关闭时再次锁定
m_DroperLayout.DroperRoped+=(对象发送方,DroperLayout.DroperRopedEventArgs e)=>
{
m_抽屉布局。设置抽屉锁定模式(抽屉布局。锁定模式解锁);
};
m_DrawerLayout.DrawerClosed+=(对象发送方,DrawerLayout.DrawerClosedEventArgs e)=>
{
m_DrawerLayout.SetDrawerLockMode(DrawerLayout.LockModeLockedClosed);
};
m_DrawerLayout.SetDrawerLockMode(DrawerLayout.LockModeLockedClosed);
//必须调用同步状态,否则汉堡图标不会显示
actionBarDrawerToggle.SyncState();
}

它按预期工作,直到我更新到Android支持设计库23.1.1.1,现在将锁定模式设置为closed还可以通过点击汉堡图标防止菜单被打开。

查看最新版本的
ActionBarDrawerToggle
类的源代码,这确实似乎是新的预期行为。它的
toggle()
方法现在看起来如下所示:

private void toggle() {
    int drawerLockMode = mDrawerLayout.getDrawerLockMode(GravityCompat.START);
    if (mDrawerLayout.isDrawerVisible(GravityCompat.START)
        && (drawerLockMode != DrawerLayout.LOCK_MODE_LOCKED_OPEN)) {
        mDrawerLayout.closeDrawer(GravityCompat.START);
    }
    else if (drawerLockMode != DrawerLayout.LOCK_MODE_LOCKED_CLOSED) {
        mDrawerLayout.openDrawer(GravityCompat.START);
    }
}
而之前它只是检查抽屉的打开/关闭状态

这是不幸的,因为现在需要一个变通方法来实现旧的行为。也许最简单的方法就是恢复到旧版本的支持库。但是,如果您想保留最新版本,一个可能的解决方案如下

首先从
ActionBarDrawerToggle
构造函数调用中删除
工具栏
参数

actionBarDrawerToggle = new ActionBarDrawerToggle(this, 
                                                  m_DrawerLayout, 
                                                  Resource.String.empty,
                                                  Resource.String.empty);
这将导致
活动
OnOptionsItemSelected()
方法在单击切换时触发,因为您已将
工具栏
设置为支持
操作栏
。我们还需要调用
SupportActionBar.SetDisplayHomeAsUpEnabled(true)
来实际显示切换,因为
ActionBarDrawerToggle
类与
ActionBar
的交互方式与其子
视图的交互方式有所不同

活动
onoptionItemSelected()
方法中,我们只需解锁抽屉,然后调用触发器自己的
onoptionItemSelected()
方法,该方法处理抽屉的打开和关闭

public override bool OnOptionsItemSelected (IMenuItem item)
{       
    switch (item.ItemId)
    {
        case Android.Resource.Id.Home:
            m_DrawerLayout.SetDrawerLockMode(DrawerLayout.LockModeUnlocked);
            actionBarDrawerToggle.OnOptionsItemSelected(item);

            return true;
        ...
    }
    ... 
}

您的
actionBarDrawerToggle
需要是您的
活动的一个字段,您可以删除
DrawerToggle
处理程序。

一个非标准行为:)可能是您必须创建一个自定义
视图
DrawerLayout
扩展而来。如果确实是更新导致了这一点,这听起来像是他们的失误。您可能希望恢复到以前的库版本,直到他们修复它,或者声明这是现在的预期行为,这将是不有趣的,因为这将需要一个不必要的复杂的解决方法来获得旧的行为。非常感谢,这是可行的。我还需要添加一件事:
SupportActionBar.SetDisplayHomeAsUpEnabled(true)否则汉堡图标将不会显示。我希望避免继续使用旧版本的支持库。这个应用还在开发中,我不想在第一次发布之前就被迫使用过时的东西……啊,是的,我想我没注意到你还没有。酷。很高兴这对你有用。干杯