Efficient Mono for Android navigation

Source: Internet
Author: User

The Android 4.0 system defines a series of efficient navigations, including tags, drop-down lists, and up and back, this article describes how to use Mono for Android to implement these navigation methods.

Prepare the Android 4.0 ICS project to create the Android ICS project

Open MonoDevelop, create a Mono for Android project, and set Target FrameworkAndroid 4.0.3 (Ice Cream Sandwich), As shown in:

Add Mono. Android. Support. v4 reference item

In the solution window, select the reference node of the Project, right-click Edit reference, and addMono.Android.Support.v4.dllReference ,:

Create a new directory SupportLib in the project and add references to the android-support-v4.jar file (located in the android-sdk/extras/android/support/v4 directory, if not, you need to install it with SDK Manager, set the BuildAction of the jar file to AndroidJavaLibrary, as shown in:

The navigation mentioned in this article is implemented according to the ActionBar recommended in Android 4.0 design specifications. Therefore, the whole application enables the topic with ActionBar. If Java is used, you need to manually edit AppManifest. but with Mono for Android, you basically do not need to manually edit this file.

Mono for Android creates an App class that inherits fromAndroid.App.ApplicationClass, and addAndroid.App.ApplicationAttributeDuring compilation, Mono for Android will automatically generate an AppManifest. xml file based on these tags and package it into the final apk file.

The App class code is as follows:

[Application(Label = "@string/AppName", Icon = "@drawable/ic_launcher", Theme = "@android:style/Theme.Holo.Light.DarkActionBar")]public class App : Application {public App(IntPtr javaReference, JniHandleOwnership transfer): base(javaReference, transfer) {}}

After this class is added, this topic is used by default for each Activity in the project. If other themes are used for an Activity, you must add your own topic attributes.

Tag navigation

Android labels are implemented using ActionBar. You can click the tag to switch to the view or switch to the view horizontally, as shown in:

You can click the 'section 0', 'section 1', and 'section 2' labels above to switch to the view, or drag the switch view horizontally on The View, the selected tag items must also be selected synchronously. The implementation code is as follows:

[Activity (Label = "@ string/AppName", Icon = "@ drawable/ic_launcher", MainLauncher = true)] public class MainActivity: fragmentActivity {// <summary> // AppSectionsPagerAdapter provides the view to be displayed, inherited from // Mono. android. support. v4.View. pagerAdapter. All loaded views are stored in the memory. // if the view occupies too much memory, replace it with the FragmentStatePagerAdapter. /// </Summary> AppSectionsPagerAdapter _ appSectionsPagerAdapter; // <summary> /// ViewPager is used to display only one of the three views at a time. /// </Summary> ViewPager _ viewPager; protected override void OnCreate (Bundle bundle) {base. onCreate (bundle); this. setContentView (Resource. layout. mainActivity); // create Adapterthis. _ appSectionsPagerAdapter = new AppSectionsPagerAdapter (this. supportFragmentManager); // sets ActionBarvar actionBar = this. actionBar; // The Home page does not need the up Home button actionBar. setHomeButtonEnabled (false); // sets the tag Navigation Mode actionBar. navigationMode = Ac TionBarNavigationMode. tabs; // set the Adapter of ViewPager so that you can switch the view horizontally. _ viewPager = this. findViewById <ViewPager> (Resource. id. pager); this. _ viewPager. adapter = this. _ appSectionsPagerAdapter; // when switching views horizontally, set the selected label this. _ viewPager. pageSelected + = delegate (object sender, ViewPager. pageSelectedEventArgs e) {actionBar. setSelectedNavigationItem (e. p0) ;}; // Add three tags in sequence, add the selected event handler function of the tag, and set the current view. For (var I = 0; I <this. _ appSectionsPagerAdapter. count; I ++) {var tab = actionBar. newTab (). setText (this. _ appSectionsPagerAdapter. getPageTitle (I); tab. tabSelected + = delegate (object sender, Android. app. actionBar. tabEventArgs e) {this. _ viewPager. currentItem = tab. position ;}; actionBar. addTab (tab );}}}
Left and right navigation

Label navigation is not suitable for all scenarios. Sometimes you only need to display the view title, but you can also switch the view horizontally, as shown in:

This navigation method is equivalent to a simplified version of the label-based navigation. you can slide the left and right to switch the view. The implementation code is as follows:

Protected override void OnCreate (Bundle bundle) {base. onCreate (bundle); this. setContentView (Resource. layout. collectionDemoActivity); // create Adapterthis. _ demoCollectionPagerAdapter = new DemoCollectionPagerAdapter (this. supportFragmentManager); // sets the Adapterthis of ViewPager. _ viewPager = this. findViewById <ViewPager> (Resource. id. pager); this. _ viewPager. adapter = this. mDemoCollectionPagerAdapter ;}

To display the title, a PagerTitleStrip is added to Layout of the Activity. The source code of Layout is as follows:

<Android. support. v4.view. viewPager xmlns: android = "http://schemas.android.com/apk/res/android" android: id = "@ + id/Pager" android: orientation = "vertical" android: layout_width = "match_parent" android: layout_height = "match_parent"> <! -- PaterTitleStrip: displays the title of the selected page, and displays the titles of the selected views. --> <android. support. v4.view. pagerTitleStrip android: id = "@ + id/PagerTitleStrip" android: layout_width = "match_parent" android: layout_height = "wrap_content" android: layout_gravity = "top" android: background = "#33b5e5" android: textColor = "# fff" android: paddingTop = "4dp" android: paddingBottom = "4dp"/> </android. support. v4.view. viewPager>
Drop-down list

In the drop-down list navigation, a menu is displayed in the ActionBar. Just like a menu, only the view corresponding to the selected menu item is displayed, as shown in:

When you set the ActionBar as a drop-down list navigation, the title of the Activity is generally not displayed. Therefore, you need to mark the Label of the Activity as an empty string, and the Activity needs to implement the interfaceActionBar.IOnNavigationListener, Some implementation code of ListNavigationActivity is as follows:

[Activity (Label = "")] public class ListNavigationActivity: FragmentActivity, ActionBar. IOnNavigationListener {ListNavSectionsPagerAdapter _ navSectionsPagerAdapter; protected override void OnCreate (Bundle bundle) {base. onCreate (bundle);/* Other Code omitted... * /// Set ActionBarvar actionBar = this. actionBar; // set Home to up actionBar. setDisplayHomeAsUpEnabled (true); // set the ActionBar navigation mode to the actionBar drop-down list. navigationMode = ActionBarNavigationMode. list; var titles = new string [this. _ navSectionsPagerAdapter. count]; for (var I = 0; I <titles. length; I ++) {titles [I] = this. _ navSectionsPagerAdapter. getPageTitle (I);} // sets the callback parameter actionBar OF THE LIST navigation. setListNavigationCallbacks (n Ew ArrayAdapter (actionBar. themedContext, Resource. layout. listNavigationActivityActionbarListItem, Android. resource. id. text1, titles), this); // sets ViewPagerthis. _ viewPager = this. findViewById <ViewPager> (Resource. id. pager); this. _ viewPager. adapter = this. _ navSectionsPagerAdapter; // synchronize the selected items of the actionBar when the selected page of ViewPager is switched. This. _ viewPager. pageSelected + = delegate (object sender, ViewPager. pageSelectedEventArgs e) {actionBar. setSelectedNavigationItem (e. p0) ;}}// ActionBar. IOnNavigationListenerpublic bool OnNavigationItemSelected (int itemPosition, long itemId) {this. _ viewPager. currentItem = itemPosition; return true ;}}
Navigation up

In the upward navigation, a left arrow is displayed on the Activity icon. Click the icon to return the previous Activity of the application. Note that the previous Activity is not the previous Activity, for the difference between going up and returning, let's take a look at the Providing Ancestral and Temporal Navigation document in the Android SDK. I will explain the above and returning very clearly. Here I will only discuss how Mono for Android is implemented.

To display the up navigation button, you mustOnCreateSet the ActionBar as follows:

// Set ActionBarvar actionBar = this. ActionBar; // The Home button is displayed as up, prompting the user to click this button to return to the upper level of the application. ActionBar. SetDisplayHomeAsUpEnabled (true );

At the same time, rewrite is required.OnOptionsItemSelectedMethod. When the user clicks the Home button, the corresponding processing is implemented. The code for the upward navigation is as follows:

Public override bool OnOptionsItemSelected (Android. Views. IMenuItem item) {// as an example, only the user clicks the Home button. If (item. itemId = Android. resource. id. home) {// when the Home button is clicked, it will be called here // create Intentvar upIntent = new Intent (this, typeof (MainActivity) that starts the parent Activity )); // use NavUtils in Suport Package to correctly process the if (NavUtils. shouldUpRecreateTask (this, upIntent) {// The parent Activity has not been started. You need to create a new navigation track TaskStackBuilder. create (this) // If there are ancestor activities, they shoshould be added here .. addNextIntent (upIntent ). startAc Tivities (); this. Finish ();} else {// The upper-level Activity has been created. Just navigate. NavUtils. NavigateUpTo (this, upIntent);} return true;} return base. OnOptionsItemSelected (item );}
Summary

The navigation of the Android system is much more complicated than that of the iOS system, and it is also relatively troublesome to implement it. Fortunately, many of the Google Support Package operations have provided better encapsulation, which is easier to grasp. The complete source code in this article has been submitted on Github, the address is https://github.com/beginor/MonoDroid/tree/master/EffectiveNavigation.

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.