ActionBar (2) in the official Android Navigation bar -- detailed usage of Action View, Action Provider, and Navigation Tabs; androidactionbar

Source: Internet
Author: User

ActionBar (2) in the official Android Navigation bar -- detailed usage of Action View, Action Provider, and Navigation Tabs; androidactionbar

In the previous article (ActionBar in the official navigation bar of Android), we introduced the basic application of each component of ActionBar. In addition to Action Buttons, ActionBar also provides multiple Navigation methods, such as Action View, Action Provider, Navigation Tabs, and Drop-down Navigation. The following describes their usage.

I. Action View

First, let's look at the Action View. action view is a visual component used to replace the action button and display it on the action bar. Take this SearchView as an example:

 

First go to the menu. xml file, as shown below

<Menu xmlns: android = "http://schemas.android.com/apk/res/android" xmlns: app = "http://schemas.android.com/apk/res-auto" xmlns: tools = "http://schemas.android.com/tools" tools: context = ". mainActivity "> <item android: id =" @ + id/action_search_actionview "android: title =" Search "android: icon =" @ drawable/biz_pc_list_search_icon_dark "app: showAsAction = "ifRoom | collapseActionView" app: actionViewClass = "android. support. v7.widget. searchView "/> </menu>

The showAsAction attribute has been mentioned in the previous article. Here, you must note the attribute value collapseActionView. This attribute will fold the action view to a button and be displayed only when you click it.

The ActionViewClass attribute is used to specify the action view class. The SearchView here is provided by the system and can be customized.

When we need to listen to ActionView related events, we need to get the SearchView object in method onCreateOptionsMenu (), as shown below:

1. Before 3.0:

MenuItem action_view = menu. findItem (R. id. action_search_actionview );

SearchView searchView = (SearchView) MenuItemCompat. getActionView (action_view );

2. After 3.0:

Menu. findItem (R. id. action_search_actionview). getActionView ();

After obtaining the SearchView object, we can listen to events related to the SearchView, such as opening or closing the SearchView. When the search text in the SearchView changes or the search information is submitted, the details are as follows:

SearchView. setOnQueryTextListener (new SearchView. onQueryTextListener () {@ Override public boolean onQueryTextSubmit (String s) {Toast. makeText (MainActivity. this, "submitted text:" + s, Toast. LENGTH_SHORT ). show (); return false ;}@ Override public boolean onQueryTextChange (String s) {Toast. makeText (MainActivity. this, "current text:" + s, Toast. LENGTH_SHORT ). show (); return false ;}}); MenuItemCompat. setOnActionExpandListener (Action_view, new MenuItemCompat. OnActionExpandListener () {@ Override public boolean onMenuItemActionExpand (MenuItem item) {Toast. makeText (MainActivity. this, "actionView expanded! ", Toast. LENGTH_SHORT). show (); return true ;}@ Override public boolean onMenuItemActionCollapse (MenuItem item) {Toast. makeText (MainActivity. this," actionView disabled! ", Toast. LENGTH_SHORT). show (); return true ;}});
2. Action provider

The Action provider can replace the Action Button in the ActionBar with the ActionView. The difference is that the Action Provider provides a sub-menu. The following describes the usage of the Action Provider using the System-provided reply actionprovider. Similarly, first:

 

  The code for Menu. xml is as follows:

<Item android: id = "@ + id/action_provider_share" android: title = "share" android: icon = "@ drawable/ic_launcher" app: showAsAction = "ifRoom" app: actionProviderClass = "android. support. v7.widget. shareActionProvider "/>

The main attribute here is actionProviderClass, which is used to specify the ActionProvider.

  The Java code is as follows:

  MenuItem shareItem = menu.findItem(R.id.action_provider_share);        ShareActionProvider mShareActionProvider =(ShareActionProvider)MenuItemCompat.getActionProvider(shareItem);        Intent shareIntent = new Intent();        shareIntent.setType("image/*");        mShareActionProvider.setShareIntent(shareIntent);

How can we customize an ActionProvider? It is very simple. We only need to inherit ActionProvider, and then implement onCreateActionView and ondomainmdefaultaction, and provide constructors. The specific content is as follows:

