问题描述
我换出了工具栏上的操作栏,我几乎有每一块拼图到位。我的问题是,特别是如果我导航向上和恢复导航抽屉,抽屉切换按钮不再起作用。我想通了,如果我设置抽屉方式来解锁我要刷卡打开抽屉的能力,但不能点击打开抽屉。
I'm swapping out the action bar for the tool bar, and I nearly have every piece of the puzzle in place. My issue is specifically if I navigate 'up' and restore the navigation drawer, the drawer toggle button no longer works. I figured out if I set the drawer mode to unlocked I have the ability to swipe to open the drawer, but can't click to open the drawer.
所以,我加载碎片A,出票人的行为是好的,再往B片段并应用了图标,打了回去A,和抽屉不会与一个开放点击了。
So I load fragment A, drawer behaviour is fine, go down to fragment B and apply the up icon, hit up to go back to A, and the drawer won't open with a click any more.
输入片段B:
Toolbar t = mHostingActivity.getToolbar();
mHostingActivity.getDrawerToggle().setDrawerIndicatorEnabled(false);
mHostingActivity.getSupportActionBar().setDisplayHomeAsUpEnabled(true);
t.setNavigationIcon(mHostingActivity.getV7DrawerToggleDelegate().getThemeUpIndicator());
t.setNavigationOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
popBackStackToTop(mHostingActivity);
}
});
/**
* Pop the back stack and hide the Up caret all the way to the top level of the {@link com.loylap.activities.MainActivity}
*
* @param activity our hosting activity
*/
public static void popBackStackToTop(MainActivity activity) {
if (activity != null) {
FragmentManager fm = activity.getSupportFragmentManager();
fm.popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);
activity.getDrawerLayout().setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED);
activity.getDrawerToggle().setDrawerIndicatorEnabled(true);
}
}
导航抽屉设置就像样本,建立选项时,这个问题也许是老样子?例如,我仍然有这个在我的活动:
The navigation drawer is set up just like the sample, maybe the old way of setting up the options is the issue? For example, I still have this in my activity:
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (mDrawerToggle.onOptionsItemSelected(item)) {
return true;
}
return super.onOptionsItemSelected(item);
}
编辑:
好了,所以我已经缩小的问题向setNavigationOnClickListener()。如果我没有设置这个(并通过后退按钮上) - 抽屉行为正确。所以,现在的问题是,我该如何正确地让用户去上升,并恢复点击监听器后,我们干什么去了?
Okay so I've narrowed down the issue to the setNavigationOnClickListener(). If I don't set this (and go up via back button) - the drawer behaves correctly. So now the question is how do I correctly allow the user to go 'up', and restore the click listener after to we do go up?
推荐答案
所以我想通了,我创建了错误的点击监听器。相反setNavigationOnClickListener()的,我需要的 setToolbarNavigationClickListener():)
So I've figured out I was creating the wrong click listener. Instead of setNavigationOnClickListener(), I need setToolbarNavigationClickListener() :)
有一个微妙而重要的变化,现在的工具栏表现在合作与V7 ActionBarDrawerToggle
A subtle but important change, now the tool bar is behaving in partnership with the v7 ActionBarDrawerToggle
/**
* Create the Up caret for a lower level fragment {@link com.loylap.activities.MainActivity}
*
* @param activity our hosting activity
*/
public static void createUpButton(final MainActivity activity)
{
ActionBarDrawerToggle toggle = activity.getDrawerToggle();
//Disables onClick toggle listener (onClick)
toggle.setDrawerIndicatorEnabled(false);
toggle.setToolbarNavigationClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
popBackStackToTop(activity);
}
});
Toolbar t = activity.getToolbar();
t.setNavigationIcon(activity.getV7DrawerToggleDelegate().getThemeUpIndicator());
activity.getDrawerLayout().setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
}
这篇关于工具栏setNavigationOnClickListener打破ActionbarDrawerToggle功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!