Reprint Please specify Source: http://blog.csdn.net/zhaokaiqiang1992
There are three previous articles that show how to add an indicator to Viewpager, and this is the last article to introduce an indicator of viewpager.
This article describes the use of open source project Pagerslidingtabstrip to achieve this effect. This open source project is the same author as the previous scrpllingtabs.
Let's look at the effect of the implementation first.
The difference between this implementation and the previous one is that the color label at the bottom will move in sync with the Viewpager, which is more vivid than the previous implementation.
In addition, code writing is basically the same as scrollingtabs.
The first is the layout file.
<relativelayout 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 " android:layout_width= "Match_parent" android:layout_height= "match_parent" > <com.astuetz.pagerslidingtabstrip Android:id= "@+id/tabs" android:layout_width= "match_parent" android:layout_height= "48dip" android: background= "@drawable/background_tabs"/> <android.support.v4.view.viewpager android:id= "@+id/ Pager " android:layout_width=" match_parent " android:layout_height=" wrap_content " android:layout_ Above= "@+id/colors" android:layout_below= "@+id/tabs" tools:context= ". Mainactivity "/></relativelayout>
in the activity, the adapter will be set.
@Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview ( R.layout.activity_main) tabs = (Pagerslidingtabstrip) Findviewbyid (r.id.tabs);p ager = (Viewpager) findViewById ( R.id.pager) adapter = new Mypageradapter (Getsupportfragmentmanager ());p Ager.setadapter (adapter); Tabs.setviewpager (pager);}
Adapter for Viewpager
public class Mypageradapter extends Fragmentpageradapter {private final string[] TITLES = {"Categories", "Home", "Top Pai D "," Top Free "," Top Grossing "," Top New Paid "," Top New free "," trending "};p ublic Mypageradapter (fragmentmanager FM) {SUP ER (FM);} @Overridepublic charsequence getpagetitle (int position) {return titles[position];} @Overridepublic int GetCount () {return titles.length;} @Overridepublic Fragment getItem (int position) {return superawesomecardfragment.newinstance (position);}}
GitHub Project Address: Https://github.com/astuetz/PagerSlidingTabStrip
"Android Interface Implementation" uses Pagerslidingtabstrip to realize the Viewpager effect of sliding label synchronization