简体   繁体   English

如何使用上下文操作栏来处理汉堡包图标和插入符号图标?

[英]How do I handle the hamburger-icon and up-caret-icon with a contextual action bar?

Ok, so normally I'm not the asking kinda guy, but I couldn't solve my problem googling. 好的,所以通常我不是要问的家伙,但是我无法解决谷歌搜索的问题。 So my first StackOverflow-Question. 所以我的第一个StackOverflow问题。

I have an activity with a navigation drawer which triggers a few fragments, those fragments have subfragments of their own. 我有一个带有导航抽屉的活动,该抽屉触发了一些片段,这些片段具有自己的亚片段。

When the user goes deeper in the app, the 'hamburger'-icon is replaced by the up-caret. 当用户深入应用程序时,“汉堡包”图标将替换为插入符号。 On pressing the caret or the back-key the subfragment is popped from the backstack and the hamburger-icon is back. 按下插入符号或返回键时,子片段会从后堆栈弹出,并且汉堡图标会返回。

This is what happens in the subfragment: 这是在子片段中发生的事情:

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    case android.R.id.home:
        // Called when the up caret in actionbar is pressed
        getActivity().onBackPressed();
        return true;
    }
    return super.onOptionsItemSelected(item);
}

In the activity: 在活动中:

@Override
public void onBackPressed() {
    // turn on the Navigation Drawer image
    FragmentManager fragmentManager = getSupportFragmentManager();

    if (fragmentManager.getBackStackEntryCount () != 0)
        mDrawerToggle.setDrawerIndicatorEnabled(true);
    }
    super.onBackPressed();
}   

So far, so good. 到现在为止还挺好。

The problem starts when we go deeper in the subfragment. 当我们深入子片段时,问题就开始了。 The subfragments has a list and onLongClickItem it shows a contextual actionbar which lets users alter those listitems. 子片段具有一个列表和onLongClickItem,它显示了一个上下文操作栏,使用户可以更改这些列表项。

If that contextual actionbar is opened and the back-button is pressed, it closes (as expected) but also changes the up-caret to the drawer-hamburger-icon-thingie. 如果打开了该上下文操作栏并按下了后退按钮,则它会关闭(如预期的那样),但同时也会将向上插入符号更改为抽屉式汉堡包图标主题。 Of course I do understand why this happens, but I don't see (or was able to find online) a clean solution. 我当然知道为什么会这样,但是我看不到(或能够在网上找到)一个干净的解决方案。

Am I going at it the wrong way, or am I just missing a simple step here? 我是用错误的方式走还是在这里错过了一个简单的步骤? I was thinking something along the lines of moving the setDrawerIndicatorEnabled to an onBackStackChangedListener and there listening for a tagged backStackItem. 我在考虑将setDrawerIndicatorEnabled移到onBackStackChangedListener并在那里侦听标记的backStackItem的思路。

But I thought; 但是我想; let's take some babysteps in to the wild and ask a question online before I start messing up all my code. 让我们踏出第一步,在我开始弄乱我的所有代码之前先在线提问。

Thanks 谢谢

The text below first was an edit on the question itselve, but because nobody seemed reluctant to answer my question I've added it as an answer so people who have a similar question are maybe helped by this. 下面的文字首先是对问题本身的修改,但由于似乎没人愿意回答我的问题,因此我将其添加为答案,以便对此有类似问题的人有所帮助。

If someone has a better answer, I'm still interested in a cleaner solution. 如果有人有更好的答案,我仍然对更清洁的解决方案感兴趣。


Ok, I managed to solve my own question, but I'm not realy sure I'm satisfied with it. 好的,我设法解决了自己的问题,但是我不确定自己是否对此感到满意。 What I did was add a fake entry to the backstack in onCreateActionMode, like so: 我所做的是在onCreateActionMode中向后堆栈添加了一个伪造的条目,如下所示:

@Override
public boolean onCreateActionMode(ActionMode actionmode, Menu menu) {
    MenuInflater inflater = actionmode.getMenuInflater();
    inflater.inflate(R.menu.task_contextual_menu, menu);

    getActivity().getSupportFragmentManager()
                .beginTransaction()
                .addToBackStack(null)
                .add(new Fragment(), FAKE_BACKSTACK_ENTRY)
                .commit();

    return true;
}

and popping that back off in onDestroyActionMode and instead of checking in onBackPressed for (fragmentManager.getBackStackEntryCount () != 0) , I check for (fragmentManager.getBackStackEntryCount () == 1) . 然后在onDestroyActionMode中弹出它,而不是在onBackPressed中检查(fragmentManager.getBackStackEntryCount () != 0) ,而不是检查(fragmentManager.getBackStackEntryCount () == 1)

I'm kind of proud of the workaround, but I realise it's not the cleanest of solutions... 我为解决方法感到自豪,但是我意识到这不是最干净的解决方案...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在活动栏中的插入符号旁边将向上按钮显示为图标? - How do I show the up button as an icon next to a caret in the Activity Bar? 如何在Contextual Action Bar / ActionMode中显示文本和图标? - How to show text together with an icon in Contextual Action Bar / ActionMode? 如何在导航react-native v2中添加汉堡包图标 - How to add hamburger-icon in navigation react-native v2 不带ActionBar的汉堡图标不带透明操作栏的汉堡图标 - Hamburger icon without ActionBar not with Transparent Action Bar 如何在不更改标准操作栏的情况下更改上下文操作栏的溢出图标? - How to change the overflow icon of the contextual action bar without changing it for the standard action bar? 使用带有导航组件的导航抽屉时如何更改操作栏图标(汉堡图标) - how to change Action bar icon (hamburger icon) when using navigation drawer with navigation component Android-如何隐藏操作栏上的应用程序图标 - Android - How do i hide the app icon on action bar 如何将搜索图标移动到操作栏的右侧? - How do I move the search icon to the right side on the action bar? 图标未显示在操作栏上 - Icon is not showing up on the action bar 如何使用主题在Android中隐藏操作栏图标? - How do I hide the action bar icon in Android using a theme?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM