Android user interface-action bar 4)

Source: Internet
Author: User

Create a custom operation provider

When you want to create an operation window with dynamic behaviors and default icons in the overflow menu, it is better to inherit the actionprovider class to define these actions. Create your own operation Providers' and provide an organized and reusable component, instead of processing the changes and actions of various operation items in the code of fragment or activity.

To create your own operation providers', simply inherit the actionprovider class and implement the appropriate callback method. You should implement the following important callback methods:

Actionprovider ()

This constructor transfers the context object of the application to an operator provider. You should save it in a member variable for other callback methods to use.

Oncreateactionview ()

This is where you define the operation window for the menu item. Use the context Object received from the constructor to obtain an instance of the layoutinflater object, fill the operation window with XML resources, and register the event listener. For example:

Public View oncreateactionview (){
// Inflate the action view to be shown on the action bar.
Layoutinflater = layoutinflater. From (mcontext );
View view = layoutinflater. Inflate (R. layout. action_provider, null );
Imagebutton button = (imagebutton) view. findviewbyid (R. Id. Button );
Button. setonclicklistener (New View. onclicklistener (){
@ Override
Public void onclick (view v ){
// Do something...
}
});
Return view;

}

Onperformdefaultaction ()
When selecting the menu in the overflow menu, the system will call this method, and the operation provider should perform the default operation on the selected menu item.

However, if your operations provider provides a sub-menu, even if it overflows the sub-menu of a menu item, you must use the onpreparesubmenu () callback method to display the sub-menu. In this way, onperformdefaaction action () is not called when the sub-menu is displayed.

Note: The activity or frament object that implements the onoptionsitemselected () callback method can overwrite the default behavior of the operator provider by processing the item-selected event (and returns true). In this case, the system does not call the onperformdefaultaction () callback method.

Add navigation option label

When you want to provide a navigation selection label in an activity, using the option label in the Operation column is a good choice (instead of using the tabwidget class ), because the system will adjust the option label in the Operation bar to meet the needs of screens of different sizes-when the screen is wide enough, the navigation option label will be placed in the main operation bar; when the screen is too narrow, the option label will be placed in a separate horizontal bar, as shown in Figure 9 and figure 10.

Figure 9. Operation column option label of the honeycomb gallery Application

Http://developer.android.com/resources/samples/HoneycombGallery/index.html

 

Figure 10. screenshot of the Option tag stacked in the action bar on the narrow screen device

To use the option label to switch between fragmengt, you must execute a fragment transaction each time you select an option label. If you are not familiar with how to use the fragmenttransaction object to change the fragment, read the fragment Development Guide.

