ViewPagerIndicate的使用总结

使用步骤(按照步骤一般都能实现想要的效果):

1)在以Module和添加依赖的方式引用ViewPagerIndicate(没有区别的);

2)让使用ViewpagerIndicate的activity继承自FragmentActivity;

3)书写模板性代码:

//实例化控件 @ViewInject(R.id.vp) private ViewPager vp; @ViewInject(R.id.tabPageIndicator) private com.viewpagerindicator.TabPageIndicator tabPageIndicator; //分类标题、装载Fragment的集合 private String[] names = {"全部", "人造革", "真皮", "超纤"}; private List<fragment> contentFragment = new ArrayList<fragment>(); //创建Fragment GenuineLeatherFragment genuineLeatherFragment = new GenuineLeatherFragment(); contentFragment.add(genuineLeatherFragment); //为viewpager设置适配器,一定要注意是FragmentPagerAdapter vp.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) { //返回对应位置上的Fragment @Override public Fragment getItem(int position) { return contentFragment.get(position); } //获取Fragment的数量 @Override public int getCount() { return contentFragment.size(); } //为对应的Fragment设置标题 @Override public CharSequence getPageTitle(int position) { return names[position]; } }); //为ViewPagerIndicate设置viewpager tabPageIndicator.setViewPager(vp);

4)别忘了给ViewPagerIndicate所在的activity设置theme;

android:theme="@style/StyledIndicators"

5)样式文件StyledIndicators 中可以自定义标题显示的状况

<style name="StyledIndicators" parent="@android:style/Theme.Light"><item name="vpiTabPageIndicatorStyle">@style/CustomTabPageIndicator</item></style> <style name="CustomTabPageIndicator" parent="Widget.TabPageIndicator"><item name="android:background">@drawable/tab_indicator</item> <item name="android:textAppearance">@style/CustomTabPageIndicator.Text</item> <item name="android:textSize">14sp</item> <item name="android:dividerPadding">8dp</item> <item name="android:showDividers">middle</item> <item name="android:paddingLeft">10dp</item> <item name="android:paddingRight">10dp</item> <item name="android:fadingEdge">horizontal</item> <item name="android:fadingEdgeLength">8dp</item></style> <style name="CustomTabPageIndicator.Text" parent="android:TextAppearance.Medium"><item name="android:typeface">monospace</item> <item name="android:textColor">@drawable/selector_tabtext</item></style>



你可能感兴趣的:(viewpager)