动作条下拉微调项默认为第一项 [英] ActionBar Dropdown Spinner item defaults to first item

查看:122
本文介绍了动作条下拉微调项默认为第一项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想设置需要在微调默认情况下要选择的项目的索引,但它总是默认为0(第1项)

I'm trying to set the index of the item that needs to be selected in the spinner by default, but it always defaults to 0 (1st item)

actionBar.setDisplayShowTitleEnabled(false);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);

SpinnerAdapter spinnerAdapter =
            new ArrayAdapter<String>(activity, android.R.layout.simple_spinner_dropdown_item,
                    names);
int selectedIndex = actionBar.getSelectedNavigationIndex();
if (selectedIndex != targetIndex) {
    actionBar.setSelectedNavigationItem(targetIndex);
}

以上,如果块总是被调用。即使设置索引2之后,下一次我检查返回0。

Above if block is called always. Even after setting index 2, next time I check it returns 0.

编辑:我怀疑getSelectedNavigationIndex给ActionBar的项目,而不是微调下拉项指标。如果是这样的话,有什么方法选定项目的索引设置内部下拉?

I suspect getSelectedNavigationIndex gives index of actionBar item rather than Spinner dropdown item. If that is the case, what method sets the index of selected item inside dropdown?

推荐答案

请确保你调用<一个href="http://developer.android.com/reference/android/app/ActionBar.html#setListNavigationCallbacks%28android.widget.SpinnerAdapter,%20android.app.ActionBar.OnNavigationListener%29">setListNavigationCallbacks方法更改所选元素之前。我不能看到它在你的榜样,所以我认为这就是问题所在。

Make sure you call setListNavigationCallbacks method before changing selected element. I can't see it in your example, so I think that's the problem.

下面是一个例子:

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

它工作在我的应用程序没有任何问题。

It works in my app without any problems.

这篇关于动作条下拉微调项默认为第一项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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