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

Source: Internet
Author: User


In the previous article about ActionBar 's blog post. We know that ActionBar is one of the important updates of Android3.0 . This blog post will write a sample that has been used frequently 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. When the user clicks on a list item, the system specifies Fragmentby the user clicking Navigation.

In order to use ActionBar to implement Tab navigation. You can do this by doing the following steps, for example.

(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) () joins multiple list items. and set up event listeners for each list item. The first of these Adapter is responsible for providing multiple list items, and the second 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"/>

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

take a look down .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. Used to pass in the fragment bundle Args=new bundle (); Args.putstring (Dummyfragment.arg_section_number, "Android General" + ( itemposition+1));//Fragment incoming fragment.setarguments (args);//Get 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 for example is the following:

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




Case effects such as what you see:

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

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

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.