To add a drop-down menu in Actionbar, there are a few key steps:
1. Generate a spinneradapter, set the menu item for the Actionbar drop-down menu
2. Implement the Actionbar.onnavigationlistener interface, when clicking on the ActionBar menu item is the appropriate action
3. Call the Setnavigationmode () method to set the Actionbar operation model to Actionbar.navigation_mode_list. Note: This step should be performed when the activity's OnCreate () callback function
4. Call the Setlistnavigationcallbacks () method to add adapters and listeners for Actionbar.
Here is the example code, this example is just a simple test, arrayadapter is the indirect subclass of Spinneradapter, where the menu item in Actionbar is an array of data, when you click on one of the menu items, A corresponding fragment will be generated instead of the original fragment.fragment there is also a simple TextView control, which is used to display the selected information.
Custom fragment:
Public classStudentinfo extends fragment{//fragment the corresponding label, when fragment is attached to the activity to get PrivateString tag; @Override Public voidOnattach (activity activity) {Super.onattach (activity); Tag=Gettag (); } @Override PublicView Oncreateview (layoutinflater inflater, ViewGroup container, Bundle savedinstancestate) {TextView TEXTV Iew=NewTextView (Getactivity ()); Textview.settext (tag); returnTextView; }}
An array is defined in Strings.xml, which is the information for the menu item:
<?xml version="1.0" encoding="utf-8"? ><resources > <string name="app_name">actionbar drop-down menu </string > <string-array name="student"> <item > Deng </item> <item> Liu Tianxi </item> <item> its Yellowstone </item> <item> Cai Hongyuan </item> </string-array></resources>
The main program fragment, Mainactivity:
Public classMainactivity extends activity{/** Called when the activity is first created.*/@Override Public voidonCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.main); //Generate a SpinneradapterSpinneradapter adapter = Arrayadapter.createfromresource ( This, R.array.student, Android. R.layout.simple_spinner_dropdown_item); //Get ActionbarActionBar ActionBar =Getactionbar (); //Set the Actionbar operation model to Navigation_mode_listActionbar.setnavigationmode (actionbar.navigation_mode_list); //setting up drop-down menus and listeners for ActionbarActionbar.setlistnavigationcallbacks (Adapter,NewDropdownlistenser ()); } /** * Implement Actionbar.onnavigationlistener interface*/ classDropdownlistenser implements Onnavigationlistener {//get an array of characters consistent with Spinneradapterstring[] ListNames =getresources (). Getstringarray (r.array.student); /*When you select a drop-down menu item, replace the contents of the activity with the corresponding fragment*/ PublicBoolean onnavigationitemselected (intItemposition,LongitemId) { //generate a custom fragmentStudentinfo student =NewStudentinfo (); Fragmentmanager Manager=Getfragmentmanager (); Fragmenttransaction Transaction=manager.begintransaction (); //Replace the contents of the activity with the fragment of the corresponding selectionTransaction.replace (R.id.context, student, listnames[itemposition]); Transaction.commit (); return true; } }}
Drop-down menu in Android actionbar