In the previous section we simply introduced the simple utility of Actionbar in Android design style, such as adding MenuItem, which we will learn more about Actionbar's other features.
Documents in Android Develop (http://developer.android.com/training/implementing-navigation/ index.html) describes in detail the practicality of navigation (we do not translate this into Chinese for better understanding):
Create a view that can be slid by tabs
It is common for us to create a view that can slide left and right through the Viewpager in the V4 package, Viewpager can fill viewpager with Fragmentstatepageradapte. Every item of it is a fragment, so it seems very flexible.
So let's do an example with a simple viewpager.
Set up a layout with Viewpager first:
<?xml version= "1.0" encoding= "Utf-8"? ><android.support.v4.view.viewpager xmlns:android= "/http Schemas.android.com/apk/res/android " android:id=" @+id/pager " android:layout_width=" Match_parent " android:layout_height= "Match_parent"/>
Create a new Fragmentstatepageradapter object to set the Viewpager content:
public class Collectiondemoactivity extends Fragmentactivity {//If requested, this adapter returns a Demoobjectfrag ment,//representing an object in the collection. Democollectionpageradapter Mdemocollectionpageradapter; Viewpager Mviewpager; public void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_collection_demo); Viewpager and its adapters use support library//fragments, so use Getsupportfragmentmanager. Mdemocollectionpageradapter = new Democollectionpageradapter (Getsupportfragmentmana GER ()); Mviewpager = (Viewpager) Findviewbyid (R.id.pager); Mviewpager.setadapter (Mdemocollectionpageradapter); }}//Since This was an object collection, with a fragmentstatepageradapter,//and not a Fragmentpageradapter.public class De Mocollectionpageradapter extends Fragmentstatepageradapter {public democollectionpageradaptER (fragmentmanager FM) {super (FM); } @Override public Fragment getItem (int i) {Fragment Fragment = new Demoobjectfragment (); Bundle args = new bundle (); Our object was just an integer:-P args.putint (demoobjectfragment.arg_object, i + 1); Fragment.setarguments (args); return fragment; } @Override public int getcount () {return 100; } @Override public charsequence getpagetitle (int position) {return "OBJECT" + (position + 1); }}//Instances of this class is fragments representing a single//object in our Collection.public static class Demoobject Fragment extends Fragment {public static final String arg_object = "OBJECT"; @Override public View Oncreateview (layoutinflater inflater, ViewGroup container, Bundle savedinstancestate) {//The last of the arguments ensure layoutparams is inflated//properly. View Rootview = Inflater.inflate (R.Layout.fragment_collection_object, container, false); Bundle args = Getarguments (); ((TextView) Rootview.findviewbyid (Android. R.ID.TEXT1). SetText (Integer.tostring (Args.getint (Arg_object))); return rootview; }}
So viewpager in each of the items can be set through the Fragmentstateviewpageradapter, is not feel life and back to the good, fragment is a flexible, reduced version of the activity, We can be very free and easy to develop happily.
To this section is not talking about Actionbar and this viewpager has a relationship, do not know if you use the client is not found on the homepage is also used Actionbar, where chat, discovery, Address book These three items is a tabs group, then tab is what we will focus on next.
Add tabs to Actionbar
In order to tell the activity that we used tabs, we need to set the Navigation_mode_tabs property for Actionbar in activity:
Actionbar.setnavigationmode (Actionbar.navigation_mode_tabs);
We can use Viewpager sliding events to make the top tabs change depending on the viewpager, so we can also click tabs to see if this is Viewpager.
For tabs can be tablistener to listen to tabs click location Changes:
@Overridepublic void OnCreate (Bundle savedinstancestate) {final ActionBar ActionBar = Getactionbar (); ...//Specify that tabs should is displayed in the Action Bar. Actionbar.setnavigationmode (Actionbar.navigation_mode_tabs); Create a tab listener that's called when the user changes tabs. Actionbar.tablistener Tablistener = new Actionbar.tablistener () {public void ontabselected (Actionbar.tab Tab, Frag Menttransaction ft) {//Show the given tab} public void ontabunselected (Actionbar.tab tab, Frag Menttransaction ft) {//Hide the given tab} public void ontabreselected (Actionbar.tab tab, Frag Menttransaction ft) {//probably ignore this event}}; ADD 3 tabs, specifying the tab ' s text and tablistener for (int i = 0; i < 3; i++) {Actionbar.addtab ( Actionbar.newtab (). SetText ("tab" + (i + 1)). SettablisteneR (Tablistener)); }}
The position of Viewpager is also changed when we can make it through the tabs position:
public void ontabselected (Actionbar.tab tab, fragmenttransaction ft) { //When the Tab was selected, switch to the Corresponding page in the Viewpager. Mviewpager.setcurrentitem (Tab.getposition ()); }
Viewpager can slide, and when sliding to which item of Viewpager, tabs's selection also changes:
@Overridepublic void OnCreate (Bundle savedinstancestate) { ... Mviewpager = (Viewpager) Findviewbyid (R.id.pager); Mviewpager.setonpagechangelistener ( new Viewpager.simpleonpagechangelistener () { @Override public void onpageselected (int position) { //when swiping between pages, select the //corresponding tab. Getactionbar (). Setselectednavigationitem (position); } ); ...}
This tabs may not reach the left and right of the sliding process, tabs the selected changes, we can also use the new V4 package in the tabs replacement that isPagerTitleStrip,这个以前也是个开源项目,Android后来也把这个放到了V4支持包中。
这个很像Tabs,不过在ViewPager在滑动过程中,Tabs的Tile下面的选中项是有滑动过程,而不是Tabs那样只有一个结果
<android.support.v4.view.viewpager xmlns:android= "Http://schemas.android.com/apk/res/android" Android:id= "@+id/pager" android:layout_width= "match_parent" android:layout_height= "Match_parent" > <android.support.v4.view.pagertitlestrip android:id= "@+id/pager_title_strip" android:layout_ Width= "Match_parent" android:layout_height= "wrap_content" android:layout_gravity= "Top" android: Background= "#33b5e5" android:textcolor= "#fff" android:paddingtop= "4DP" android:paddingbottom= " 4DP "/></android.support.v4.view.viewpager>
。