首先导入PagerSlidingTabStripLib包建立关联。
MainActivity的xml文件
<com.astuetz.PagerSlidingTabStrip
android:layout_height="40dp"
android:layout_width="match_parent"
android:id="@+id/pst"
></com.astuetz.PagerSlidingTabStrip>
<android.support.v4.view.ViewPager
android:layout_height="match_parent"
android:layout_width="match_parent"
android:id="@+id/vp"
android:layout_below="@id/pst"
></android.support.v4.view.ViewPager>
MainActivity方法里
public class MainActivity extends FragmentActivity {
private PagerSlidingTabStrip pst;
private List<String> list;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
pst = (PagerSlidingTabStrip) findViewById(R.id.pst);
ViewPager vp = (ViewPager) findViewById(R.id.vp);
setData();
vp.setAdapter(new Mypageradapter(getSupportFragmentManager(), list));
pst.setViewPager(vp);
}
private void setData() {
list = new ArrayList<String>();
list.add("推荐");
list.add("视频");
list.add("社会");
list.add("北京");
list.add("热点");
list.add("订阅");
list.add("图片");
list.add("娱乐");
}
}
Mypageradapter适配器
public class Mypageradapter extends FragmentPagerAdapter {
List<String> list;
public Mypageradapter(FragmentManager fm, List<String> list) {
super(fm);
this.list = list;
}
@Override
public Fragment getItem(int arg0) {
switch (arg0) {
case 0:
return new Fragment1(list.get(arg0));
default:
return new Fragment1(list.get(arg0));
}
}
@Override
public int getCount() {
return list != null ? list.size() : 0;
}
// 添加指针
// 必须填写
@Override
public CharSequence getPageTitle(int position) {
return list.get(position);
}
}