Android navigation type

Source: Internet
Author: User

Drop-down Navigation:

Final ActionBar actionBar = getSupportActionBar (); // sets whether the ActionBar displays the title actionBar. setDisplayShowTitleEnabled (false); // sets the navigation mode and uses the List navigation actionBar. setNavigationMode (ActionBar. NAVIGATION_MODE_LIST); // sets the list item data source and listener actionBar for the navigation. setListNavigationCallbacks (// Specify a SpinnerAdapter to populate the dropdown list. new ArrayAdapter
 
  
(// Set the list item actionBar for the navigation. getThemedContext (), android. r. layout. simple_list_item_1, android. r. id. text1, new String [] {getString (R. string. title_section1), getString (R. string. title_section2), getString (R. string. title_section3),}), this); // this sets the listener ActionBar for the navigation. onNavigationListener, as shown below // This method is triggered When the navigation is selected @ Overridepublic boolean onNavigationItemSelected (int position, long id) {// When the given dropdown item is selected, show its contents in the // container view. getsuppfrfragmentmanager (). beginTransaction (). replace (R. id. container, PlaceholderFragment. newInstance (position + 1 )). commit (); return true;} // internal class, dynamically created based on the selected list id and returned Fragment class public static class PlaceholderFragment extends Fragment {}
 

ActionBar implements Tab Navigation:

// Set the Tabs navigation final ActionBar = getSupportActionBar (); actionBar. setNavigationMode (ActionBar. vertex); // FragmentPaperAdapter object (attached below). This adapter returns the corresponding Fragment mSectionsPagerAdapter = new SectionsPagerAdapter (getsuppfrfragmentmanager () according to the selection. // ViewPaper is the container of Fragment, you can manage multiple Fragment at the same time and allow multiple Fragment switches to provide animation effects. You need to Set the adapter FragmentPagerAdapter // Set up the ViewPager with the sections adapter. mViewPager = (ViewPager) findViewById (R. id. pager); mViewPager. setAdapter (mSectionsPagerAdapter); // sets a listener for ViewPaper. This method is triggered when the Fragment displayed by ViewPaper changes. setOnPageChangeListener (new ViewPager. simpleOnPageChangeListener () {@ Override public void onPageSelected (int position) {actionBar. setSelectedNavigationItem (position) ;}}); // traverses all Fragment contained in the paperAdapter object. Each Fragment corresponds to the creation of a Tab, and set the event listening interface object TabListener for (int I = 0; I <mSectionsPagerAdapter. getCount (); I ++) {// Create a tab with text corresponding to the page title defined by // the adapter. also specify this Activity object, which implements // the TabListener interface, as the callback (listener) for when // this tab is selected. actionBar. addTab (actionBar. newTab (). setText (mSectionsPagerAdapter. getPageTitle (I )). setTabListener (this);} public class SectionsPagerAdapter extends FragmentPagerAdapter {public SectionsPagerAdapter (FragmentManager fm) {super (fm );} get the Fragment @ Override public Fragment getItem (int position) {// getItem is called to instantiate the fragment for the given page. // Return a PlaceholderFragment (defined as a static inner class below ). return PlaceholderFragment. newInstance (position + 1);} // the return value of this method I indicates the total number of Fragment @ Override public int getCount () {// Show 3 total pages. return 3;} // the return value of this method determines the title of each Fragment @ Override public CharSequence getPageTitle (int position) {Locale l = Locale. getDefault (); switch (position) {case 0: return getString (R. string. title_section1 ). toUpperCase (l); case 1: return getString (R. string. title_section2 ). toUpperCase (l); case 2: return getString (R. string. title_section3 ). toUpperCase (l);} return null ;}}

TabHost provides navigation in two ways:

General steps for using the Tab
First, design the page layout.
Activity inherits TabActivity
Call the getTabHost () method of TabActivity to obtain the TabHost object.
Create a Tab using TabHost


TabHost: Core class of the tag control, a set of tags
TabHost. TabSpec: Tag object. You can load the View. Such as a control or layout

Code Description:

// Declare the TabHost and use LayoutInflater to filter out the layout. Add the FrameLayoutTabHost myTabhost containing the Tab page to the TabHost. getTabHost (); // obtain the TabHostLayoutInflater where the Tab is placed from the TabActivity. from (this ). inflate (R. layout. main, myTabhost. getTabContentView (), true); // from (this) Get LayoutInflater/R from this TabActivity. layout. main stores the Tab layout // obtains the FrameLayout that stores the Tab content through TabHost. // determines whether to tie inflate to the root layout element and create a label on TabHost, set the title/icon/TAB layout to myTabhost. addTab (myTabhost. newTabSpec ("TT") // create a new tag TT. setIndicator ("KK", getResources (). getDrawable (R. drawable. (ajjc) // set the title to KK and the label icon to ajjc. setContent (R. id. widget_layout_red); // set the layout of this tab to R. id. widget_layout_red, which is a sub-Layout in FrameLayout.

NavigationDrawer implements Tab Navigation:

// Extends Fragment implements the corresponding navigation callback method public void onNavigationDrawerItemSelected (int position) implements NavigationDrawerFragment. navigationDrawerCallbacks mNavigationDrawerFragment = (NavigationDrawerFragment) getsuppfrfragmentmanager (). findFragmentById (R. id. navigation_drawer); mTitle = getTitle (); // Set up the drawer. mNavigationDrawerFragment. setUp (R. id. navigation_drawer, (DrawerLayout) findViewById (R. id. drawer_layout); // return the corresponding Fragment object @ Override public void merge (int position) {// update the main content by replacing Fragment manager FragmentManager fragmentManager = getSupportFragmentManager (); fragmentManager. beginTransaction (). replace (R. id. container, PlaceholderFragment. newInstance (position + 1 )). commit ();} // the return value of this method determines the title of each Fragment public void onSectionAttached (int number) {switch (number) {case 1: mTitle = getString (R. string. title_section1); break; case 2: mTitle = getString (R. string. title_section2); break; case 3: mTitle = getString (R. string. title_section3); break ;}// set the public void restoreActionBar () {ActionBar actionBar = getSupportActionBar (); actionBar. setNavigationMode (ActionBar. NAVIGATION_MODE_STANDARD); actionBar. setDisplayShowTitleEnabled (true); actionBar. setTitle (mTitle );}

ScrollableTab enables Tab Navigation:

// Create the adapter that will return a fragment for each of the three// primary sections of the app.mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());// Set up the ViewPager with the sections adapter.mViewPager = (ViewPager) findViewById(R.id.pager);mViewPager.setAdapter(mSectionsPagerAdapter);public class SectionsPagerAdapter extends FragmentPagerAdapter {public SectionsPagerAdapter(FragmentManager fm) {super(fm);}@Overridepublic Fragment getItem(int position) {// getItem is called to instantiate the fragment for the given page.// Return a DummySectionFragment (defined as a static inner class// below) with the page number as its lone argument.Fragment fragment = new DummySectionFragment();Bundle args = new Bundle();args.putInt(DummySectionFragment.ARG_SECTION_NUMBER, position + 1);fragment.setArguments(args);return fragment;}@Overridepublic int getCount() {// Show 3 total pages.return 3;}@Overridepublic CharSequence getPageTitle(int position) {Locale l = Locale.getDefault();switch (position) {case 0:return getString(R.string.title_section1).toUpperCase(l);case 1:return getString(R.string.title_section2).toUpperCase(l);case 2:return getString(R.string.title_section3).toUpperCase(l);}return null;}


Related Article

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.