First look at a simple, first effect map, to attract everyone to the eyeball.
Three sliding between pages, this time with the title of the above slide.
Take a look at Android's official explanation for Pagertitlestrip:
Pagertitlestrip is a viewpager indicator of the current page, the previous page, and the next page. It is often added as a child control of the Viewpager control in an XML layout file. In your layout file, add it as a child control in the Viewpager. And you want to set its Android:layout_gravity property to top or bottom to display it at the tops or bottom of the viewpager. The title of each page is provided to Viewpager by the getpagetitle (int) function of the adapter.
Focus on two points:
1, first of all, the article mentions: in your layout file, it is added as a child control in the Viewpager.
2. Second, the title is obtained by overriding the Getpagetitle (int) function of the adapter.
Based on these two points, we can look at the code:
1. xml layout file:
<relativelayout xmlns:android= "http://schemas.android.com/apk/res/android"
xmlns:tools= "http:// Schemas.android.com/tools "
android:layout_width=" match_parent "
android:layout_height=" Match_parent "
tools:context= "com.example.testviewpage_2.MainActivity" >
<android.support.v4.view.viewpager
android:id= "@+id/viewpager"
android:layout_width= "wrap_content"
android:layout_height= "200dip"
android:layout_gravity= "center" >
<android.support.v4.view.pagertitlestrip
android:id= "@+ Id/pagertitle "
android:layout_width=" wrap_content "
android:layout_height=" Wrap_content "
Android : layout_gravity= "Top"
/>
</android.support.v4.view.ViewPager>
</RelativeLayout>
Clearly see that we will. Pagertitlestrip it directly into the Viewpager as a child control; This is the first step; the value of android:layout_gravity= "" is set to top or bottom. Displays the title bar at the top or bottom.
2, rewrite the adapter getpagetitle () function
Easy for everyone to have a holistic understanding, the first to paste the global code, and then speak individually:
Package com.example.testviewpage_2;
Import java.util.ArrayList;
Import java.util.List;
Import android.app.Activity;
Import Android.os.Bundle;
Import Android.support.v4.view.PagerAdapter;
Import Android.support.v4.view.PagerTitleStrip;
Import Android.support.v4.view.ViewPager;
Import Android.view.LayoutInflater;
Import Android.view.View;
Import Android.view.ViewGroup;
public class Mainactivity extends activity {private View view1, View2, VIEW3; Private list<view> viewlist;//View array private Viewpager Viewpager; The corresponding Viewpager private list<string> titlelist;
The title list array @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_main);
Viewpager = (Viewpager) Findviewbyid (R.id.viewpager);
Layoutinflater inflater = Getlayoutinflater ();
View1 = inflater.inflate (R.LAYOUT.LAYOUT1, NULL);
View2 = inflater.inflate (r.layout.layout2, NULL); VIEW3 = inflater.inFlate (R.LAYOUT.LAYOUT3, NULL);
Viewlist = new arraylist<view> ()//The View to be displayed is loaded into the array viewlist.add (VIEW1);
Viewlist.add (VIEW2);
Viewlist.add (VIEW3);
Titlelist = new arraylist<string> ();//title data for each page Titlelist.add ("Wang Peng");
Titlelist.add ("Ginger language");
Titlelist.add ("marriage"); Pageradapter pageradapter = new Pageradapter () {@Override public boolean isviewfromobject (View arg0, Object Arg 1) {//TODO auto-generated method Stub//According to the key, find the view, judge with the argument from the view arg0 is not the same view return arg0 = = Viewlist
. get ((int) integer.parseint (arg1.tostring ()));
@Override public int GetCount () {//TODO auto-generated a stub return viewlist.size (); @Override public void Destroyitem (ViewGroup container, int position, object) {//TODO auto-
Generated method stub Container.removeview (Viewlist.get (position)); @Override public Object Instantiateitem (viewgroup container, int position) {//TODO auto-generated Method Stub Container.addview (Viewlist.get (position));
The current new view position (position) as key pass return position; @Override public charsequence getpagetitle (int position) {//TODO auto-generated method stub ret
Urn Titlelist.get (position);
}
};
Viewpager.setadapter (Pageradapter);
}
}
3, variable
Private list<string> titlelist; Array of title lists
A string array was applied to store the title of the three pages.
4. Initialization
Titlelist = new arraylist<string> ();//title data for each page
titlelist.add ("Wang Peng");
Titlelist.add ("Ginger Language");
Titlelist.add ("marriage");
The code to initialize the array is added during the initialization phase.
5. Rewrite the charsequence getpagetitle (int) function
@Override public
charsequence getpagetitle (int position) {
//TODO auto-generated a stub
return Titlelist.get (position);
}
Returns the current caption based on the location.
You can see, in fact, here only rewrite the Getpagetitle () function, it can be based on different locations to return a different string to achieve the above title bar function. The first and second steps about arrays and initialization are actually this step, and we can actually replace them with the following code:
@Override public
charsequence getpagetitle (int position) {
//TODO auto-generated Method stub
switch ( Position) {Case
0: Return
"Wang Peng";
Case 1: Return to
"Ginger";
Case 2: Return
"marriage";
Default: Return
"";
}
}
The effect is the same, but the code is not maintenance.
Set Title
a custom view that separates page title, which allows you to flexibly set the style and text of the title.
Effect:
XML usage:
<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=" 30DP "
android:layout_gravity=" bottom "
android:paddingtop=" 4DP "
android:paddingbottom=" 4dp/>
</android.support.v4.view.ViewPager>
Android:layout_gravity the position of the control title is generally bottom or top
See Open Source Project Imageloader Use this, looking for a long time did not see where FindByID,
Originally in its source code to think of it as a Viewpager child control.
Partial Source:
@Override
protected void Onattachedtowindow () {
super.onattachedtowindow ();
Final Viewparent parent = GetParent ();
if (!) ( Parent instanceof Viewpager)) {
throw new IllegalStateException (
"Pagertitlestrip must is a direct child of View Pager. ");
}
Final Viewpager pager = (Viewpager) parent;
Final Pageradapter adapter = Pager.getadapter ();
Pager.setinternalpagechangelistener (Mpagelistener);
Pager.setonadapterchangelistener (Mpagelistener);
Mpager = pager;
Updateadapter (mwatchingadapter!= null? Mwatchingadapter.get (): null, adapter);
Analysis: In Onattachedtowindow (), directly find Parent-view, if it is viewpager to use, or throw an exception directly.
To set the title value:
Pageradapter has a getpagetitle () that needs to be rewritten and can then return different title according to the different page.
@Override public
charsequence getpagetitle (int position) {
switch (position) {case
0:
return GetString (r.string.title_list);
Case 1: Return
getString (r.string.title_grid);
Default: Return
null;
}
}
And in the Pagetitlestrip updatetext () source code, called the Viewpager Adapter.getpagetitle, get title and set.