AndroidUI component-ActionBar-based drop-down navigation
In the previous blog on ActionBar, we learned that ActionBar is one of the important updates of Android3.0. This blog post describes an example that is frequently used in development. ActionBar provides a drop-down-based navigation mode.
The ActionBar in the drop-down navigation bar generates a drop-down list box at the top. When you click a list item, the system specifies the Fragment according to the user's click in the navigation bar.
To use ActionBar to implement Tab navigation, follow these steps.
(1) Call the ActionBar. setNavigationMode (actionBar. NAVIGATION_MODE_LIST) method of the ActionBar to set the navigation method using the drop-down list.
(2) Call setListNavigationCallbacks (SpinnerAdapter adapter, ActionBar. OnNavigationListener callback) () of the ActionBar to add multiple list items and set the event listener for each list item. The first parameter Adapter provides multiple list items, and the second parameter is the event listener.
First, let's look at the layout file.
The layout file is used as a simple container to load Fragment.
Next let's take a look at the MainActivity source 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 the ActionBar displays the title actionBar. setDisplayShowTitleEnabled (true); // sets the navigation mode and uses the List navigation actionBar. setNavigationMode (ActionBar. NAVIGATION_MODE_LIST); // install ArrayAdapteractionBar for actionBar. setListNavigationCallbacks (new ArrayAdapter
(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 to pass the Bundle args parameter to Fragment = new Bundle (); args. putString (DummyFragment. ARG_SECTION_NUMBER, "Android General" + (itemPosition + 1); // input the fragment parameter to fragment. setArguments (args); // get the FragmentTransaction object FragmentTransaction ft = getFragmentManager (). beginTransaction (); // use fragment to replace containerft in the Activity. replace (R. id. container, fragment); // submit the transaction ft. commit (); return true ;}}
The source code of DummyFragment used in this case 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 this method is the View component displayed by this Fragment @ SuppressLint ("NewApi ") @ Overridepublic View onCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {// TODO Auto-generated method stubTextView textView = new TextView (getActivity (); textView. setGravity (Gravity. START); // obtain the BundleBundle args = getArguments () parameter passed in when this Fragment is created; // set the text TextView displayed in textView. setText (args. getString (ARG_SECTION_NUMBER) + ""); textView. setTextSize (30); return textView ;}}
Shows the results of the case:
Reprinted please indicate the source: http://blog.csdn.net/android_jiangjun/article/details/38434965