jjzjj

android - 具有多个 fragment 的单个 Activity 应用程序不显示向上导航

coder 2023-12-28 原文

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

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

Set whether home should be displayed as an "up" affordance. Set this to true if selecting "home" returns up by a single level in your UI rather than back to the top level or front page.

主要问题是当我使用函数时:

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);
}

然后,我有其他 fragment 都扩展了“BaseAppFragment”,其中包含以下内容:

基础应用 fragment .java

public class BaseAppFragment extends Fragment {

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

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

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

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

主 Activity .java

public void onSectionAttached(android.app.Fragment fragment) { Class 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) 时没有错误,但仍然没有显示向上按钮。我知道现在除了 fragment 事务中的 addToBackStack(null) 调用之外我没有设置任何类型的 fragment 导航层次结构,但看起来这段代码应该足以让向上按钮替换抽屉导航按钮。

最佳答案

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

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

When the indicator is disabled, the ActionBar will revert to displaying the home-as-up indicator provided by the Activity's theme in the android.R.attr.homeAsUpIndicator attribute instead of the animated drawer glyph.

关于android - 具有多个 fragment 的单个 Activity 应用程序不显示向上导航,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25768680/

有关android - 具有多个 fragment 的单个 Activity 应用程序不显示向上导航的更多相关文章

  1. ruby-on-rails - Rails 3 中的多个路由文件 - 2

    Rails2.3可以选择随时使用RouteSet#add_configuration_file添加更多路由。是否可以在Rails3项目中做同样的事情? 最佳答案 在config/application.rb中:config.paths.config.routes在Rails3.2(也可能是Rails3.1)中,使用:config.paths["config/routes"] 关于ruby-on-rails-Rails3中的多个路由文件,我们在StackOverflow上找到一个类似的问题

  2. ruby - 具有身份验证的私有(private) Ruby Gem 服务器 - 2

    我想安装一个带有一些身份验证的私有(private)Rubygem服务器。我希望能够使用公共(public)Ubuntu服务器托管内部gem。我读到了http://docs.rubygems.org/read/chapter/18.但是那个没有身份验证-如我所见。然后我读到了https://github.com/cwninja/geminabox.但是当我使用基本身份验证(他们在他们的Wiki中有)时,它会提示从我的服务器获取源。所以。如何制作带有身份验证的私有(private)Rubygem服务器?这是不可能的吗?谢谢。编辑:Geminabox问题。我尝试“捆绑”以安装新的gem..

  3. ruby-on-rails - Rails 编辑表单不显示嵌套项 - 2

    我得到了一个包含嵌套链接的表单。编辑时链接字段为空的问题。这是我的表格:Editingkategori{:action=>'update',:id=>@konkurrancer.id})do|f|%>'Trackingurl',:style=>'width:500;'%>'Editkonkurrence'%>|我的konkurrencer模型:has_one:link我的链接模型:classLink我的konkurrancer编辑操作:defedit@konkurrancer=Konkurrancer.find(params[:id])@konkurrancer.link_attrib

  4. ruby-on-rails - 在 Ruby 中循环遍历多个数组 - 2

    我有多个ActiveRecord子类Item的实例数组,我需要根据最早的事件循环打印。在这种情况下,我需要打印付款和维护日期,如下所示:ItemAmaintenancerequiredin5daysItemBpaymentrequiredin6daysItemApaymentrequiredin7daysItemBmaintenancerequiredin8days我目前有两个查询,用于查找maintenance和payment项目(非排他性查询),并输出如下内容:paymentrequiredin...maintenancerequiredin...有什么方法可以改善上述(丑陋的)代

  5. ruby-on-rails - Rails - 一个 View 中的多个模型 - 2

    我需要从一个View访问多个模型。以前,我的links_controller仅用于提供以不同方式排序的链接资源。现在我想包括一个部分(我假设)显示按分数排序的顶级用户(@users=User.all.sort_by(&:score))我知道我可以将此代码插入每个链接操作并从View访问它,但这似乎不是“ruby方式”,我将需要在不久的将来访问更多模型。这可能会变得很脏,是否有针对这种情况的任何技术?注意事项:我认为我的应用程序正朝着单一格式和动态页面内容的方向发展,本质上是一个典型的网络应用程序。我知道before_filter但考虑到我希望应用程序进入的方向,这似乎很麻烦。最终从任何

  6. ruby-on-rails - Rails 应用程序之间的通信 - 2

    我构建了两个需要相互通信和发送文件的Rails应用程序。例如,一个Rails应用程序会发送请求以查看其他应用程序数据库中的表。然后另一个应用程序将呈现该表的json并将其发回。我还希望一个应用程序将存储在其公共(public)目录中的文本文件发送到另一个应用程序的公共(public)目录。我从来没有做过这样的事情,所以我什至不知道从哪里开始。任何帮助,将不胜感激。谢谢! 最佳答案 无论Rails是什么,几乎所有Web应用程序都有您的要求,大多数现代Web应用程序都需要相互通信。但是有一个小小的理解需要你坚持下去,网站不应直接访问彼此

  7. ruby - 无法运行 Rails 2.x 应用程序 - 2

    我尝试运行2.x应用程序。我使用rvm并为此应用程序设置其他版本的ruby​​:$rvmuseree-1.8.7-head我尝试运行服务器,然后出现很多错误:$script/serverNOTE:Gem.source_indexisdeprecated,useSpecification.Itwillberemovedonorafter2011-11-01.Gem.source_indexcalledfrom/Users/serg/rails_projects_terminal/work_proj/spohelp/config/../vendor/rails/railties/lib/r

  8. ruby-on-rails - Rails 应用程序中的 Rails : How are you using application_controller. rb 是新手吗? - 2

    刚入门rails,开始慢慢理解。有人可以解释或给我一些关于在application_controller中编码的好处或时间和原因的想法吗?有哪些用例。您如何为Rails应用程序使用应用程序Controller?我不想在那里放太多代码,因为据我了解,每个请求都会调用此Controller。这是真的? 最佳答案 ApplicationController实际上是您应用程序中的每个其他Controller都将从中继承的类(尽管这不是强制性的)。我同意不要用太多代码弄乱它并保持干净整洁的态度,尽管在某些情况下ApplicationContr

  9. Ruby Readline 在向上箭头上使控制台崩溃 - 2

    当我在Rails控制台中按向上或向左箭头时,出现此错误:irb(main):001:0>/Users/me/.rvm/gems/ruby-2.0.0-p247/gems/rb-readline-0.4.2/lib/rbreadline.rb:4269:in`blockin_rl_dispatch_subseq':invalidbytesequenceinUTF-8(ArgumentError)我使用rvm来管理我的ruby​​安装。我正在使用=>ruby-2.0.0-p247[x86_64]我使用bundle来管理我的gem,并且我有rb-readline(0.4.2)(人们推荐的最少

  10. ruby - 多个属性的 update_column 方法 - 2

    我有一个具有一些属性的模型:attr1、attr2和attr3。我需要在不执行回调和验证的情况下更新此属性。我找到了update_column方法,但我想同时更新三个属性。我需要这样的东西:update_columns({attr1:val1,attr2:val2,attr3:val3})代替update_column(attr1,val1)update_column(attr2,val2)update_column(attr3,val3) 最佳答案 您可以使用update_columns(attr1:val1,attr2:val2

随机推荐