Android-ViewPagerIndicator使用方法

来源:互联网 发布:金蝶软件精斗云 编辑:程序博客网 时间:2024/05/29 15:29

转自http://blog.csdn.net/dalancon/article/details/41696373

现在很多的应用页面都是由一个个的TAB组成的,我们可以用布局加事件监听实现tab ,只是这样的控制非常麻烦,而且有很多的开源项目可以实现这样的功能,我们今天就介绍一下ViewPagerIndicator,这个项目就是可以实现这种效果。


首先下载依赖项目

https://github.com/JakeWharton/Android-ViewPagerIndicator

 

这个页面上有集成到自己项目的方式,大致就是:

1、将该项目设为自己项目的依赖项目。

2、在相应窗口的布局文件中引入TabPageIndicator,在Android-ViewPagerIndicator项目中有很多的tab的样式,它们对应不同的类。

一般我们都是将Android-ViewPagerIndicator与viewpager组合使用,当我们切换tab的时候下面的viewpager也一起切换。

[html] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     android:layout_width="fill_parent"  
  3.     android:layout_height="fill_parent"  
  4.     android:orientation="vertical"  
  5.     >  
  6.     <com.viewpagerindicator.TabPageIndicator  
  7.         android:id="@+id/indicator"  
  8.         android:layout_width="fill_parent"  
  9.         android:layout_height="wrap_content" />  
  10.   
  11.     <android.support.v4.view.ViewPager  
  12.         android:id="@+id/pager"  
  13.         android:layout_width="fill_parent"  
  14.         android:layout_height="0dp"  
  15.         android:layout_weight="1" />  
  16. </LinearLayout>  

3、然后在窗口Activity或者Fragment内,我们获取该TabPageIndicator和ViewPager,并将它们关联起来。

[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. pager = (ViewPager) findViewById(R.id.pager);  
  2. indicator = (TabPageIndicator) findViewById(R.id.indicator);  
  3. indicator.setViewPager(pager);//关联  

4、改变tab的样式,我们这边只看TabPageIndicator的样式修改,其他基本类似。我们进入TabPageIndicator的源码在构造函数。

[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. public TabPageIndicator(Context context, AttributeSet attrs) {  
  2.                             super(context, attrs);  
  3.     setHorizontalScrollBarEnabled(false);  
  4.     mTabLayout = new IcsLinearLayout(context, R.attr.vpiTabPageIndicatorStyle);  
  5.     addView(mTabLayout, new ViewGroup.LayoutParams(WRAP_CONTENT, MATCH_PARENT));  
  6. }  

我们可以看出TabPageIndicator使用的是vpiTabPageIndicatorStyle样式。我们可以在依赖项目中看到系统自带的样式,在依赖项目的values/vpi_styles.xml文件中,这里面定义了所有tab类型的样式。

[html] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. <style name="Widget.TabPageIndicator" parent="Widget">  
  2.         <item name="android:gravity">center</item>  
  3.         <item name="android:background">@drawable/vpi__tab_indicator</item>  
  4.         <item name="android:paddingLeft">22dip</item>  
  5.         <item name="android:paddingRight">22dip</item>  
  6.         <item name="android:paddingTop">12dp</item>  
  7.         <item name="android:paddingBottom">12dp</item>  
  8.         <item name="android:textAppearance">@style/TextAppearance.TabPageIndicator</item>  
  9.         <item name="android:textSize">12sp</item>  
  10.         <item name="android:maxLines">1</item>  
  11.     </style>  

我们可以根据自己的需要继承这个样式并修改。

到这里基本就OK了。Android-ViewPagerIndicator的集成非常简单的。

 

Android-ViewPagerIndicator可以与ActionBarSherlock合成开发的。非常方便


最后附上我的Viewpager适配器的类

[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. private class MyViewPagerAdapter extends FragmentPagerAdapter {  
  2.   
  3.         private List<SelectItem> mList = new ArrayList<SelectItem>();  
  4.   
  5.         public void setSelects(List<SelectItem> list) {  
  6.             this.mList = list;  
  7.         }  
  8.   
  9.         public MyViewPagerAdapter(FragmentManager fm) {  
  10.             super(fm);  
  11.         }  
  12.   
  13.         @Override  
  14.         public Fragment getItem(int position) {  
  15.             return mList.get(position).fragment;  
  16.         }  
  17.   
  18.         @Override  
  19.         public CharSequence getPageTitle(int position) {  
  20.             return mList.get(position).desc;  
  21.         }  
  22.   
  23.         @Override  
  24.         public int getCount() {  
  25.             return mList.size();  
  26.         }  
  27.   
  28.     }  
0 0