我的视图中有两个导航抽屉。当右侧抽屉打开时,我想锁定左侧抽屉,当左侧抽屉打开时,我想锁定右侧抽屉。
目前,当(例如)左边的一个打开,你从右边滑动以关闭它时,右边的抽屉开始打开,然后左边的一个完全关闭。这不是理想的行为。
这是我尝试过的内容,但是当我刷抽屉时,我得到NullPointerException
。
// Left Drawer Config
_leftDrawerToggler = new DrawerToggler(this,
_rootViewLayout,
Resource.Drawable.ic_drawer_light,
Resource.String.drawer_open,
Resource.String.drawer_close);
_leftDrawerToggler.DrawerClosed += delegate
{
InvalidateOptionsMenu();
_rootViewLayout.SetDrawerLockMode(DrawerLayout.LockModeUnlocked, _rightDrawerLayout);
};
_leftDrawerToggler.DrawerOpened += delegate
{
_leftDrawerList.SetItemChecked(0, true);
InvalidateOptionsMenu();
_rootViewLayout.SetDrawerLockMode(DrawerLayout.LockModeLockedClosed, _rightDrawerLayout);
};
_rootViewLayout.SetDrawerShadow(Resource.Drawable.drawer_shadow_dark, (int)GravityFlags.Left);
_rootViewLayout.SetDrawerListener(_leftDrawerToggler);
// Right Drawer Config
_rightDrawerToggler = new DrawerToggler(this,
_rootViewLayout,
Resource.Drawable.ic_drawer_light,
Resource.String.drawer_open,
Resource.String.drawer_close);
_rightDrawerToggler.DrawerClosed += delegate
{
InvalidateOptionsMenu();
_rootViewLayout.SetDrawerLockMode(DrawerLayout.LockModeUnlocked, _leftDrawerLayout);
};
_rightDrawerToggler.DrawerOpened += delegate
{
_rightDrawerList.SetItemChecked(0, true);
InvalidateOptionsMenu();
_rootViewLayout.SetDrawerLockMode(DrawerLayout.LockModeLockedClosed, _leftDrawerLayout);
};
_rootViewLayout.SetDrawerShadow(Resource.Drawable.right_drawer_shadow_dark, (int)GravityFlags.Right);
_rootViewLayout.SetDrawerListener(_rightDrawerToggler);
答案 0 :(得分:1)
问题是你没有锁定正确的 VieW
。另一个问题是,Listener
只需要一个DrawerLayout
。从你的代码我可以理解你有两个DrawerLayout
您的Layout
XML
应该有DrawerLayout
,其中ListView
layout_gravity
对于ListView start
应为end
。{ {1}})
当Drawer
opened
检查View Object
是否相等View.equal()
时,请锁定另一个ListView' and when the 'Drawer
closed
解锁另一个ListView
。
像这样改变你的代码
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_drawer_layout);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mLeftDrawerList = (ListView) findViewById(R.id.left_drawer);
mRightDrawerList = (ListView) findViewById(R.id.right_drawer);
mDrawerToggle = new ActionBarDrawerToggle(this,
mDrawerLayout,R.drawable.ic_drawer,
R.string.drawer_open,R.string.drawer_close)
{
public void onDrawerOpened(View view){
if(view.equals(mRightDrawerList)) {
mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED, mLeftDrawerList);
}
else {
mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED, mRightDrawerList);
}
invalidateOptionsMenu();
}
public void onDrawerClosed(View view)
{
if(view.equals(mRightDrawerList)) {
mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED, mLeftDrawerList);
}
else {
mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED, mRightDrawerList);
}
invalidateOptionsMenu();
}
};
mDrawerLayout.setDrawerListener(mDrawerToggle);
}
这应该有效。如果您发现任何问题,请与我们联系。