向导不显示具有多个片段的单个活动应用

时间:2014-09-10 14:55:00

标签: android android-navigation

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

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

  

设置是否应将主页显示为“向上”可供件。设置这个   如果选择“home”在UI中按单个级别返回,则为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扩展了Fragment {

@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 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)时没有错误,但仍然没有显示Up按钮。我知道现在我没有在片段事务中设置除addToBackStack(null)调用之外的任何类型的片段导航层次结构,但看起来这个代码应该足以让up按钮替换导航抽屉按钮。 / p>

2 个答案:

答案 0 :(得分:3)

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

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

  

当指示器被禁用时,ActionBar将恢复显示由Activity.R.attr.homeAsUpIndicator属性中的Activity主题提供的home-as-up指示符,而不是动画抽屉标志符号。

答案 1 :(得分:1)

和你一样有问题,我努力获得一致的向上箭头行为。我做了这个例子来说明如何在使用导航抽屉和多个级别的单个活动时正确地执行此操作。

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