Android: Sherlock action bar drop down

这一生的挚爱 提交于 2019-12-21 16:59:24

问题


I'm trying to implement a drop down list as navigation for the action bar in Android. I can see the drop down list and the items, but I can't get the clicking event.

I'm not sure what I'm missing since I was following the tutorial in http://developer.android.com/guide/topics/ui/actionbar.html

This is my code:

public void onCreate(Bundle savedInstanceState) {
        OnNavigationListener mOnNavigationListener;
        super.onCreate(savedInstanceState);
        // setContentView(R.layout.info_layout);
        // getSupportActionBar().setHomeButtonEnabled(true);
         getSupportActionBar().setDisplayShowTitleEnabled(false);

        getSupportActionBar().setNavigationMode(getSupportActionBar().NAVIGATION_MODE_LIST);
        SpinnerAdapter mSpinnerAdapter = ArrayAdapter.createFromResource(this, R.array.navigation_array, android.R.layout.simple_dropdown_item_1line);
        mOnNavigationListener = new OnNavigationListener() {
            public boolean onNavigationItemSelected(int itemPosition, long itemId) {
                switch (itemPosition) {
                case 1:
                    Intent i = new Intent();
                    i.setClass(getApplicationContext(), ZoekAndBoekActivity.class);
                    break;
                case 2:
                    break;
                case 3:
                    break;
                case 4:
                    break;
                case 5:
                    break;
                }
                // return super.onOptionsItemSelected(itemPosition);
                return true;
            }
        };
        getSupportActionBar().setListNavigationCallbacks(mSpinnerAdapter, mOnNavigationListener);
    }

Thanks a lot in advance!


回答1:


Are you sure that you don't get click events? You're creating intent but doesn't do anything with it. Try something like this:

switch (itemPosition) {
    case 1:
        Intent i = new Intent();
        i.setClass(getApplicationContext(), ZoekAndBoekActivity.class);
        startActivity(i);
        break;
    ...
}

or add writing to log to be sure:

public boolean onNavigationItemSelected(int itemPosition, long itemId) {
    Log.d("SomeTag", "Get click event at position: " + itemPosition);
    switch (itemPosition) {
        ...
    }
}

and see in the logcat output for message with "SomeTag" when you click on items.




回答2:


I think the return statement must be false inside the switch case, and your case must have brackets.. Hope it helps :)))



来源:https://stackoverflow.com/questions/10735176/android-sherlock-action-bar-drop-down

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!