Androidui component actionbar--based on the pull-down navigation method

Source: Internet
Author: User


In the last blog post on ActionBar , we learned that ActionBar is one of the important updates of Android3.0 . This blog post is to write a frequently used example in development. Use the ActionBar to provide a pull-based navigation method.

The ActionBar of the drop-down navigation generates a drop-down list box at the top, and when the user clicks a list item, the system specifies Fragmentbased on the user's click Navigation.

In order to use ActionBar to implement Tab navigation, follow the steps below.

(1) call ActionBar 's actionbar.setnavigationmode(actionbar.navigation_mode_list ) method to set how the drop-down list is used for navigation.

(2) call ActionBar 's setlistnavigationcallbacks (Spinneradapter adapter, Actionbar.onnavigationlistener callback) () add multiple list items and set up event listeners for each list item. The first parameter, Adapter , is responsible for providing multiple list items, and the second parameter is the event listener.

First take a look at the layout file

<relativelayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http// Schemas.android.com/tools "android:id=" @+id/container "android:layout_width=" Match_parent "android:layout_height= "Match_parent"/>

here the layout file is just as a simple container to load Fragment .

here 's a look.mainactivityThe source code:
<pre class= "java" name= "code" >package com.gc.actionbar_dropdownnav;import Android.os.bundle;import Android.annotation.suppresslint;import Android.app.actionbar;import Android.app.ActionBar.OnNavigationListener; Import Android.app.activity;import Android.app.fragment;import Android.app.fragmenttransaction;import Android.view.menu;import android.widget.arrayadapter;/** * * @author Android General * */@SuppressLint ("Newapi") public class Mainactivity extends Activity implements onnavigationlistener{private static final String selected_item= "Selected_item "; @Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview ( R.layout.activity_main); final ActionBar Actionbar=getactionbar ();// Sets whether Actionbar displays the title actionbar.setdisplayshowtitleenabled (true);//sets the navigation mode, using the list navigation Actionbar.setnavigationmode ( actionbar.navigation_mode_list);//install arrayadapteractionbar.setlistnavigationcallbacks for ActionBar (new ArrayAdapter <String> (Mainactivity.this, Android. R.layoUt.simple_list_item_1,android. R.id.text1,new string[]{"First page", "second page", "third Page"}), this); @Overridepublic boolean onnavigationitemselected (int itemposition, long itemId) {//Create a new fragment object fragment fragment= New Dummyfragment ();//Create a bundle object that is used to pass the parameter bundle Args=new bundle () to fragment; Args.putstring (Dummyfragment.arg_ Section_number, "Android General" + (itemposition+1));//input parameter fragment.setarguments (args) to fragment;// Gets the Fragmenttransaction object Fragmenttransaction Ft=getfragmentmanager (). BeginTransaction ();// Use fragment instead of containerft.replace in the activity (R.id.container, fragment);//COMMIT Transaction Ft.commit (); return true;}

used in this case dummyfragment The source code is as follows:

Package Com.gc.actionbar_dropdownnav;import Android.annotation.suppresslint;import Android.app.fragment;import Android.os.bundle;import Android.view.gravity;import Android.view.layoutinflater;import Android.view.View;import Android.view.viewgroup;import Android.widget.textview;public class Dummyfragment extends fragment{public static final String arg_section_number= "Section_number";//The return value of the method is the View component @suppresslint ("Newapi") displayed by the fragment @Overridepublic View Oncreateview (Layoutinflater inflater, ViewGroup container,bundle savedinstancestate) {//TODO Auto-generated Method Stubtextview textview=new TextView (getactivity ()); textview.setgravity (Gravity.start);// Gets the parameters passed in when the fragment was created Bundlebundle args=getarguments ();//Set TextView displayed text Textview.settext (args.getstring (arg_ Section_number) + ""); Textview.settextsize (+); return textView;}}




The case effect looks like this:

Reprint Please specify source: http://blog.csdn.net/android_jiangjun/article/details/38434965

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.