tionItemSelected从来没有号召ActionBar的

tionItemSelected从来没有号召ActionBar的

本文介绍了onNavigationItemSelected从来没有号召ActionBar的微调的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图建立一个下拉操作栏中的COMPAT菜单,按dev的例子:的

我不能让onNavigationItemSelected火虽然。

在操作栏上的微调下拉项目正在生成到的动作条的标题的权利。当我preSS是我看到的三角形图标变成蓝色。该OnNavigationListener和Sting []字符串是跑在OnCreate中。

我将如何得到这个工作?

 公共类主要扩展ActionBarActivity {

    私人ActionBar的动作条;

    @覆盖
    公共无效的onCreate(包savedInstanceState){

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

        动作条= getActionBar();
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);

        微调=新的微调(本);
        SpinnerAdapter mSpinnerAdapter = ArrayAdapter.createFromResource(这一点,
                R.array.operating_systems,android.R.layout.simple_spinner_dropdown_item);
        spinner.setAdapter(mSpinnerAdapter);

        ActionBar.OnNavigationListener mOnNavigationListener =新ActionBar.OnNavigationListener(){
            的String []字符串= getResources()getStringArray(R.array.operating_systems)。

            @覆盖
            公共布尔onNavigationItemSelected(INT位置,长的itemId){
                ListContentFragment newFragment =新ListContentFragment();
                FragmentTransaction英尺= getSupportFragmentManager()的BeginTransaction()。
                ft.replace(R.id.fragment_container,newFragment,串[位置]);
                ft.commit();

                返回true;
            }
        };
    }

    @覆盖
    公共布尔onCreateOptionsMenu(功能菜单){
        MenuInflater充气= getMenuInflater();
        inflater.inflate(R.menu.main,菜单);
        返回super.onCreateOptionsMenu(菜单);
    }

    @覆盖
    公共布尔onOptionsItemSelected(菜单项项){
        开关(item.getItemId()){
            案例R.id.action_share:
                Toast.makeText(这一点,刷新选择,Toast.LENGTH_SHORT)
                        。显示();
                打破;
            案例R.id.action_settings:
                Toast.makeText(这一点,设置中选择,Toast.LENGTH_SHORT)
                        。显示();
                打破;
            默认:
                打破;
        }
        返回true;
    }

}
 

解决方案

看起来不像你调用 ActionBar.setListNavigationCallbacks

第4步的例子:

  actionBar.setListNavigationCallbacks(mSpinnerAdapter,mNavigationCallback);
 

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

I can't get onNavigationItemSelected to fire though.

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.

How would I get this to work?

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

}
解决方案

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

From step 4 in the example:

actionBar.setListNavigationCallbacks(mSpinnerAdapter, mNavigationCallback);

这篇关于onNavigationItemSelected从来没有号召ActionBar的微调的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-01 12:51