如何操作栏中的下拉导航设置活动项目? [英] How to set active item in the Action Bar drop-down navigation?

查看:169
本文介绍了如何操作栏中的下拉导航设置活动项目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想与方向的变化重新启动活动解决这个问题。

I'm trying to fix the issue with restarting activity on orientation changes.

我有一个动作条与下拉列表导航列表中的每一个转动的第一元素之后被激活。保持片段的内容并不难,但我不知道如何设置活动列表中的项目。

I have an ActionBar with drop-down list navigation and after every rotation first element of this list is being activated. Keeping fragment content wasn't difficult, but I don't know how to set active list item.

下面是这样定义的动作条

getActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
ArrayAdapter<CharSequence> list = ArrayAdapter
    .createFromResource(this, R.array.action_list, android.R.layout.simple_dropdown_item_1line);
list.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
getActionBar().setListNavigationCallbacks(list, this);

和这里是我的解决方法:

And here is my workaround:

@Override
public boolean onNavigationItemSelected(int itemPosition, long itemId) {
    if (!application.isRotated) {
        application.activePosition = itemPosition;
        application.activeId = itemId;
        getFragmentManager().beginTransaction()
            .replace(android.R.id.content, MyFragment.newInstance(itemPosition))
            .commit();
    } else {
        application.isRotated = false;
        this.onNavigationItemSelected(application.activePosition, application.activeId);            
    }
    return true;
}

@Override
protected void onStop() {
    super.onStop();
    application.isRotated = true;
}

我不知道这是最好的解决办法,但。

I'm not sure it's the best solution though.

推荐答案

我刚刚发现的功能。这是<一个href="http://developer.android.com/reference/android/app/ActionBar.html#setSelectedNavigationItem%28int%29"><$c$c>setSelectedNavigationItem(int位置) 。

I just found that function. It is setSelectedNavigationItem(int position).

设置选择导航项目的在清单或分页导航模式。

Set the selected navigation item in list or tabbed navigation modes.

例如:

actionBar = getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
actionBar.setListNavigationCallbacks(adapter, this);
actionBar.setSelectedNavigationItem(position);

这篇关于如何操作栏中的下拉导航设置活动项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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