How to implement a drop down navigation action bar in Android after you've added it?

橙三吉。 提交于 2019-12-10 20:55:43

问题


By following this guide, http://wptrafficanalyzer.in/blog/adding-drop-down-navigation-to-action-bar-in-android/

I was able to add my drop down navigation bar. The click events and everything function. Now, how do I make it so once an option is selected, it navigates to a different screen with its own layout and different functions.

Any help would be great, thanks in advance!

Edit: This is what I have. My app runs for about a millisecond, and I can see a glimpse "Hello World" and then it crashes. I'm using Sherlock by the way, if that matters.

package com.poe.statcalc;

import com.actionbarsherlock.app.SherlockActivity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.Toast;
import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.ActionBar.OnNavigationListener;

public class MainActivity extends SherlockActivity {

 /** An array of strings to populate dropdown list */
String[] actions = new String[] {
    "Bookmark",
    "Subscribe",
    "Share",
    "Something"

};

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    /** Create an array adapter to populate dropdownlist */
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(getBaseContext(), R.layout.sherlock_spinner_item, actions);

    /** Enabling dropdown list navigation for the action bar */
    getSupportActionBar().setNavigationMode(com.actionbarsherlock.app.ActionBar.NAVIGATION_MODE_LIST);

    /** Defining Navigation listener */
    ActionBar.OnNavigationListener navigationListener = new OnNavigationListener() {

        @Override
        public boolean onNavigationItemSelected(int itemPosition, long itemId) {
            switch(itemPosition) {
            case 0:
                Intent i = new Intent(MainActivity.this, SecondActivity.class);
                startActivity(i);
                break;
            case 1:
                //...
                break;
            }
            return false;
        }
    };

    /** Setting dropdown items and item navigation listener for the actionbar */
    getSupportActionBar().setListNavigationCallbacks(adapter, navigationListener);
    adapter.setDropDownViewResource(R.layout.sherlock_spinner_dropdown_item);

}

@Override
public boolean onCreateOptionsMenu(com.actionbarsherlock.view.Menu menu) {
    getSupportMenuInflater().inflate(R.menu.activity_main, menu);
    return super.onCreateOptionsMenu(menu);
}

}


回答1:


You need to maniputate your ArrayAdapter if you want to change the elements, but I don't think you you can use the ArrayAdapter<String> class for that porouse. You may need to use something else than string.

For handling clicks you need to change the onNavigationItemSelected function:

@Override
public boolean onNavigationItemSelected(int itemPosition, long itemId) {
    switch(itemPosition) {
    case 0:
        Intent i = new Intent(this, SecondActivity.class);
        startActivity(i);
        break;
    case 1:
        // ...
        break;
    }
    return false;
}



回答2:


You have to start a new Activity by calling startActivity(Intent) in the onNavigationItemSelected callback.




回答3:


Don't know if this is the exact problem you had (though it sounds like it!), but beware using startActivity from a spinner: it can be called during onCreate().

onNavigationItemSelected in ActionBar is being called at startup how can avoid it?



来源:https://stackoverflow.com/questions/13958912/how-to-implement-a-drop-down-navigation-action-bar-in-android-after-youve-added

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