Github:https://github.com/astuetz/PagerSlidingTabStrip
使用方法
1、下载Zip压缩文件
从 PagerSlidingTabStrip官网(https://github.com/astuetz/PagerSlidingTabStrip)上下载Zip文件,下载成功后解压缩Zip文件,如下:
2、导入library和sample项目
Import -> Android -> Existing Android Code Into Workspace -> Browse,选择你解压缩的library 目录即可,记住勾选 Copy projects into workset
3、具体用法
导入后的项目结构如下:
我简单翻译一下官网上的内容:
交互式页面指示器控件,完美配合ViewPager控件(来自Android Support Library)
注意:这里的ViewPager的适配器必须是继承的FragmentPagerAdapter,并重写getPageIconResId(int position)或者getPageTitle(int position)方法
PagerSlidingTabStrip需要配合ViewPager一起使用,PagerSlidingTabStrip的作用相当于一个顶部的导航条
MainActivity 布局如下:
<pre name="code" class="html"><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_below="@+id/tabs"
tools:context=".MainActivity" />
</RelativeLayout>
MainActivity的onCreate方法如下:
在onCreate方法(或者onCreateView对于一个fragment),绑定PagerSlidingTabStrip控件到ViewPager上
<pre name="code" class="java">
.获取PagerSlidingTabStrip控件,绑定ViewPager
fragment,都是很简单的设置了不同的颜色背景而已,我这里就不再贴出代码了
(可选的)如果你想在你的ViewPager用到onPageChangeListener监听方法,你应该如下设置,而不是直接用ViewPager设置
//从上面继续
tabs.setOnPageChangeListener(mPageChangeListener);
ViewPager Adapter 代码如下:
其中,1.android:layout_below="@id/tabs"一定要记得写上,是让ViewPager控件在tabs之下。
2. xmlns:app="http://schemas.android.com/apk/res/com.example.viewfragment, 这句话是我的理解是,如果你想要用PagerSlidingTabStrip,就必须加上,不过他会自动加,不需要你来管他。
<pre name="code" class="java">public class MyPagerAdapter extends FragmentPagerAdapter {
private final String[] TITLES = { "Categories", "Home", "Top Paid", "Top Free", "Top Grossing", "Top New Paid",
"Top New Free", "Trending" };
public MyPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public CharSequence getPageTitle(int position) {
return TITLES[position];
}
@Override
public int getCount() {
return TITLES.length;
}
@Override
public Fragment getItem(int position) {
return SuperAwesomeCardFragment.newInstance(position);
}
}
这样就已经完成了,很简单吧~!
个性化设置
为了让你的app不像另一个 Play Store上面的app,你可以添加这些属性来做出自己独具一格的应用。
pstsIndicatorColor
Color of the sliding indicator 滑动条的颜色pstsUnderlineColor
Color of the full-width line on the bottom of the view 滑动条所在的那个全宽线的颜色pstsDividerColor
Color of the dividers between tabs 每个标签的分割线的颜色pstsIndicatorHeight
Height of the sliding indicator 滑动条的高度pstsUnderlineHeight
Height of the full-width line on the bottom of the view 滑动条所在的那个全宽线的高度pstsDividerPadding
Top and bottom padding of the dividers 分割线底部和顶部的填充宽度pstsTabPaddingLeftRight
Left and right padding of each tab 每个标签左右填充宽度pstsScrollOffset
Scroll offset of the selected tabpstsTabBackground
Background drawable of each tab, should be a StateListDrawable 每个标签的背景,应该是一个StateListDrawablepstsShouldExpand
If set to true, each tab is given the same weight, default false 如果设置为true,每个标签是相同的控件,均匀平分整个屏幕,默认是falsepstsTextAllCaps
If true, all tab titles will be upper case, default true 如果为true,所有标签都是大写字母,默认为true
所有的属性都有他们自己的getter和setter方法来随时改变他们
关于TypedValue.applyDimension()这个方法,你可以查看 http://blog.csdn.net/harryweasley/article/details/42172321
关于params.setMargins(margin, margin, margin, margin);你可以查看http://www.itnose.net/detail/6038494.html
这里就不在解释了
1.关于cannot convert from Fragment1 to Fragment这个错误,你可以查看http://blog.csdn.net/jason0539/article/details/9712273
2.关于TypedValue.applyDimension()这个方法,你可以查看http://blog.csdn.net/harryweasley/article/details/42172321
3.关于params.setMargins(margin, margin, margin, margin);你可以查看http://www.itnose.net/detail/6038494.html
</pre><pre code_snippet_id="120392" snippet_file_name="blog_20131220_3_2494930" name="code" class="java" style="margin-top: 0px; margin-bottom: 10px; font-size: 13px; line-height: 24px; background-color: rgb(255, 255, 255);">