First, your layout must contain a viewgroup object that is used to place option labels associated with each fragment object. Make sure that the viewgroup object has a resource ID so that you can reference it in the switch code of the Option label. In addition, if the content of the Option label is filled in the activity layout (excluding the operation bar), the activity does not need any layout (you do not even need to call the setcontentview () method ). Instead, you can place each fragment object in the default root viewgroup object. You can use android. r. id. content ID to reference this viewgroup object (during fragment's execution of the transaction, you can see how to use this ID in the following sample code.

After determining the display position of the fragment object in the layout, the basic process of adding option labels is as follows:

1. Implement the actionbar. tablistener interface. The callback method in this interface will respond to user events on the option label, so that you can switch the fragment object;

2. For each option tag to be added, an actionbar. Tab object must be instantiated, And the settablistener () method must be called to set the event listener of the actionbar. Tab object. You can also use settext () or seticon () to set the title or icon of the Option label.

3. Call addtab () to add each option label to the operation bar.

When viewing the actionbar. tablistener interface, it is noted that the callback method only provides the selected actionbar. Tab object and the fragmenttransaction object that executes the fragment Object Transaction-it does not indicate any fragment switching. Therefore. You must define the association between each of your actionbar. tabs and the appropriate fragment object represented by actionbar. tab (to execute the appropriate fragment transaction ). Depending on your design, there are several different methods to define this association. In the following example, the implementation of the actionbar. tablistener interface provides a constructor, so that each new option tag uses its own listener instance. Each listener instance defines several member variables required to execute transactions on the corresponding fragment object.

For example, the following example shows an implementation of the actionbar. tablistener interface. In this implementation, each option tag uses its own listener instance:

Public static class tablistener <t extends fragment> implements actionbar. tablistener {
Private fragment mfragment;
Private Final Activity mactivity;
Private final string mtag;
Private final class <t> mclass;

/** Constructor used each time a new tab is created.
* @ Param activity the host activity, used to instantiate the fragment
* @ Param tag the identifier tag for the fragment
* @ Param clz the fragment's class, used to instantiate the fragment
*/
Public tablistener (activity, string tag, class <t> clz ){
Mactivity = activity;
Mtag = tag;
Mclass = clz;
}

/* The following are each of
Actionbar. tablistener callbacks */

Public void ontabselected (tab, fragmenttransaction ft ){
// Check if the fragment is already initialized
If (mfragment = NULL ){
// If not, instantiate and add it to the activity
Mfragment = fragment. instantiate (mactivity, mclass. getname ());
Ft. Add (Android. R. Id. Content, mfragment, mtag );
} Else {
// If it exists, simply attach it in order to show it
Ft. Attach (mfragment );
}
}

Public void ontabunselected (tab, fragmenttransaction ft ){
If (mfragment! = NULL ){
// Detach the fragment, because another one is being attached
Ft. Detach (mfragment );
}
}

Public void ontabreselected (tab, fragmenttransaction ft ){
// User selected the already selected tab. Usually do nothing.
}
}

Warning for fragment transactions in each callback, you do not have to call the Commit () method-the system will call this method, and if you call this method yourself, it may throw an exception. You cannot add these fragment transactions to the rollback stack.

In this example, when the corresponding option label is selected, the listener simply attaches a fragment object (attach () method) to the activity layout --- or, if not instantiated, this fragment object will be created, and the (add () method) will be added to the layout (Android. r. id. A subclass of content viewgroup). When this option label is deselected, the corresponding fragment object will also be removed from the dependency with the layout.

Actionbar. tablistener implements a lot of work, and the rest is to create each actionbar. tab object and add it to the actionbar object. In addition, you must call the setnavigationmode (navigation_mode_tabs) method to make the option label visible. If the title of the Option label actually indicates the current view object, you can also disable the title of the activity by calling the setdisplayshowtitleenabled (false) method.

For example, the following code adds two option labels in the action bar using the listener defined above.

@ Override
Protected void oncreate (bundle savedinstancestate ){
Super. oncreate (savedinstancestate );
// Notice that setcontentview () is not used, because we use the root
// Android. R. Id. content as the container for each fragment

// Setup action bar for tabs
Actionbar = getactionbar ();
Actionbar. setnavigationmode (actionbar. navigation_mode_tabs );
Actionbar. setdisplayshowtitleenabled (false );

Tab tab = actionbar. newtab ()
. Settext (R. String. Artist)
. Settablistener (New tablistener <artistfragment> (
This, "artist", artistfragment. Class ));
Actionbar. addtab (Tab );

Tab = actionbar. newtab ()
. Settext (R. String. album)
. Settablistener (New tablistener <albumfragment> (
This, "album", albumfragment. Class ));
Actionbar. addtab (Tab );
}

Note: The above implementation of actionbar. tablistener is only one of several possible technologies. You can see more of this style in the API demos application.

Http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/app/FragmentTabs.html

If the activity is terminated, you should save the status of the selected option tag so that when the user returns again, you can open the appropriate option tag. When the status is saved, you can use the getselectednavigationindex () method to query the selected option tags. This method returns the index location of the selected option tag.

Warning it is critical to save the State required for each fragment, because when you use the option label to switch between fragment objects, it will view the fragment's appearance when it leaves.

Note: In some cases, the android System displays the option label in the Operation column as a drop-down list to ensure optimal display of the Operation column.

Add drop-down navigation

As another internal navigation (or filtering) mode of the activity, the Operation Bar provides a built-in drop-down list. The drop-down list provides different sorting modes for the content in the activity.

The basic process for enabling drop-down navigation is as follows:

1. Create a list of optional projects for the drop-down list and the layout used to describe the projects;

2. Implement the actionbar. onnavigationlistener callback, which defines the behavior that occurs when the user selects a project in the list;

3. Use the setnavigationmode () method to enable the Navigation Mode in this operation bar, for example:

Actionbar = getactionbar ();
Actionbar. setnavigationmode (actionbar. navigation_mode_list );

4. Use the setlistnavigationcallbacks () method to set a callback method for the drop-down list, for example:

Actionbar. setlistnavigationcallbacks (mspinneradapter, mnavigationcallback );

This method requires the spinneradapter and actionbar. onnavigationlistener object.

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.