Action View and action provider (Action views and action Providers)

Source: Internet
Author: User

First admit: This article translation is a bit inaccurate, because this action, I am not sure how to translate, do not know whether to translate into verbs or nouns. So I listed the translation results in the Youdao dictionary below.

Action N. Action, activity, function, battle, plot

Start translating

The V7 AppCompat Support library provides several ways for your app to interact with users. The first few lessons explain how to define an action (not how to understand it) can be a button or a menu item. This lesson explains how to add two other common components.

    • The action view on the app bar has many features. For example, a search action view allows the user to enter search text on the app bar without changing the activity or fragment interface (meaning that there is no search action on a new interface).

    • An action provider has a custom layout. This action appears as a button or menu item at the beginning, and when the user clicks on the action, the action provider controls any action you want to define. For example, when you click, the action provider pops up a menu.

The Android Support library provides several specific action view and action provider components. For example, Searchview implements the action view of the input to query, and Shareactionprovider implements an action provider that shares information with other applications. You can also define your own action view and action provider.

Add a action view (add an action)

Create a new element in the toolbar menu resource file to add an action view.

    • Actionviewclass: A class that implements the component of this action

    • Actionlayout: A layout file that describes the contents of this action
      The Showasaction property can have a value of "Ifroom|collapseactionview" or "Never|collapseactionview". (Collapse meaning collapsible) Collapseactionview Represents how the action component displays when the user does not interact with the current action. If the component is on the app bar, this component will appear as an icon. If this component is in the overflow button, this component will be displayed as a menu item. When the user interacts with the build, the component fills the entire app bar.

For example, the following code adds a Searchview to the app bar

  <item        android:id="@+id/action_search"        android:title="搜索"        android:icon="@drawable/ic_search_white_48dp"        app:showAsAction="always|collapseActionView"        app:actionViewClass="android.support.v7.widget.SearchView"/>

If the user does not interact with the component, the app will show the component as an icon. (the icon defined by Android:icon) (if there is not enough space on the app bar, attach the component to the overflow menu.) When the user clicks on the icon or menu item, the component fills the entire toolbar, allowing the user to interact with it.

Figure 1: When the user clicks on an icon for an action view, the view fills the entire toolbar.

If you need to set this action view, set it in the Oncreateoptionsmenu () callback method of your activity. You can call Getactionview () This static method to get a reference to this action view. For example, the following code obtains a reference to the Searchview defined in the example code above.

