There are always a lot of articles that can be done on a sliding control such as Viewpager. The last article, we implemented a self-defined Viewpager indicator, this article, we mainly want to take advantage of Android's own 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.
Can see that the effect is also very good, more fluent than previously defined label indicators. Here's a brief introduction to Pagertabstrip and its use.
Pagertabstrip is the class within the V4 support package, which is a viewpager-only class. cannot be used anywhere else. When we use it, we just need to declare it in the Viewpager layout.
As in 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 the Layout_gravity property, for example, top or bottom, to control where the display is.
In addition, no other 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, we show how we control the text of the title. We just need to rewrite the Getpagertitle method. Assume that you do not want the following indicators. Change the Pagertabstrip to Pagertitlestrip. Able to put the rest. Just give it to the great Android system!
"Android Interface Implementation" uses Pagertabstrip to implement sliding label Viewpager