Public class MyActionProvider extends ActionProvider {private Context mContext;/*** Creates a new instance. ** @ param context Context for accessing resources. */public MyActionProvider (Context context) {super (context); this. mContext = context ;}@ Override public View onCreateActionView () {View view = LayoutInflater. from (this. mContext ). inflate (R. layout. action_provider_layout, null, false); Button btnOk = (Button) view. findViewById (R. id. btnOk); btnOk. setOnClickListener (new View. onClickListener () {@ Override public void onClick (View v) {Toast. makeText (mContext, "click it", Toast. LENGTH_SHORT ). show () ;}}); return view ;}@ Override public boolean on=mdefaultaction () {// call Toast when selected when it is in action flow. makeText (mContext, "click it", Toast. LENGTH_SHORT ). show (); return true ;}}

The effect is as follows:

Iii. Drop-Down Navigation

Drop-Down Navigation provides a Drop-Down Navigation method. You can also view the result first:

 

To implement this type of navigation, you only need to perform the following four steps:

1. Customize the adapter to implement the SpinnerAdaper interface, or use the ArrayAdapter provided by the system.

2. Implement the ActionBar. OnNavigationListerner interface to respond to users' selection of drop-down list content.

3. Set the Navigation Mode of the ActionBar to NAVIGATION_MODE_LIST.

4. Set setListNavigatioinCallbacks () of the ActionBar ()

  The Code is as follows:

Final String [] data = new String [] {"Java", "Android", "Oracle"}; ArrayAdapter <String> adapter = new ArrayAdapter <String> (this, android. r. layout. simple_dropdown_item_1line, data); actionBar. setNavigationMode (ActionBar. NAVIGATION_MODE_LIST); actionBar. setListNavigationCallbacks (adapter, new ActionBar. onNavigationListener () {@ Override public boolean onNavigationItemSelected (int I, long l) {String tmp = data [I]; Toast. makeText (MainActivity. this, "you have selected:" + tmp, Toast. LENGTH_SHORT ). show (); return true ;}});
Iv. Navigation Tabs

Navigation Tabs provides Tab-based Navigation with the following effects:

 

Tabs is mainly used in combination with Fragment. The procedure is as follows:

1. Implement the ActionBar. TabListener interface. This interface is mainly used to listen to Tab switching events. There are three methods to implement

A) onTabSelected: callback when the tab is selected

B) onTabUnselected: the callback from the selected to the unselected tab.

C) onTabReselected: Specifies the callback when the Selected tab is selected.

2. Create a Tab and set attributes and TabListener listener for the Tab.

3. Add the Tab to the ActionBar.

  The code of the Tab listener is as follows:

Private static class TabListener <T extends Fragment> implements ActionBar. tabListener {private Fragment mFragment; private Activity mActivity; private String mTag; private Class <T> mClass; private String mTitle; public TabListener (Activity activity, String tag, Class <T> clz, string title) {mActivity = activity; mTag = tag; mClass = clz; mTitle = title ;}@ Override public void onTabSelected (ActionBar. ta B tab, FragmentTransaction fragmentTransaction) {if (mFragment = null) {mFragment = Fragment. instantiate (mActivity, mClass. getName (); Bundle bundle = new Bundle (); bundle. putString ("name", mTitle); mFragment. setArguments (bundle); fragmentTransaction. add (R. id. content, mFragment, mTag);} else {fragmentTransaction. attach (mFragment);} Toast. makeText (mActivity, tab. getText () + "onTabSelected", Toast. LENGTH_SHO RT). show () ;}@ Override public void onTabUnselected (ActionBar. Tab tab, FragmentTransaction fragmentTransaction) {if (mFragment! = Null) {fragmentTransaction. detach (mFragment);} Toast. makeText (mActivity, tab. getText () + "onTabUnselected", Toast. LENGTH_SHORT ). show () ;}@ Override public void onTabReselected (ActionBar. tab tab, FragmentTransaction fragmentTransaction) {Toast. makeText (mActivity, tab. getText () + "onTabReselected", Toast. LENGTH_SHORT ). show () ;}} Add a Tab as follows: private void initActionBarTabs (ActionBar actionBar) {ActionBar. tab tab1 = actionBar. newTab (); tab1.setText ("dial "). setTabListener (new TabListener <TabFragment> (this, "bohao", TabFragment. class, "dialing"); actionBar. addTab (tab1); ActionBar. tab tab2 = actionBar. newTab (); tab2.setText ("Contact "). setTabListener (new TabListener <TabFragment> (this, "lxr", TabFragment. class, "Contact"); actionBar. addTab (tab2); ActionBar. tab tab3 = actionBar. newTab (); tab3.setText ("info "). setTabListener (new TabListener <TabFragment> (this, "xx", TabFragment. class, "info"); actionBar. addTab (tab3 );}

 

For more information, clickView Source CodeRun the test in person.

 

Author: Jerry Education
Source: http://www.cnblogs.com/jerehedu/
The copyright of this article belongs to Yantai Jerry Education Technology Co., Ltd. and the blog Park. You are welcome to repost it. However, you must keep this statement without the author's consent and provide the original article connection on the article page, otherwise, you are entitled to pursue legal liability.

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.