Android仿网易新闻导航栏PagerSlidingTabStrip

效果图:
  • Android / 手机 / 平板 实现手指滑动切换页面,页面头部的tab下方出现一个条纹来显示当前页面。也可以点击tab来切换页面。可以更改tab的配色方案。
  • Android / 手机 / 平板 实现手指滑动切换页面,页面头部的tab下方出现一个条纹来显示当前页面。也可以点击tab来切换页面。可以更改tab的配色方案。
下载地址:

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,都是很简单的设置了不同的颜色背景而已,我这里就不再贴出代码了

[java]  view plain copy print ?
  1. @Override  
  2.     protected void onCreate(Bundle savedInstanceState) {  
  3.         super.onCreate(savedInstanceState);  
  4.         setContentView(R.layout.activity_main);  
  5.   
  6.         tabs = (PagerSlidingTabStrip) findViewById(R.id.tabs);  
  7.         pager = (ViewPager) findViewById(R.id.pager);  
  8.         adapter = new MyPagerAdapter(getSupportFragmentManager());  
  9.   
  10.         pager.setAdapter(adapter);  
  11.   
  12.         final int pageMargin = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 4, getResources()  
  13.                 .getDisplayMetrics());  
  14.         pager.setPageMargin(pageMargin);  
  15.   
  16.         tabs.setViewPager(pager);  
  17.   
  18.         changeColor(currentColor);  
  19.     } 
 
(可选的)如果你想在你的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   每个标签的分割线的颜色
  • 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

所有的属性都有他们自己的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);">

                
  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
YOLO高分设计资源源码,详情请查看资源内容中使用说明 YOLO高分设计资源源码,详情请查看资源内容中使用说明 YOLO高分设计资源源码,详情请查看资源内容中使用说明 YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值