Mono for Android enables efficient navigation (effective navigation) _android

Source: Internet
Author: User

The Android 4.0 system defines a range of efficient navigation methods (effective navigation), including tags, drop down lists, and up and go, and how to use Mono for Android to navigate these ways.

Prepare the Android 4.0 ICS project

New Android ICS Project

Open MonoDevelop, create a new Mono for Android project, and set the Target Framework to the project's property page as shown in the Android 4.0.3 (Ice Cream Sandwich) following illustration:

Add MONO.ANDROID.SUPPORT.V4 Reference Item

In the Solution window, select the reference node for the project, right-click to select the edit reference, and add Mono.Android.Support.v4.dll a reference to it, as shown in the figure:

Create a new directory Supportlib in the project and add the Android-support-v4.jar file (located in the Android-sdk/extras/android/support/v4 directory, if not, need SDK Manager installation, and set the Compile action (buildaction) of the jar file to Androidjavalibrary, as shown in the following illustration:

The navigation mentioned in this article is implemented according to the Actionbar recommended in the Android 4.0 design specification, so the entire application is enabled with Actionbar themes, and if you use Java, you need to manually edit the Appmanifest.xml file settings, Mono for Android, there's basically no need to manually edit this file.

Mono for Android is to create a new APP class, inherit from Android.App.Application the class, and add Android.App.ApplicationAttribute tags, at compile time, Mono for Android automatically generates a Appmanifest.xml file based on these tags and packs it to the most End of the apk file.

the code for the App class is as follows :

Copy Code code 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 you add this class, each activity in your project will default to this topic, and you need to add your own theme properties if you have an activity that uses other topics.

Label Navigation

Android tags are implemented with Actionbar, which allows users to switch views either by clicking the tabs or by sliding the view horizontally, as shown in the following illustration:

The user can either click on the ' section 0 ', ' section 1 ', ' section 2 ' tab above to toggle the view, or drag the view horizontally on the view, while the label selection will be synchronized to select the following code:

Copy Code code as follows:

[Activity (Label = "@string/appname", Icon = "@drawable/ic_launcher", Mainlauncher = True)]
public class Mainactivity:fragmentactivity {

<summary>
Appsectionspageradapter provides the view to display, inherited from
Mono.Android.Support.V4.View.PagerAdapter, all loaded views are saved in memory,
If the view consumes too much memory, consider replacing it with Fragmentstatepageradapter.
</summary>
Appsectionspageradapter _appsectionspageradapter;

<summary>
Use Viewpager to display three views of the view, one at a time.
</summary>
Viewpager _viewpager;

protected override void OnCreate (Bundle Bundle) {
Base. OnCreate (bundle);

This. Setcontentview (Resource.Layout.MainActivity);

Create Adapter
This._appsectionspageradapter = new Appsectionspageradapter (this. Supportfragmentmanager);
Set Actionbar
var Actionbar = this. Actionbar;
Home does not need the up button
Actionbar.sethomebuttonenabled (FALSE);
Set Label Navigation mode
Actionbar.navigationmode = Actionbarnavigationmode.tabs;
Set the Viewpager Adapter so that the user can swipe horizontally to toggle the view
This._viewpager = this. Findviewbyid<viewpager> (Resource.Id.Pager);
This._viewpager.adapter = This._appsectionspageradapter;
Set the selected label when sliding horizontally to toggle the view
this._viewpager.pageselected + = Delegate (object sender, Viewpager.pageselectedeventargs e) {
Actionbar.setselectednavigationitem (E.P0);
};

Add three labels in turn, and add the selected event handler for the label to 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 it just needs to display the caption of the view, but you can also slide the view horizontally, as shown in the following illustration:

This kind of navigation is equivalent to the simplified version of tabbed navigation, the user can only slide left and right to switch views, the implementation of the code is as follows:

Copy Code code as follows:

protected override void OnCreate (Bundle Bundle) {
Base. OnCreate (bundle);
This. Setcontentview (Resource.Layout.CollectionDemoActivity);
Create Adapter
This._democollectionpageradapter = new Democollectionpageradapter (this. Supportfragmentmanager);

Set the Viewpager Adapter
This._viewpager = this. Findviewbyid<viewpager> (Resource.Id.Pager);
This._viewpager.adapter = This.mdemocollectionpageradapter;
}

Because the caption is displayed, the Layout of this activity adds a pagertitlestrip, Layout the source code as follows:
Copy Code code 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 to display the title of the selected page, as well as the titles of several views near the selected view
-->
<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

Drop-down list navigation is to display a drop-down list (Spinner) in Actionbar, like a menu that displays only the view of the selected menu item, as shown in the following illustration:

When you set Actionbar as a drop-down list navigation, you generally do not display the title of the activity itself, so you need to mark the activity's label as an empty string, and the activity needs to implement the interface ActionBar.IOnNavigationListener , listnavigationact The partial implementation code for Ivity is as follows:

Copy Code code 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 Actionbar
var Actionbar = this. Actionbar;
Set home to Up
Actionbar.setdisplayhomeasupenabled (TRUE);
Set Actionbar navigation mode to Drop-down list
Actionbar.navigationmode = actionbarnavigationmode.list;

var titles = new String[this._navsectionspageradapter.count];
for (var i = 0; i < titles. Length; i++) {
Titles[i] = This._navsectionspageradapter.getpagetitle (i);
}
Set callback parameters for list navigation
Actionbar.setlistnavigationcallbacks (
New Arrayadapter (
Actionbar.themedcontext,
Resource.Layout.ListNavigationActivityActionbarListItem,
ANDROID.RESOURCE.ID.TEXT1,
Titles
),
This
);
Set Viewpager
This._viewpager = this. Findviewbyid<viewpager> (Resource.Id.Pager);
This._viewpager.adapter = This._navsectionspageradapter;
Synchronizes the selected items of the Actionbar when the Viewpager selected page toggles.
this._viewpager.pageselected + = Delegate (object sender, Viewpager.pageselectedeventargs e) {
Actionbar.setselectednavigationitem (E.P0);
};
}

Actionbar.ionnavigationlistener
public bool onnavigationitemselected (int itemposition, long itemId) {
This._viewpager.currentitem = itemposition;
return true;
}
}

Navigate Up

The so-called up navigation is to display a left arrow on the activity's icon, and click on the icon to return to the top level of activity on the application, note that the previous activity, not the last activity, about the difference between up and return, you can look at Android The providing ancestral and temporal navigation in the SDK will explain up and down very clearly, where only the Mono for Android implementation is discussed.

To display a button that navigates up, you need OnCreate to set the Actionbar in the method as follows:

Copy Code code as follows:

Set Actionbar
var Actionbar = this. Actionbar;
Displays the home button as up, prompting the user to click this button to return to the previous level of the application.
Actionbar.setdisplayhomeasupenabled (True), and also need to rewrite the onoptionsitemselected method, when the user clicks the Home button, do the appropriate processing, to achieve the following code to navigate up:

public override bool Onoptionsitemselected (Android.Views.IMenuItem Item) {
As an example, only the user clicking the Home button is handled.
if (item. ItemId = = Android.Resource.Id.Home) {
When the home button is clicked, it is called here
Create a Intent that initiates a parent activity
var upintent = new Intent (this, typeof (Mainactivity));
Use the navutils in Suport Package to properly handle up navigation
if (Navutils.shoulduprecreatetask (this, upintent)) {
The superior activity has not started, needs to create a new navigation plank Road
Taskstackbuilder.create (This)
If There are ancestor activities, they should is added here.
. Addnextintent (upintent)
. Startactivities ();
This. Finish ();
}
else {
The parent activity has been created, direct navigation on the line.
Navutils.navigateupto (this, upintent);
}
return true;
}
Return base. onoptionsitemselected (item);
}

Summarize

Android's navigation is a lot more complicated than IOS, and it's a bit more cumbersome to implement, but it's good to have Google's Support Package has provided a better package for most operations, or easier to master. The text of the complete source code has been submitted on the 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.