Article 10 Android actionbar and menu mechanism-MPV Mode

Source: Internet
Author: User

Action bar is one of the important development resources used by Android applications.
Bar provides consistent application navigation and view switching modes, as well as prominent and easy ways to execute important operations in a predictable manner.

Generally, the action bar is located at the top of the screen, which includes four operational areas: the application icon or logo area. It is used for the view control of the spinner drop-down menu or the tab control area. Action
The button (also called Action item) area provides the action of the overflow menu.
Overflow area.

In addition, the action bar also providesContextual
Action bar, and Action view bound to the menu item, and provide
Shared ServiceShare
Action provider.

In terms of functions, the entire system class diagram is divided into three opposite parts: View Control and viewAction provider,The third is the menu display part.

Figure 1 actionbar View Control

Figure 1 shows the category chart of the Control Department in the actionbar view. The action bar consists of several display areas. Each area is displayed by different view types. Each view type corresponds to different view objects in the class 1 graph.

The view where the application icon object is located corresponds to the internal view object mhomelayout of the homeview type of the actionbarview object. The callback object mupclicklistener of the onclicklistener type of the actionbarview object provides the action listening for the operation of the application icon, in this way, the onmenuitemselected callback function of the actual activity is passed through the callback, and corresponding actions are processed in the onmenuitemselected callback function.

The spinner drop-down menu for view control corresponds to the spinner object mspinner in actionbarview (by creatingSpinnerAdapterThe object providesThe mnavitemselectedlistener of the adapterview. onitemselectedlistener type of actionbarview is the event listening object.SpinnerAdapterThe event is triggered when the drop-down of the objectThe onitemselected callback function of the mnavitemselectedlistener object is called, and the onitemselected callback function is called.ActionBar.OnNavigationListener
The onnavigationitemselected function of the type of application object to process the corresponding event.ActionBar.OnNavigationListenerThe object along with the spinneradapter objectThe setlistnavigationcallbacks (spinneradapter adapter, onnavigationlistener callback) function of the actionbarimpl object is passed to the actionbarview object. To enable the drop-down menu function, call the setnavigationmode function of the actionbarimpl object to set navigationmode to actionbar. navigation_mode_list mode.

The view of the action button object corresponds to the internal object mmenuview of the actionmenuview type of the absactionbarview object. Each action button actually corresponds to a menu item, which can be processed in the same way as the menu item.onCreateOptionsMenu()Reads and installs menu Resources in the callback.XMLResource file. By menu ResourcesXMLFileAndroid: The showasaction property defines a menu item as action
ButtonAction buttonByActionMenuPresenterOwner.

The sub-menu of the overflow menu and action button is also inMenu ResourcesXMLFile.XMLResource file DefinitionAction
ButtonOfAndroid: When the showasaction attribute is defined as "ifroom,When the activity bar does not have enough spaceAction buttonShowOverflow menu space. The overflow menu is rendered by the overflowpopup derived from the menupopuphelper object, and the action button sub-menu is rendered by the actionbuttonsubmenu derived from the menupopuphelper object.

Contextual action barActionbarcontextview classThe actionbarcontextview is initialized by the startactionmode function of the actionbarimpl object by calling its initformode function (the actionmodeimpl object is used as the parameter). The menu of the actionbarcontextview is the menu of the actionmodeimpl object, the Menu Generation of the actionmodeimpl object and the event processing of the menu item are all done through the actionmode of the actionmodeimpl object. callback object processing of the callback type. The presentation of the actionbarcontextview menu is also created by the actionbarcontextview object.ActionMenuPresenterOwner.

The callback object of the actionmode. Callback type is passed to the actionmodeimpl object through the parameter of the startactionmode function. The callback object of the actionmode. Callback type is defined by the user for a specific context view. Therefore, to make a specific view have a context action bar, you need to perform the following two tasks:

1. ImplementationActionMode.CallbackCallback interface, inActionMode.CallbackCallback FunctionGenerate the corresponding menu and complete the event processing code of the menu item.

2 callstartActionMode(ActionMode.Callback callback)To displayContext action bar.

Action view objectMexpandedactionview in the actionbarview object,The expandedactionviewmenupresenter object is responsible for rendering, and each action view object is bound to the menu item.Menu ResourcesXMLFileXMLFileandroid:actionLayout
Andandroid:actionViewClassConfirm the property.

The tabimpl object group in actionbarimpl corresponding to the tab control group is displayed in scrollingtabcontainerview. To enable the tab navigation mode, call the setnavigationmode function of the actionbarimpl object to set navigationmode to actionbar. navigation_mode_tabs. Tabimpl object, actionbarimpl object, and scrollingtabcontainerview constitute the MVC pattern relationship. The tabimpl object corresponds to the model,
The actionbarimpl object corresponds to the view of scrollingtabcontainerview for the controller.

Actionbarimpl in the class chart provides the control function for each view.

The action provider element mechanism is similar to action view. Like action view, it needs to be bound to a menu item. It also specifies the bound Action provider object for the menu item in the XML file of the menu resource.XMLFileAndroid: actionproviderclass attribute OK,Only action
Providers are more complex, including more objects, while action view only corresponds to one view object. Users are usingAction providerYou must create
DetailsActionprovider class, and implement the corresponding callback interface of actionprovider as appropriate, especially inDetailsYou must create a view in the oncreateactionview callback function of the actionprovider class,DetailsThe actionprovider class can have an independent XML layout file and read it in the oncreateactionview callback to generate the corresponding view.

Figure 2 actionbar menu display

Figure 2
Yes
Action bar view objectDisplays menus and creates related class diagrams.

Action barMenu rendering and menu building class diagrams include the menu building object (menubuilder) for the menu building process, the menu rendering object (menupresenter) created for the menu container and the menu item view), menu container and menu item view object (
View). Three types of objects constitute the MVP mode.

