最近搞一個項目,要求做一個和網易新聞頂部菜單的滑動效果,如圖:
頂部標題中下面有個紅色的矩形小條,左右滑動時會跟隨手勢動態滑動,效果很絢麗,唉,特效啊!
自己搞了一上午無果,還是是github上找大神吧,最后發現了PagerSlidingTabStrip這個庫,如下圖:
頂部就是用的PagerSlidingTabStrip下面是用ViewPager實現的,紅色矩形條可以跟着屏幕的滑動而滑動,效果同樣非常炫,最重要的是使用非常方便,引入library方式或者自己整理出來都可以,很方便很實用,Github地址為:https://github.com/astuetz/PagerSlidingTabStrip 萬能的Github啊。具體怎么引入工程中使用就不多介紹了,大家都搞的定的,下面簡單介紹下這個類庫常用的方法和屬性。
PagerSlidingTabStrip常用屬性如下,所有的屬性都可以在xml中或者Activity中設置,可以通過get和set方法來設置屬性:
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
簡單介紹下用法,下載后在sample/文件夾下可運行的示例工程,可以參考着寫
第一步、引入library作為本地包工程,在你的layout的xml布局文件中加入PagerSlidingTabStrip控件
<com.demo.PagerSlidingTabStrip android:id="@+id/tabs" android:layout_width="match_parent" android:layout_height="50dp" />
第二步、在Activity的onCreate方法中綁定PagerSlidingTabStrip控件到ViewPager上
//為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監聽方法
tabs.setOnPageChangeListener(onPageChangeListener);