ViewPager顶部标题控件PagerSlidingTabStrip

      最近搞一个项目,要求作一个和网易新闻顶部菜单的滑动效果,如图: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   每一个标签的分割线的颜色
  • pstsIndicatorHeightHeight 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 tab
  • pstsTabBackground 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,每一个标签是相同的控件,均匀平分整个屏幕,默认是false
  • pstsTextAllCaps 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);
相关文章
相关标签/搜索