首先下载pagerslidingtabstrip第三方包
导入到eclipse中 应用到项目中
<com.astuetz.PagerSlidingTabStrip android:id="@+id/tab" android:layout_width="match_parent" android:layout_height="48dp" android:background="#D43D3D" />
private void init() {
tabs = (PagerSlidingTabStrip) view.findViewById(R.id.tab);
pager = (ViewPager) view.findViewById(R.id.vp);
// 设置字体颜色
tabs.setTextColor(Color.WHITE);
// 添加fragment的方法
addway();
// 设置适配器
TextAdapter adapter = new TextAdapter(getActivity()
.getSupportFragmentManager());
pager.setAdapter(adapter);
// 头部与pager关联
tabs.setViewPager(pager);
}
//添加Fragment private void addway() { for (int i = 0; i < paths.length; i++) { NewsFragment frag = new NewsFragment(paths[i]); fragments.add(frag); } } //設置適配器 class TextAdapter extends FragmentPagerAdapter { String[] titles = { "推荐", "热点", "北京", "社会", "科技", "体育", "财经", "娱乐" };
public TextAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int arg0) {
return fragments.get(arg0);
}
@Override
public int getCount() {
return fragments.size();
}
@Override
public CharSequence getPageTitle(int position) {
return titles[position];
}
}