Android official navigation bar Actionbar (ii) detailed usage of--action View, Action Provider, Navigation tabs

Source: Internet
Author: User
Tags gettext

  in the previous article ( Android's official navigation bar Actionbar) , we introduce the basic applications of the Actionbar components. In addition to the action buttons, Actionbar offers a variety of navigation methods such as Action View, action Provider, Navigation Tabs, Drop-down Navigation, etc. We will describe their usage in detail below.

First, Action View

First of all, action View,action view is a visual component that replaces the action button and appears above the action Bar. In this searchview example, first:

First on the Menu.xml file, as follows

<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>

Showasaction properties in the previous article has been said, here is to note that the Collapseactionview property value, this property will be the action view collapsed into a button, the user will not expand when clicked.

The Actionviewclass property is used to specify the class of the action view, where the Searchview is provided by the system, and we can customize it.

When we need to listen to Actionview related events, we need to get the Searchview object in Method Oncreateoptionsmenu (), as follows:

1, 3.0 before:

MenuItem Action_view = Menu.finditem (R.id.action_search_actionview);

Searchview Searchview = (searchview) menuitemcompat.getactionview (Action_view);

After 2, 3.0:

Menu.finditem (R.id.action_search_actionview). Getactionview ();

After getting the Searchview object, We can listen to Searchview related events, such as Searchview open Close, when the search text in Searchview changes or submit search information, as follows:

        Searchview.setonquerytextlistener (New Searchview.onquerytextlistener () {@Override public bo                Olean Onquerytextsubmit (String s) {Toast.maketext (mainactivity.this, "Submit 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, "Actionvie W Spread out!                ", Toast.length_short). Show ();            return true; } @Override public boolean onmenuitemactioncollapse (MenuItem item) {Toast.maketext (M Ainactivity.this, "Actionview's off! ", Toast.lenGth_short). Show ();            return true; }        });
Second, Action provider

Action provider similar to Actionview can replace the action Button in Actionbar, unlike how the action provider provides a submenu. Here we use the system-provided shareactionprovider to explain the usage of action provider, again, 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 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);

So how do we customize the implementation of a actionprovider? Very simply, we only need to inherit Actionprovider, then implement Oncreateactionview, Onperformdefaultaction, and provide the constructor, the following:

 
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.layou        T.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, "dot", Toast.length_short). Show ();        }        });    return view; @Override public boolean onperformdefaultaction () {//Is in action flow, when selected, call Toast.maketext (Mcontext, "point        Bar ", Toast.length_short). Show ();    return true; }}

The effect is as follows:

Third, Drop-down Navigation

Drop-down Navigation provides a pull-down form of navigation, which also looks at the effect first:

The following four steps are required to achieve this type of navigation:

1, the custom adapter, realizes the Spinneradaper interface, or uses the system to provide the arrayadapter and so on.

2. Implement the Actionbar.onnavigationlisterner interface to respond to the user's choice of the contents of the Drop-down list.

3, set the Actionbar navigation mode for Navigation_mode_list

4, set the Actionbar setlistnavigatioincallbacks ()

  The code is specific 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 chosen:" +tmp,toast.length_short). Show ();            return true; }        });
Iv. Navigation Tabs

Navigation tabs provides navigation in the form of a tab page, with the following effects:

Tabs are used in conjunction with the fragment, here are the specific steps:

1,   Implement Actionbar.tablistener interface, this interface is mainly used to monitor tab switching events, there are three main methods need to implement

a)          callback

C)           ontabreselected: The callback when the tab that has been selected is selected.

2,   Create tab and set properties for tab and Tablistener listener

3.   Add tab to Actionbar

Tab Listener code 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.tab 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_short). 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 (); The method of adding Tab is as follows: private void Initactionbartabs (ActionBar ActionBar) {Actionbar.tab tab1 = Actionbar.newtab        ();        Tab1.settext ("Dial-up"). Settablistener (New tablistener<tabfragment> (This, "Bohao", Tabfragment.class, "dialing"));        Actionbar.addtab (TAB1);        Actionbar.tab tab2 = Actionbar.newtab (); Tab2.settext ("Contacts"). Settablistener (New TablisteneR<tabfragment> (This, "LXR", Tabfragment.class, "contacts"));        Actionbar.addtab (TAB2);        Actionbar.tab tab3 = Actionbar.newtab ();        Tab3.settext ("Information"). Settablistener (New tablistener<tabfragment> (This, "XX", Tabfragment.class, "information"));    Actionbar.addtab (TAB3); }

  To learn more about the small partners, you can click to view the source code , run the test yourself.

Jerry Education
Source:http://blog.csdn.net/jerehedu/
This article is the copyright of Yantai Jerry Education Technology Co., Ltd. and CSDN Common, welcome reprint, but without the author's consent must retain this paragraph statement, and in the article page obvious location to the original link, otherwise reserves the right to pursue legal responsibility.

Android official navigation bar Actionbar (ii) detailed usage of--action View, Action Provider, Navigation tabs

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.