onNavigationItemSelected 从未调用 Actionbar 的微调器 [英] onNavigationItemSelected never called on Actionbar's spinner

查看:31
本文介绍了onNavigationItemSelected 从未调用 Actionbar 的微调器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试根据开发示例在 Action Bar Compat 中构建下拉菜单:http://developer.android.com/guide/topics/ui/actionbar.html#Dropdown

I'm trying to build a drop down menu in the Action Bar Compat, as per the dev example: http://developer.android.com/guide/topics/ui/actionbar.html#Dropdown

我无法让 onNavigationItemSelected 触发.

I can't get onNavigationItemSelected to fire though.

正在生成操作栏中的微调下拉项,位于操作栏标题的右侧.当我按下它时,我确实看到三角形图标变成蓝色.OnNavigationListener 和 Sting[]strings 正在 OnCreate 中运行.

The spinner dropdown item in the action bar is being generated, to the right of the actionbar's Title. When I press it I do see the triangle icon turn blue. The OnNavigationListener and Sting[]strings are being ran in OnCreate.

我如何让它发挥作用?

public class Main extends ActionBarActivity {

    private ActionBar actionBar;

    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        actionBar = getActionBar();
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);

        spinner = new Spinner(this);
        SpinnerAdapter mSpinnerAdapter = ArrayAdapter.createFromResource(this,
                R.array.operating_systems, android.R.layout.simple_spinner_dropdown_item);
        spinner.setAdapter(mSpinnerAdapter);

        ActionBar.OnNavigationListener mOnNavigationListener = new ActionBar.OnNavigationListener() {
            String[] strings = getResources().getStringArray(R.array.operating_systems);

            @Override
            public boolean onNavigationItemSelected(int position, long itemId) {
                ListContentFragment newFragment = new ListContentFragment();
                FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
                ft.replace(R.id.fragment_container, newFragment, strings[position]);
                ft.commit();

                return true;
            }
        };
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.main, menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.action_share:
                Toast.makeText(this, "Refresh selected", Toast.LENGTH_SHORT)
                        .show();
                break;
            case R.id.action_settings:
                Toast.makeText(this, "Settings selected", Toast.LENGTH_SHORT)
                        .show();
                break;
            default:
                break;
        }
        return true;
    }

}

推荐答案

看起来不像是在调用 ActionBar.setListNavigationCallbacks.

Doesn't look like you're calling ActionBar.setListNavigationCallbacks.

来自示例中的步骤 4:

使用 setListNavigationCallbacks() 设置下拉列表的回调.例如:

Set the callback for the drop-down list with setListNavigationCallbacks(). For example:

actionBar.setListNavigationCallbacks(mSpinnerAdapter, mNavigationCallback);

这篇关于onNavigationItemSelected 从未调用 Actionbar 的微调器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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