There are always a lot of articles that can be done on viewpager this sliding control. Last article, we implemented a custom Viewpager indicator, this article, we mainly want to take advantage of the android comes with the control, implement an indicator, this control, is the SUPPORT-V4 package inside the Pagertabstrip control.
First, let's take a look at the implementation effect.
As you can see, the effect is also great, smoother than the previously customized label indicator. Below, a brief introduction to Pagertabstrip and its use.
Pagertabstrip is the class within the V4 support package, which is a Viewpager class and cannot be used elsewhere. When we use it, we just need to declare it in the Viewpager layout.
As the following code
<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android " android:layout_width=" fill_parent " android:layout_height=" fill_parent " android:background = "@android: Color/white" android:orientation= "vertical" > <android.support.v4.view.viewpager Android:id= "@+id/viewpager" android:layout_width= "match_parent" android:layout_height= "Match_parent" > <android.support.v4.view.pagertabstrip android:layout_width= "match_parent" android:layout _height= "Wrap_content" android:layout_gravity= "top" android:background= "@android: Color/holo_blue_ Light " android:textcolor=" @android: Color/white "/> </android.support.v4.view.viewpager></ Linearlayout>
We can set layout_gravity properties, such as top or bottom, to control where the display is. In addition, no additional settings are required.
The rest is what we need to implement in the code.
Import Java.util.arraylist;import java.util.list;import android.os.bundle;import android.support.v4.app.Fragment; Import Android.support.v4.app.fragmentactivity;import Android.support.v4.app.fragmentpageradapter;import Android.support.v4.view.viewpager;public class Pagertabstripactivity extends Fragmentactivity {private List< fragment> fragments;private viewpager viewpager;private list<string> titlelist; @Overridepublic void onCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.main); Initview ();} private void Initview () {Viewpager = (Viewpager) Findviewbyid (r.id.viewpager); titlelist = new arraylist<string> () ; Titlelist.add ("newest recommendation"); Titlelist.add ("Game Entertainment"); Titlelist.add ("AV video"); fragments = new arraylist<fragment> (); Fragments.add (New Myfragment (Android). R.color.holo_green_light)); Fragments.add (new Myfragment (Android). R.color.holo_orange_light)); Fragments.add (new Myfragment (Android). R.color.holo_red_light)); Fragmentpageradapter AdapteR = new Fragmentpageradapter (Getsupportfragmentmanager ()) {@Overridepublic int getcount () {return 3;} @Overridepublic Fragment getItem (int position) {return fragments.get (position);} @Overridepublic charsequence getpagetitle (int position) {return titlelist.get (position);}}; Viewpager.setadapter (adapter);}}
In the above code, showing how we control the text of the title, we just need to rewrite the Getpagertitle method, the rest, to the great Android system to do it!
"Android Interface Implementation" uses Pagertabstrip to achieve beautiful sliding labels