最近搞一個項目,要求作一個和網易新聞頂部菜單的滑動效果,如圖:java
頂部標題中下面有個紅色的矩形小條,左右滑動時會跟隨手勢動態滑動,效果很絢麗,唉,特效啊!android
本身搞了一上午無果,仍是是github上找大神吧,最後發現了PagerSlidingTabStrip這個庫,以下圖:git
頂部就是用的PagerSlidingTabStrip下面是用ViewPager實現的,紅色矩形條能夠跟着屏幕的滑動而滑動,效果一樣很是炫,最重要的是使用很是方便,引入library方式或者本身整理出來均可以,很方便很實用,Github地址爲:https://github.com/astuetz/PagerSlidingTabStrip 萬能的Github啊。具體怎麼引入工程中使用就很少介紹了,你們都搞的定的,下面簡單介紹下這個類庫經常使用的方法和屬性。github
PagerSlidingTabStrip經常使用屬性以下,全部的屬性均可以在xml中或者Activity中設置,能夠經過get和set方法來設置屬性:ide
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 每一個標籤的背景,應該是一個StateListDrawable pstsShouldExpand
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簡單介紹下用法,下載後在sample/文件夾下可運行的示例工程,能夠參考着寫佈局
第一步、引入library做爲本地包工程,在你的layout的xml佈局文件中加入PagerSlidingTabStrip控件spa
<com.demo.PagerSlidingTabStrip android:id="@+id/tabs" android:layout_width="match_parent" android:layout_height="50dp" />
第二步、在Activity的onCreate方法中綁定PagerSlidingTabStrip控件到ViewPager上code
//爲ViewPager設置適配器
ViewPager .setAdapter(new MyAdapter(getSupportFragmentManager()));
//ViewPager綁定PagerSlidingTabStrip
PagerSlidingTabStrip tabs = (PagerSlidingTabStrip) findViewById(R.id.tab_one);
tabs.setViewPager(pager);
注意:這裏的ViewPager的適配器必須是繼承的FragmentPagerAdapter,並重寫getPageIconResId(int position)或者getPageTitle(int position)方法
第三步、設置onPageChangeListener監聽方法xml
tabs.setOnPageChangeListener(onPageChangeListener);