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

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

问题描述

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

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 position).但是到哪个位置呢?我怎么记住它?

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.

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

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.

推荐答案

此模式在 实现 Fragments 的返回导航正确返回导航"文档的部分.

This pattern is described in the Implement Back Navigation for Fragments section of the "Proper Back Navigation" documentation.

如果您的应用程序更新其他用户界面元素以反映您的片段的当前状态,例如操作栏,请记住提交事务时更新 UI.你应该更新您的用户界面在返回堆栈更改后除了何时你提交交易.您可以在FragmentTransaction 通过设置一个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天全站免登陆