@OverridepublicbooleanonCreateOptionsMenu(Menu menu) {    getMenuInflater().inflate(R.menu.main_activity_actions, menu);    MenuItem searchItem = menu.findItem(R.id.action_search);    SearchView searchView =            (SearchView) MenuItemCompat.getActionView(searchItem);    // 设置搜索内容并添加事件监听等等。。。    returnsuper.onCreateOptionsMenu(menu);}
Interaction when the action view is expanded (responding to Action view expansion)

If an action element has a Collapseactionview flag. Then when the user does not interact with the action view, the app will show the action view as an icon. When the user clicks on the icon, the onoptionsitemselected () 's built-in handler will expand the action view. If you integrate the activity and rewrite the onoptionsitemselected () method, your replication method must (must) call super.onoptionsitemselected (), otherwise the action view cannot be expanded.

If you want to do something when the action view is expanded or folded up, you can define a class to implement Menuitem.onactionexpandlistener and pass it as a parameter to Setonactionexpandlistener (). For example, when the action view expands or collapses, you want to update the activity. The following fragment shows how to define and pass a listener.

@Override Public Boolean Oncreateoptionsmenu(Menu menu) {Getmenuinflater (). Inflate (r.menu.options, menu);// ...    //define a listenerOnactionexpandlistener Expandlistener =NewOnactionexpandlistener () {@Override         Public Boolean Onmenuitemactioncollapse(MenuItem Item) {//Handling action when item is folded up            return true;//Returns True to collapse this action view}@Override         Public Boolean Onmenuitemactionexpand(MenuItem Item) {//Handling action when item is expanded            return true;//Returns TRUE to expand this action view}    };//Get the MenuItem for the action itemMenuItem Actionmenuitem = Menu.finditem (R.id.myactionitem);//Add a listener to this action viewMenuitemcompat.setonactionexpandlistener (Actionmenuitem, Expandlistener);//Options menu created when you want to do other things    return true;
Add an action provider (add an action Provider)

Create a new element in the toolbar menu resource file to declare an action provider. Add a Actionproviderclass property and assign a value to the full pathname of the content provider class.

For example, the following code declares a shareactionprovider. This is a component in the support library that is used to share data with other apps.

<item android:id="@+id/action_share"    android:title="@string/share"    app:showAsAction="ifRoom"    app:actionProviderClass="android.support.v7.widget.ShareActionProvider"/>

In this case, it is not necessary to provide an icon for this component because Shareactionprovider has its own icon, but if you want to use a custom icon, declare an icon.

For more information on customizing an action provider, refer to "Actionprovider reference". For more information on setting up Shareactionprovider, please refer to "Shareactionprovider class"

Then go to the practice stage
    1. Add an item item to my Mymenu.xml file
<item        android:id="@+id/action_search"        android:title="搜索"        android:icon="@drawable/ic_search_white_48dp"        app:showAsAction="always|collapseActionView"        app:actionViewClass="android.support.v7.widget.SearchView"/>

2. Then override the method Oncreateoptionsmenu (Menu menu) in mainactivity

@Override    publicbooleanonCreateOptionsMenu(Menu menu) {        //这句话的意思是把mymenu加载到menu中        getMenuInflater().inflate(R.menu.mymenu, menu);        returnsuper.onCreateOptionsMenu(menu);    }

3. Look at the effect of the operation

4. Add a listener event to the search action view that changes the text.

    • First, add a edittext in the mainactivity layout file to display the content entered in the search box. Locate the control in the OnCreate () method.
 @Override     Public Boolean Oncreateoptionsmenu(Menu menu) {//This sentence means to load the MyMenu into the menuGetmenuinflater (). Inflate (R.menu.mymenu, menu);        MenuItem Searchitem=menu.finditem (R.id.action_search); Searchview searchview= (Searchview) Menuitemcompat.getactionview (Searchitem);/** * Add a listening event for search text to make, just do a simple operation, * /Searchview.setonquerytextlistener (NewSearchview.onquerytextlistener () {@Override             Public Boolean Onquerytextsubmit(String query) {return false; }@Override             Public Boolean Onquerytextchange(String NewText) {//In the search box here, enter what EditText to show whatEdittext.settext (NewText);return true; }        });return Super. Oncreateoptionsmenu (menu); }

4. Add an expanded and collapsed listener event to this search action view.

    @Override     Public Boolean Oncreateoptionsmenu(Menu menu) {//This sentence means to load the MyMenu into the menuGetmenuinflater (). Inflate (R.menu.mymenu, menu);        MenuItem Searchitem=menu.finditem (R.id.action_search); Menuitemcompat.onactionexpandlistener expandlistener=NewMenuitemcompat.onactionexpandlistener () {@Override             Public Boolean Onmenuitemactionexpand(MenuItem Item) {Toast toast= toast.maketext (mainactivity. This,The search box expands., Toast.length_short); Toast.setgravity (Gravity.center,0,0); Toast.show ();return true; }@Override             Public Boolean Onmenuitemactioncollapse(MenuItem Item) {Toast Toast=toast.maketext (mainactivity. This,"The search box collapsed.", Toast.length_short); Toast.setgravity (Gravity.center,0,0); Toast.show ();return true;        }        }; Menuitemcompat.setonactionexpandlistener (Searchitem,expandlistener);return Super. Oncreateoptionsmenu (menu); }

Enter the actual combat link to add an action provider

    1. Declare a intent myshareintent in the mainactivity;
    2. In the Oncreateoptionsmenu (Menu menu)
The meaning of this sentence is to load the MyMenu into the menu Getmenuinflater (). Inflate(R. Menu. MyMenu, menu);MenuItem Shareitem = Menu. FindItem(R. ID. Action_share);Shareactionprovider Myshareactionprovider = (shareactionprovider) menuitemcompat. Getactionprovider(Shareitem);Myshareintent = new Intent (Intent. ACTION_send);Myshareintent. SetType("image/*");My phone's memory card root directory under the pictures directory has a picture called share, I want to share it to QQ friend file Imgfile = new file (environment. Getexternalstoragepublicdirectory(Environment. DIRECTORY_pictures) +"/share.jpg");Myshareintent. PutExtra(Intent. EXTRA_stream, Uri. FromFile(Imgfile));Myshareactionprovider. Setshareintent(myshareintent);

3. Operation in onoptionsitemselected (MenuItem item)

switch (item.getItemId()) {            case R.id.action_share:            //分享图片                MainActivity.this"分享图片"));                returntrue;        }        returnsuper.onOptionsItemSelected(item);

The effect is as follows

Almost there, the translation of toolbar should be over. There are doubts and errors where the point is welcomed.

Action View and action provider (Action views and action Providers)

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.