【问题标题】:Up navigation not appearing for single activity app with multiple fragments具有多个片段的单个活动应用程序不会出现向上导航
【发布时间】:2014-09-10 14:54:59
【问题描述】:

我有一个 android 应用程序,它有一个主 Activity,它使用许多切换到视图中的片段。我不确定这是否是正确的做法,但我继承了这个项目,并希望避免进行任何重大的重构,例如将片段更改为所有活动或类似的东西。

根据 android 文档,看起来调用 setDisplayHomeAsUp(bool) 函数应该默认只显示向上按钮:

设置是否应将主页显示为“向上”可供性。设置这个 如果在您的 UI 中选择“home”返回单个级别,则为 true 而不是回到顶层或首页。

主要问题是当我使用该功能时:

actionBar.setDisplayHomeAsUpEnabled(true);

它不会将打开导航抽屉的按钮设置为“向上”按钮。它只是从侧面删除了“汉堡包”ic_drawer 图标。导航抽屉仍然打开。

这是 NavigationDrawerFragment 的自定义代码(我复制并粘贴了您在 android studio 中创建带有导航抽屉的新应用程序时获得的确切文件):

NavigationDrawerFragment.java

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    super.onCreateView(inflater, container, savedInstanceState);

    mDrawerListView = (ListView) inflater.inflate(
            R.layout.fragment_navigation_drawer, container, false);
    mDrawerListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            selectItem(position);
        }
    });

    PopulateAppDrawerList();
    return mDrawerListView;
}

public void PopulateAppDrawerList() {
    List<AppOption> allApps = getAllApps();
    List<AppOption> filteredApps = new ArrayList<AppOption>();

    for (int i = 0; i < allApps.size(); i++) {
        if (allApps.get(i).getLaunchable()) {
            filteredApps.add(allApps.get(i));
        }
    }


    NavDrawerListAdapter adapter = new NavDrawerListAdapter(filteredApps, MainActivity.getInstance());
    mDrawerListView.setAdapter(adapter);
    mDrawerListView.setItemChecked(mCurrentSelectedPosition, true);
}

然后,我有所有扩展“BaseAppFragment”的其他片段,其中包含以下内容:

BaseAppFragment.java

公共类 BaseAppFragment 扩展片段 {

@Override
public void onStart() {
    super.onStart();

    MainActivity.getInstance().onSectionAttached(this);
}

@Override
public void onAttach(Activity activity) {
    super.onAttach(activity);
} }

这使我可以在一个区域中更改操作栏上的标题,并设置是否默认设置后退按钮。

MainActivity.java

public void onSectionAttached(android.app.Fragment 片段) { 类 fragmentType = fragment.getClass();

    ActionBar actionBar = getActionBar();

    mNavigationDrawerFragment.PopulateAppDrawerList();
    if (fragmentType != null) {
        if (fragmentType.equals(AuthenticationFragment.class)) {
            actionBar.setDisplayHomeAsUpEnabled(false);
            mTitle = "Login";
        } else if (fragmentType.equals(MyOptionsFragment.class)) {
            actionBar.setDisplayHomeAsUpEnabled(false);
            mTitle = "My Options";
        } else if (fragmentType.equals(GLAuthenticationFragment.class)) {
            actionBar.setDisplayHomeAsUpEnabled(false);
            mTitle = "Login";
        } else if (fragmentType.equals(InitialLoginFragment.class)) {
            actionBar.setDisplayHomeAsUpEnabled(false);
            mTitle = "Login";
        } else if (fragmentType.equals(LoginFragment.class)) {
            actionBar.setDisplayHomeAsUpEnabled(false);
            mTitle = "Login";
        } else if (fragmentType.equals(DailyOverviewFragment.class)) {
            actionBar.setDisplayHomeAsUpEnabled(true);
            mTitle = "Overview";
        } else if (fragmentType.equals(SingleComponentFragment.class)) {
            actionBar.setDisplayHomeAsUpEnabled(true);
            SingleComponentFragment singleComponentFragment = (SingleComponentFragment) fragment;

            if (singleComponentFragment != null && singleComponentFragment .mComponent != null) {
                mTitle = String.format("Back To Day %s", singleComponentFragment.mComponent.getDay() + "");
            }
            else {
                mTitle = "";
            }
        } else if (fragmentType.equals(singleDayOverviewFragment.class)) {
            actionBar.setDisplayHomeAsUpEnabled(true);
            mTitle = "Back To Overview";
        } 
    }

    actionBar.setTitle(mTitle);
}

标题设置完美运行,调用 setDisplayHomeAsUpEnabled(true) 时没有错误,但仍然没有显示向上按钮。我知道现在除了片段事务中的 addToBackStack(null) 调用之外,我没有设置任何类型的片段导航层次结构,但看起来这段代码应该足以让向上按钮替换导航抽屉按钮。

【问题讨论】:

  • 你正在测试哪个 api?
  • 你是说SDK吗?我们的目标是 sdk v.17+

标签: android android-navigation


【解决方案1】:

问题是导航抽屉图标劫持了向上指示器。就操作栏中的哪个 View 显示图标而言,导航抽屉图标也是向上图标。这就是为什么您需要调用 actionBar.setDisplayHomeAsUpEnabled(true); 来显示抽屉式导航图标的原因。

要解决此问题,您需要使用ActionBarDrawerToggle#setDrawerIndicatorEnabled(false)。这将用向上图标替换导航抽屉图标。来自此方法的文档:

当指示器被禁用时,ActionBar 将恢复为在 android.R.attr.homeAsUpIndicator 属性中显示 Activity 主题提供的 home-as-up 指示器,而不是动画抽屉字形。

【讨论】:

  • 嘿@tanis,不久前你帮我解决了这个问题——我在实现支持设计库中的导航视图时遇到了完全相同的问题......如果你看看我的问题,我想也许你可能有一些见识?我完全被难住了。
  • 呼叫setDrawerIndicatorEnabled(false) 不再适合您吗?目前它对我有用。
  • 当我调用它时,它会完全隐藏工具栏上的按钮。
  • 您是否尝试过调用setHomeAsUpIndicator() 来指定在抽屉指示器被禁用时使用的资源而不是默认资源?
  • 哈。在发布问题之前,我确实花了 2 天时间试图弄清楚这一点。如果您发布我昨天的问题,我会将其标记为已接受。再次感谢您。
【解决方案2】:

和你有同样的问题,我很难获得一致的向上箭头行为。我做了这个例子来展示如何在使用带有导航抽屉和多个级别的单个活动时正确地做到这一点。

https://github.com/tskulbru/android-navdrawer-up-pattern-example

【讨论】:

    猜你喜欢
    • 2023-03-20
    • 1970-01-01
    • 1970-01-01
    • 2015-07-31
    • 1970-01-01
    • 2014-06-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多