按下后更新导航抽屉的选定状态 [英] Update selected state of navigation drawer after back press

查看:84
本文介绍了按下后更新导航抽屉的选定状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

后按后处理导航抽屉的选定状态的正确方法是什么?

Whats the proper way to handle the selected state of the navigation drawer after back press?

我有一个导航抽屉,其中有n个条目(在列表视图中),例如Android Studio中的SDK示例. 当我单击导航抽屉条目时,我希望将它们添加到后堆栈中,以便我可以移回它们.

I have a navigation drawer with n entries (in a listview) like the SDK sample in Android Studio. When i click on the navigation drawer entries i want them to be added to the back stack, so i can move back to them.

在我拥有的onNavigationDrawerItemSelected(int pos)中

In onNavigationDrawerItemSelected(int pos) i have

        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        if (position == 0) {
            transaction.replace(R.id.container, new FragmentA());
        } else if (position == 1) {
            transaction.replace(R.id.container, new FragmentB());
        } else {
            transaction.replace(R.id.container, new FragmentC());
        }
        transaction.addToBackStack(null);
        transaction.commit();

当我单击抽屉中的第二个条目时,将选择B并替换A.如果随后单击了后退"按钮,则片段A会像应有的方式再次显示,但仍在导航抽屉中选择了B.

When i click on the second entry in the drawer, B gets selected and replaces A. If i click the back button afterwards, Fragment A is shown again like it should, but B is still selected in the navigation drawer.

按下后如何更新抽屉的选择状态?

How can i get the selection status of the drawer updated after pressing back?

以某种方式我需要调用mDrawerListView.setItemChecked(position,true);或NavigationDrawerFragment.selectItem(int位置).但是到哪个位置呢?我该如何记住?

Somehow i need a call to mDrawerListView.setItemChecked(position, true); or NavigationDrawerFragment.selectItem(int position). But to which position? How do i remeber it?

拦截onBackPressed吗?

Intercept with onBackPressed?

@Override
    public void onBackPressed() {}

但是我怎么知道哪个片段又是活跃的呢?以及它对应的位置.

But how do i know which fragment is active again? And to which position it corresponds.

我有看不见的简单解决方法吗?似乎将back与导航抽屉结合使用并更新选择状态是一种标准模式.

Is there some easy solution i am blind to see? It seems that using back in combination with the navigation drawer and updating the selection status is a standard pattern.

推荐答案

部分.

如果您的应用程序更新了其他用户界面元素以反映 请记住片段的当前状态,例如操作栏 在提交事务时更新UI.你应该更新 除了以下情况外,后退堆栈更改后的用户界面 您提交交易. 您可以在以下情况下收听 通过设置 FragmentManager.OnBackStackChangedListener :

If your application updates other user interface elements to reflect the current state of your fragments, such as the action bar, remember to update the UI when you commit the transaction. You should update your user interface after the back stack changes in addition to when you commit the transaction. You can listen for when a FragmentTransaction is reverted by setting up a FragmentManager.OnBackStackChangedListener:

getSupportFragmentManager().addOnBackStackChangedListener(
    new FragmentManager.OnBackStackChangedListener() {
        public void onBackStackChanged() {
            // Update your UI here.
        }
    });

那将是刷新导航抽屉中当前选项的适当位置.

That would be the proper place to refresh the current option in the navigation drawer.

这篇关于按下后更新导航抽屉的选定状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