Menubuilder corresponds to the model of MVP mode, menupresenter corresponds to the presenter of MVP mode, and view object corresponds to view.
Menubuilder builds each menu item of a menu.
MenupresenterRead menu items from the menubuilder object and generate corresponding menu item subviews. The created menu item subview is added to the menu container view, menupresenter can also obtain menubuilder objects and their menu items and perform other operations. The view obtains the view corresponding to the operation menu and menu item through menupresenter. In MVP mode, the view and mode do not directly interact.

The actionbar system includes fourThe specific menupresenter class, actionmenupresenter is responsibleAction button andContextual action
Display the menu view corresponding to the bar. expandedactionviewmenupresenter is responsible for displaying the view of the action view object bound to the menu item, and overflowpopup is responsible
View rendering corresponding to the overflow menu, and actionbuttonsubmenu is responsible for view rendering corresponding to the sub menu.Overflowpopup andActionbuttonsubmenu is derived from menupopuphelper.Overflowpopup:Menubuilder object, andActionbuttonsubmenuDisplayed menuCorresponds to the submenubuilder object, so the menupopuphelperThe menubuilder object is an adapter object,The adapter mode is used to encapsulate different objects into the same interface.

In Figure 2, The menuitemimpl class is the specific implementation class of the corresponding menu item.Every created menubuilder objectMenuitemimpl typeMenu items are placed in.

The menupopuphelper object is also registered as the anchor view listener. The viewtreeobserver object is used to detect the changes in the anchor view where the menu is located. The anchor view of the overflow menu is overflowmenubutton.


Figure 3Provided by Action provider

Figure 3
Is the internal actionprovider class and related objects provided by the system, providing quick access
The action of the shared service.

The class diagram mainly includes intent actionprovider, activitychoosermodel, activitychooserview, activitychooserviewadapter and other objects.

The specific class that is derived from actionprovider; actionprovider, Used for instantiationActivitychoosermodel and activitychooserview objects, and set the data mode for the activitychooserview object to generate a view,Operate and obtain model information. Create an activity menu based on model information.

Activitychoosermodel, activitychooserview, and metadata constitute the MVC mode, which corresponds to model, view, and control in adapter mode. activitychooserview obtains activity information in activitychoosermodel through callback, and activitychoosermodel is derived from datasetobservable, you can register a datasetobserver type object for the activitychoosermodel object in the activitychooserview object. The activitychoosermodel object sends a Data Change Notification to the activitychooserview object through this object.

Activitychoosermodel adopts the policy mode for sorting intent-compliant activity records (stored in the array list as activityresolveinfo) obtained from the Package Manager using intent, the sorting method is encapsulated as an object. The default sorting method is used when the setactivitysorter method is not used to set the sorting method, which is encapsulated by the defaultsorter object. The ultsorter object is sorted based on the weight value in the activity record, the Weight Value of the sorted activity record is also modified based on the activity sequence in the historicalrecord (the activity in historicalrecord is obtained by reading a private XML-type shared history file) list, the weight value is added to the corresponding objects in the mactivites array according to the order of the historicalrecord list from the back to the back. The smaller the weight value of the corresponding activity in the mactivites list, the higher the weight of the latest corresponding record.

The historypersister thread object is used to save records in the historicalrecord list to historical files. The historyloader thread object is used to read historical files to the historicalrecord list. The datamodelpackagemonitor object is used to monitor data packets and synchronize the mactivites list of activity records when data packets are updated. The activitychoosermode object also uses Map <string,
The activitychoosermodel> type hashmap ensures that only one activitychoosermodel object can be instantiated for the same history file. It is a specific application of the singleton mode.

You are welcome to repost the document. Please respect the source of the original article.

 

 

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.