Add operation window
The operation window is a visible component displayed in the Operation Bar as a substitute for the Operation item button. For example, if you have an optional menu item for search, you can use the searchview class to replace the search button on the Operation bar, as shown in 7:
Figure 7. Operation bar for folding (top) and expanding (bottom) search windows
To declare an operation window for a project in a menu resource, you can use both the Android: actionlayout attribute and the Android: actionviewclass attribute to specify a layout resource or the Visible Component class to be used. For example:
<? XML version = "1.0" encoding = "UTF-8"?>
<Menu xmlns: Android = "http://schemas.android.com/apk/res/android">
<Item Android: Id = "@ + ID/menu_search"
Android: Title = "@ string/menu_search"
Android: icon = "@ drawable/ic_menu_search"
Android: showasaction = "ifroom | collapseactionview"
Android: actionviewclass = "android. widget. searchview"/>
</Menu>
Android: The showasaction attribute can also contain the "collapseactionview" attribute value, which is optional and declares that the Operation window should be folded into a button. When you select this button, this operation window is displayed. Otherwise, this operation window is visible by default, and occupies a valid space in the Operation Column even if it is not applicable.
If you need to add some events to the operation window, you need to do this during the oncreateoptionsmenu () callback execution. You can call the finditem () method with the menu item ID to obtain the menu item, and then call the element in the getactionview () side operation window. For example, use the following method to obtain the search window component in the previous example.
@ Override
Public Boolean oncreateoptionsmenu (menu ){
Getmenuinflater (). Inflate (R. Menu. Options, menu );
Searchview = (searchview) menu. finditem (R. Id. menu_search). getactionview ();
// Configure the search info and add any event listeners
...
Return super. oncreateoptionsmenu (menu );
}
Process foldable operation windows
The operation window allows you to provide quick access and rich operations without changing the activity or fragment. However, it may be inappropriate to make the operation window visible by default. Ensure that the space in the Operation bar (especially when running on a small screen device) is able to fold the operation window into an operation button. When you select this button, the Operation window is displayed in the Operation bar. If you define the Android: showasaction = "ifroom" attribute when it is folded, the system may put this item in the overflow menu, but when the user selects this menu item, it will still be displayed in the Operation bar. You can fold the operation window by adding the "collapseactionview" attribute value to the Android: showasaction attribute.
When you select this project, the system will display this operation window, so you do not need to respond to this menu item in the onoptionsitemselected () callback method. When you select this menu item, the system will still call the onoptionsitemselected () method, but unless you return true in the method (indicating that you have replaced the system to process this event ), otherwise, the system will always expand this operation window.
When you select the "up" icon in the Operation bar or press the "back" button, the system also folds the operation window.
You can expand or collapse the operation window in the Code by using the expandactionview () and collapseactionview () methods.
Note: although it is optional to fold the operation window, if it contains a searchview object, we recommend that you always fold the window as needed, it is displayed only after the user selects it. You should also be careful when the device provides a dedicated "Search" button. If you press the "Search" button, you should also expand the search window, simply override the onkeyup () callback method of the activity, listen to keycode_search-type key events, and call the expandactionview () method to expand the operation window.
If you need to update your activity based on the visibility of the Operation window, you can define an onactionexpandlistener event and use the setonactionexpandlistener () method to register this event, then you can accept this callback method when the operation window is expanded and collapsed, for example:
@ Override
Public Boolean oncreateoptionsmenu (menu ){
Getmenuinflater (). Inflate (R. Menu. Options, menu );
Menuitem = menu. finditem (R. Id. actionitem );
...
Menuitem. setonactionexpandlistener (New onactionexpandlistener (){
@ Override
Public Boolean onmenuitemactioncollapse (menuitem item ){
// Do something when collapsed
Return true; // return true to collapse action View
}
@ Override
Public Boolean onmenuitemactionexpand (menuitem item ){
// Do something when expanded
Return true; // return true to expand action View
}
});
}
Add an operation provider
Similar to the operation window, the Operation provider (defined by the actionprovider class) replaces an operation project with a custom layout. It also needs to control the behavior of all these projects. When you declare an operation item for a menu item in the Operation bar, it requires a custom layout to control the appearance of the menu item, in addition, when it is displayed in the overflow menu, it also needs to handle its default events. It provides a sub-menu either in the Operation bar or in the overflow menu.
For example, the extended class of actionprovider, which is convenient for sharing operations, displays a valid list of sharing targets in the Operation column. Unlike the operation items that use the traditional call action_send intent object, you can declare an actionprovider object to process an operation item. The action provider retains a drop-down list of applications that process intent objects of the action_send type, even if this menu item is displayed in the overflow menu. Therefore, when you use an operator provider like this, you do not have to process user events related to this menu item.
To declare an operation providerer for an operation item, define the Android: actionproviderclass attribute in the <item> element of the menu resource, and use the complete class name. For example:
<? XML version = "1.0" encoding = "UTF-8"?>
<Menu xmlns: Android = "http://schemas.android.com/apk/res/android">
<Item Android: Id = "@ + ID/menu_share"
Android: Title = "@ string/share"
Android: showasaction = "ifroom"
Android: actionproviderclass = "android. widget. Define actionprovider"/>
...
</Menu>
In this example, the actionprovider class is used as the operation provider. Here, the Operation provider needs to control the menu items, and process the appearance and behavior in the Operation bar and the behavior in the overflow menu. You must still provide a text title for this menu item to overflow the menu.
Although the action provider provides the default actions that can be performed when the overflow menu is displayed, the activity (or fragment) can also process the operations from onoptionsitemselected () click events of the callback method to override this default operation. If you do not process click events in this callback method, the Operation provider receives the onperformdefaultaction () callback to process events. However, if the operator provider provides a sub-menu, the activity will not receive the onoptionsitemselected () callback, because the display of the sub-menu replaces the default menu behavior called during selection.
Use the javasactionprovider class
If you want to provide a "share" Operation in the Operation bar to take full advantage of other applications installed on the device (such, share an image to a message or social application), then using the intent actionprovider class is a valid method, rather than adding an operation item that calls the action_send intent object. When you use the intent actionprovider class for an operation item, it will display a drop-down list of applications that can process the action_send intent object (8 ).
Figure 8. The Gallery application screenshot shows the sharing target with the intent actionprovider object.
Create all the logics of the sub-menu, including the encapsulation of the shared target and the processing of click events (the project display of the package in the overflow menu, all are implemented in the intent actionprovider class. the only code you need to write is to provide a tool for the corresponding menu item declaration operation and specify the shared intent object.
By default, the intent actionprovider object retains the order of Shared Objects Based on the frequency of use. The frequently used target application is displayed on the list, and the most common target is displayed in the Operation column as the default sharing target. By default, sorting information is stored in a private file named by default_cmd_history_file_name. If you only use one operation type, the history actionprovider class or one of its subclasses, you should continue to use this default history file without having to do anything. However, if you use the javasactionprovider class for multiple operations of different types or a subclass of it, in order to keep their own history, each javasactionprovider class should specify its own historical files. To specify different historical files for each javasactionprovider class, call the setjavashistoryfilename () method and provide the name of an XML file (for example, custom_share_history.xml)
Note: although the intent actionprovider class arranges shared targets based on the frequency of use, this behavior can be expanded, and the extension of the intent actionprovider class can execute different behaviors and sorting based on historical files.
To add the intent actionprovider object, you just need to set the Android. widget. Intent actionprovider attribute value for the Android. actionproviderclass attribute. The only thing to do is to define the intent object you want to use for sharing. You must first call the getactionprovider () method to obtain the intent actionprovider object that matches the menu item, and then call the setintent intent () method.
If the format of the shared intent object is dependent on the selected menu item, or other variables changed during the activity lifecycle, you should save the intent actionprovider object in a member attribute and call the setintent intent () method to update it as needed. For example:
Private sector actionprovider mpipeline actionprovider;
...
@ Override
Public Boolean oncreateoptionsmenu (menu ){
M1_actionprovider = (your actionprovider) menu. finditem (R. Id. menu_share). getactionprovider ();
// If you use more than one provided actionprovider, each for a different action,
// Use the following line to specify a unique history file for each one.
// M1_actionprovider. set1_historyfilename ("custom_1__history.xml ");
// Set the default share intent
Mshareactionprovider. setshareintent (getdefashareshareintent ());
Return true;
}
// When you need to update the share intent somewhere else in the app, call
// M1_actionprovider. set1_intent ()
In the preceding example, the intent actionprovider object processes all user interactions related to this menu item, and does not need to process click events from the onoptionsitemselected () callback method.