Android 应用开发----7. ViewPager+Fragment一步步打造顶部导航界面滑动效果

本文详述了使用ViewPager和Fragment在Android应用中实现顶部导航界面滑动效果的步骤。从创建FragmentActivity和Fragment,到添加Adapter及实现监听事件,最后添加指示标签,全过程深入浅出。

ViewPager+Fragment一步步打造顶部导航界面滑动效果

 

在许多应用中,我们常常用到这么一个效果:

可以看到,由于现在的应用数据经常需要涉及到多个模块,所以常常需要使用滑动标签在多个页面之间跳转,实现这样的效果有很多种方式(比如系统自带的tabhost控件),但android-support-v4包中还为我们提供了另外一个专门实现滑动页面的控件——ViewPager,ViewPager中提供了很多接口,能让我们用很少的代码就能实现分屏页面滑动,本文也将分享如何一步一步实现ViewPager+fragment组合来轻松实现分页滑动效果,先上最终效果图(由于gif总是录制失败,此处使用静态图):

 

实现这样一个效果,主要分为以下几步:
1.创建一个FragmentActivity作为主页面,并设计好对应的布局文件
2.创建几个fragment作为每个子页面的容器,并创建对应的布局文件
3.为ViewPager添加一个Adapter,将所有fragment添加进去
4.实现ViewPager的OnPageChangeListener监听事件,重写onPageSelected()方法,实现左右滑动页面
5.实现每个标题的onClick事件,点击跳转到相应页面
6.添加指示标签块,也就是标题栏下面那个红色的指示,计算指示标签的位移,使其与标题同步变化

 

 

工程目录如下:

 

接下来我们开始一步一步实现这个效果:

1.创建一个FragmentActivity作为主页面,并设计好对应的布局文件

先来看一下布局文件activity_main.xml:

 

 

 
  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

  2. xmlns:tools="http://schemas.android.com/tools"

  3. android:layout_width="match_parent"

  4. android:layout_height="match_parent"

  5. android:orientation="vertical" >

  6.  
  7. <LinearLayout

  8. android:id="@+id/bottomlinear"

  9. android:layout_width="fill_parent"

  10. android:layout_height="0dp"

  11. android:layout_weight="1"

  12. android:orientation="horizontal"

  13. android:background="#DCDCDC">

  14. <Button

  15. android:id="@+id/btn_first"

  16. android:layout_width="0dp"

  17. android:layout_height="fill_parent"

  18. android:layout_weight="1"

  19. android:padding="-5dp"

  20. android:textSize="14sp"

  21. android:text="最新"

  22. />

  23. <Button

  24. android:id="@+id/btn_second"

  25. android:layout_width="0dp"

  26. android:layout_height="fill_parent"

  27. android:layout_weight="1"

  28. android:textSize="14sp"

  29. android:text="前端"/>

  30. <Button

  31. android:id="@+id/btn_third"

  32. android:layout_width="0dp"

  33. android:layout_height="fill_parent"

  34. android:layout_weight="1.5"

  35. android:textSize="14sp"

  36. android:text="移动开发"/>

  37. <Button

  38. android:id="@+id/btn_four"

  39. android:layout_width="0dp"

  40. android:layout_height="fill_parent"

  41. android:layout_weight="1"

  42. android:textSize="14sp"

  43. android:text="语言"/>

  44. <Button

  45. android:id="@+id/btn_fifth"

  46. android:layout_width="0dp"

  47. android:layout_height="fill_parent"

  48. android:layout_weight="1.5"

  49. android:textSize="14sp"

  50. android:text="游戏&图像"/>

  51.  
  52. </LinearLayout>

  53. <LinearLayout

  54. android:id="@+id/cursorarea"

  55. android:layout_width="fill_parent"

  56. android:background="#CDCDCD"

  57. android:orientation="horizontal"

  58. android:layout_height="2dp">

  59. <ImageView

  60. android:id="@+id/cursor_btn"

  61. android:layout_width="fill_parent"

  62. android:layout_height="fill_parent">

  63. </ImageView>

  64. </LinearLayout>

  65.  
  66.  
  67. <android.support.v4.view.ViewPager

  68. android:id="@+id/myviewpager"

  69. android:layout_width="fill_parent"

  70. android:layout_height="0dp"

  71. android:layout_weight="12">

  72. </android.support.v4.view.ViewPager>

  73.  
  74.  
  75.  
  76. </LinearLayout>


其中,创建了五个按钮作为标题栏的5个标题,创建一个ImageView作为指示标签,再导入一个ViewPager位于标题栏下面

 

MainActivity.java(注意是继承自FragmentActivity类):

 

 
  1. public class MainActivity extends FragmentActivity{

  2.  
  3. private ViewPager myviewpager;

  4.  
  5. //选项卡中的按钮

  6. private Button btn_first;

  7. private Button btn_second;

  8. private Button btn_third;

  9. private Button btn_four;

  10. private Button btn_fifth;

  11. //作为指示标签的按钮

  12. private ImageView cursor;

  13. //标志指示标签的横坐标

  14. float cursorX = 0;

  15. //所有按钮的宽度的数组

  16. private int[] widthArgs;

  17. //所有标题按钮的数组

  18. private Button[] btnArgs;

  19.  
  20. @Override

  21. protected void onCreate(Bundle savedInstanceState) {

  22. super.onCreate(savedInstanceState);

  23. setContentView(R.layout.activity_main);

  24.  
  25. initView();

  26. }

  27.  
  28. //初始化布局

  29. public void initView(){

  30. myviewpager = (ViewPager)this.findViewById(R.id.myviewpager);

  31.  
  32. btn_first = (Button)this.findViewById(R.id.btn_first);

  33. btn_second = (Button)this.findViewById(R.id.btn_second);

  34. btn_third = (Button)this.findViewById(R.id.btn_third);

  35. btn_four = (Button)this.findViewById(R.id.btn_four);

  36. btn_fifth = (Button)this.findViewById(R.id.btn_fifth);

  37. //初始化按钮数组

  38. btnArgs = new Button[]{btn_first,btn_second,btn_third,btn_four,btn_fifth};

  39. //指示标签设置为红色

  40. cursor = (ImageView)this.findViewById(R.id.cursor_btn);

  41. cursor.setBackgroundColor(Color.RED);

  42.  
  43. btn_first.setOnClickListener(this);

  44. btn_second.setOnClickListener(this);

  45. btn_third.setOnClickListener(this);

  46. btn_four.setOnClickListener(this);

  47. btn_fifth.setOnClickListener(this);

  48.  
  49. //先重置所有按钮颜色

  50. resetButtonColor();

  51. //再将第一个按钮字体设置为红色,表示默认选中第一个

  52. btn_first.setTextColor(Color.RED);

  53.  
  54. }

  55.  
  56. //重置所有按钮的颜色

  57. public void resetButtonColor(){

  58. btn_first.setBackgroundColor(Color.parseColor("#DCDCDC"));

  59. btn_second.setBackgroundColor(Color.parseColor("#DCDCDC"));

  60. btn_third.setBackgroundColor(Color.parseColor("#DCDCDC"));

  61. btn_four.setBackgroundColor(Color.parseColor("#DCDCDC"));

  62. btn_fifth.setBackgroundColor(Color.parseColor("#DCDCDC"));

  63. btn_first.setTextColor(Color.BLACK);

  64. btn_second.setTextColor(Color.BLACK);

  65. btn_third.setTextColor(Color.BLACK);

  66. btn_four.setTextColor(Color.BLACK);

  67. btn_fifth.setTextColor(Color.BLACK);

  68. }

  69. }

 

 

 

2.创建几个fragment作为每个子页面的容器,并创建对应的布局文件

layout_first.xml:

 

 

 
  1. <?xml version="1.0" encoding="utf-8"?>

  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

  3. android:layout_width="match_parent"

  4. android:layout_height="match_parent"

  5. android:orientation="vertical" >

  6.  
  7. <TextView

  8. android:layout_width="fill_parent"

  9. android:layout_height="fill_parent"

  10. android:gravity="center"

  11. android:layout_gravity="center"

  12. android:text="这是第一个Fragment"/>

  13.  
  14. </LinearLayout>

 

FirstFragment.java:

 

 
  1. public class FirstFragment extends Fragment{

  2.  
  3. @Override

  4. public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState) {

  5. // TODO Auto-generated method stub

  6. View v = inflater.inflate(R.layout.layout_first, container,false);

  7. return v;

  8. }

  9.  
  10. }

 

layout_second.xml:

 

 
  1. <?xml version="1.0" encoding="utf-8"?>

  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

  3. android:layout_width="match_parent"

  4. android:layout_height="match_parent"

  5. android:orientation="vertical" >

  6.  
  7. <TextView

  8. android:layout_width="fill_parent"

  9. android:layout_height="fill_parent"

  10. android:gravity="center"

  11. android:layout_gravity="center"

  12. android:text="这是第二个Fragment"/>

  13.  
  14. </LinearLayout>

 

SecondFragment.java:

 

 
  1. public class SecondFragment extends Fragment{

  2.  
  3. @Override

  4. public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState) {

  5. // TODO Auto-generated method stub

  6. View v = inflater.inflate(R.layout.layout_second, container,false);

  7. return v;

  8. }

  9.  
  10. }

 

layout_thrid.xml:

 

 
  1. <?xml version="1.0" encoding="utf-8"?>

  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

  3. android:layout_width="match_parent"

  4. android:layout_height="match_parent"

  5. android:orientation="vertical" >

  6.  
  7. <TextView

  8. android:layout_width="fill_parent"

  9. android:layout_height="fill_parent"

  10. android:gravity="center"

  11. android:layout_gravity="center"

  12. android:text="这是第三个Fragment"/>

  13.  
  14. </LinearLayout>

 

ThridFragment.java:

 

 
  1. public class ThridFragment extends Fragment{

  2.  
  3. @Override

  4. public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState) {

  5. // TODO Auto-generated method stub

  6. View v = inflater.inflate(R.layout.layout_thrid, container,false);

  7. return v;

  8. }

  9.  
  10. }

 

layout_four.xml:

 

 
  1. <?xml version="1.0" encoding="utf-8"?>

  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

  3. android:layout_width="match_parent"

  4. android:layout_height="match_parent"

  5. android:orientation="vertical" >

  6.  
  7. <TextView

  8. android:layout_width="fill_parent"

  9. android:layout_height="fill_parent"

  10. android:gravity="center"

  11. android:layout_gravity="center"

  12. android:text="这是第四个Fragment"/>

  13.  
  14. </LinearLayout>

 

FourFragment.java:

 

 
  1. public class FourFragment extends Fragment{

  2.  
  3. @Override

  4. public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState) {

  5. // TODO Auto-generated method stub

  6. View v = inflater.inflate(R.layout.layout_four, container,false);

  7. return v;

  8. }

  9.  
  10. }

 

layout_fifth.xml:

 

 
  1. <?xml version="1.0" encoding="utf-8"?>

  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

  3. android:layout_width="match_parent"

  4. android:layout_height="match_parent"

  5. android:orientation="vertical" >

  6.  
  7. <TextView

  8. android:layout_width="fill_parent"

  9. android:layout_height="fill_parent"

  10. android:gravity="center"

  11. android:layout_gravity="center"

  12. android:text="这是第五个Fragment"/>

  13.  
  14. </LinearLayout>

 

FifthFragment.java:

 

 
  1. public class FifthFragment extends Fragment{

  2.  
  3. @Override

  4. public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState) {

  5. // TODO Auto-generated method stub

  6. View v = inflater.inflate(R.layout.layout_fifth, container,false);

  7. return v;

  8. }

  9.  
  10. }

 

每个Fragment的内容布局基本一致,这里只是简单地用一个TextView来表示当前页面是哪个页面,在onCreateView中使用inflate加载对应的布局文件

 

 

 

3.为ViewPager添加一个Adapter,将所有fragment添加进去

上面已经创建好了每个子页面对应的fragment,接下来要做的便是将这些fragment装载到ViewPager中去,android.support.v4.app包为我们提供了一个特别的迭代器——FragmentPagerAdapter,我们重写它的getItem()getCount()方法,分别返回第几个fragment以及fragment的数量,可以这么理解:此步相当于让ViewPager能够控制管理我们的fragment

MyFragmentPagerAdapter.java:

 

 

 
  1. public class MyFragmentPagerAdapter extends FragmentPagerAdapter{

  2. //存储所有的fragment

  3. private List<Fragment> list;

  4.  
  5. public MyFragmentPagerAdapter(FragmentManager fm, ArrayList<Fragment> list){

  6. super(fm);

  7. this.list = list;

  8. }

  9.  
  10. @Override

  11. public Fragment getItem(int arg0) {

  12. // TODO Auto-generated method stub

  13. return list.get(arg0);

  14. }

  15.  
  16. @Override

  17. public int getCount() {

  18. // TODO Auto-generated method stub

  19. return list.size();

  20. }

  21.  
  22. }

 

创建完adapter后,我们还要在MainActivity中将所有fragment添加到一个list并作为构造参数传到adapter中去:

 

 
  1. //fragment的集合,对应每个子页面

  2. private ArrayList<Fragment> fragments;

  3. fragments = new ArrayList<Fragment>();

  4. fragments.add(new FirstFragment());

  5. fragments.add(new SecondFragment());

  6. fragments.add(new ThridFragment());

  7. fragments.add(new FourFragment());

  8. fragments.add(new FifthFragment());

  9. MyFragmentPagerAdapter adapter = new MyFragmentPagerAdapter(getSupportFragmentManager(),fragments);

 

将装载了数据的adapter设置给viewpager

 

myviewpager.setAdapter(adapter);

 

 

4.实现ViewPager的OnPageChangeListener监听事件,重写onPageSelected()方法,实现左右滑动页面

让Activity实现监听接口:

为myviewpager注册监听:

 

myviewpager.setOnPageChangeListener(this);

 

 

实现三个接口方法,这里关键在于重写onPageSelected方法,onPageSelected会在每次滑动ViewPager的时候触发,所以所有滑动时的变化都可以在这里面定义,比如标题按钮的颜色随着滑动的变化等

 

 
  1. @Override

  2. public void onPageScrollStateChanged(int arg0) {

  3. // TODO Auto-generated method stub

  4.  
  5. }

  6.  
  7. @Override

  8. public void onPageScrolled(int arg0, float arg1, int arg2) {

  9. // TODO Auto-generated method stub

  10.  
  11. }

  12.  
  13. @Override

  14. public void onPageSelected(int arg0) {

  15. // TODO Auto-generated method stub

  16. //每次滑动首先重置所有按钮的颜色

  17. resetButtonColor();

  18. //将滑动到的当前按钮颜色设置为红色

  19. btnArgs[arg0].setTextColor(Color.RED);

  20. }

 

 

 

5.实现每个标题的onClick事件,点击跳转到相应页面

在上一步,已经实现了左右滑动切换页面效果,但发现点击标题栏并无响应,所以我们需要为每个button添加一个点击事件:
让Activity实现监听接口:

 

为所有标题按钮注册监听:

 

 
  1. btn_first.setOnClickListener(this);

  2. btn_second.setOnClickListener(this);

  3. btn_third.setOnClickListener(this);

  4. btn_four.setOnClickListener(this);

  5. btn_fifth.setOnClickListener(this);

 

重写onclick:

 

 
  1. @Override

  2. public void onClick(View whichbtn) {

  3. // TODO Auto-generated method stub

  4.  
  5. switch (whichbtn.getId()) {

  6. case R.id.btn_first:

  7. myviewpager.setCurrentItem(0);

  8. break;

  9. case R.id.btn_second:

  10. myviewpager.setCurrentItem(1);

  11. break;

  12. case R.id.btn_third:

  13. myviewpager.setCurrentItem(2);

  14. break;

  15. case R.id.btn_four:

  16. myviewpager.setCurrentItem(3);

  17. break;

  18. case R.id.btn_fifth:

  19. myviewpager.setCurrentItem(4);

  20. break;

  21. }

  22. }

 

 

可以看到,只是一句简单的setCurrentItem方法的调用,就能实现跳转到对应的子页面,所以才说ViewPager非常的方便

 

 

 

6.添加指示标签块,也就是标题栏下面那个红色的指示,计算指示标签的位移,使其与标题同步变化

上面的步骤其实已经实现了大部分功能,但为了让我们的界面更加友好一些,需要再添加一个指示器,用来指示当前处于哪个页面,要实现这种功能,要注意以下几点:
【在滑动到某个子页面时,指示器需要横向跳到相应的位置】
【在滑动到某个子页面时,指示器需要变化到与当前标题一样的大小】

如图:

 

 

 

首先创建两个数组,便于根据下标得到某个按钮以及对应的宽度:

 

 
  1. //所有按钮的宽度的集合

  2. private int[] widthArgs;

  3. //所有按钮的集合

  4. private Button[] btnArgs;

 

 

注意两个数组实例化的位置不同,btnArgs是像平常一样在onCreate方法中实例化,而widthArgs在滑动的时候再实例化,因为在onCreate方法中获取不了所有按钮的宽度,因为系统还未测量它们的宽度
btnArgs的实例化:

 

btnArgs = new Button[]{btn_first,btn_second,btn_third,btn_four,btn_fifth};

 

 

widthArgs的实例化:

 

初始化指示器位置和大小:

 

 
  1. btn_first.post(new Runnable(){

  2. @Override

  3. public void run() {

  4. LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams)cursor.getLayoutParams();

  5. //减去边距*2,以对齐标题栏文字

  6. lp.width = btn_first.getWidth()-btn_first.getPaddingLeft()*2;

  7. cursor.setLayoutParams(lp);

  8. cursor.setX(btn_first.getPaddingLeft());

  9. }

  10. });

 

这里需要解释一下,为什么不直接cursor.setWidth()和cursor.setX()?因为Android系统绘制原理是只有全部遍历测量之后才会布局,只有在整个布局绘制完毕后,视图才能得到自身的高和宽。所以在正常情况下,在OnCreate()方法中直接获取控件的宽度和高度取得值是0。而我们此处设置指示器的大小和位置都需要用到第一个按钮的大小作为参考值,所以可以通过post将一个runnable投递到消息队列的尾部,然后等待UI线程Looper调用此runnable的时候,view也已经初始化好了。这个时候就能成功获取控件的宽高。

 

指示器的动态变化方法如下,注释得已经很清楚:

 

 
  1. //指示器的跳转,传入当前所处的页面的下标

  2. public void cursorAnim(int curItem){

  3. //每次调用,就将指示器的横坐标设置为0,即开始的位置

  4. cursorX = 0;

  5. //再根据当前的curItem来设置指示器的宽度

  6. LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams)cursor.getLayoutParams();

  7. //减去边距*2,以对齐标题栏文字

  8. lp.width = widthArgs[curItem]-btnArgs[0].getPaddingLeft()*2;

  9. cursor.setLayoutParams(lp);

  10. //循环获取当前页之前的所有页面的宽度

  11. for(int i=0; i<curItem; i++){

  12. cursorX = cursorX + btnArgs[i].getWidth();

  13. }

  14. //再加上当前页面的左边距,即为指示器当前应处的位置

  15. cursor.setX(cursorX+btnArgs[curItem].getPaddingLeft());

  16. }

 

接下来只需要在刚才的那些onClick以及onPageSelected方法中调用它就可以了:

 

 

好了,总算完成所要的效果,完整的MainActivity代码如下:

 

 
  1. public class MainActivity extends FragmentActivity implements OnClickListener, OnPageChangeListener{

  2.  
  3. private ViewPager myviewpager;

  4. //fragment的集合,对应每个子页面

  5. private ArrayList<Fragment> fragments;

  6. //选项卡中的按钮

  7. private Button btn_first;

  8. private Button btn_second;

  9. private Button btn_third;

  10. private Button btn_four;

  11. private Button btn_fifth;

  12. //作为指示标签的按钮

  13. private ImageView cursor;

  14. //标志指示标签的横坐标

  15. float cursorX = 0;

  16. //所有按钮的宽度的集合

  17. private int[] widthArgs;

  18. //所有按钮的集合

  19. private Button[] btnArgs;

  20.  
  21. @Override

  22. protected void onCreate(Bundle savedInstanceState) {

  23. super.onCreate(savedInstanceState);

  24. setContentView(R.layout.activity_main);

  25.  
  26. initView();

  27. }

  28.  
  29. public void initView(){

  30. myviewpager = (ViewPager)this.findViewById(R.id.myviewpager);

  31.  
  32. btn_first = (Button)this.findViewById(R.id.btn_first);

  33. btn_second = (Button)this.findViewById(R.id.btn_second);

  34. btn_third = (Button)this.findViewById(R.id.btn_third);

  35. btn_four = (Button)this.findViewById(R.id.btn_four);

  36. btn_fifth = (Button)this.findViewById(R.id.btn_fifth);

  37. btnArgs = new Button[]{btn_first,btn_second,btn_third,btn_four,btn_fifth};

  38.  
  39. cursor = (ImageView)this.findViewById(R.id.cursor_btn);

  40. cursor.setBackgroundColor(Color.RED);

  41.  
  42. myviewpager.setOnPageChangeListener(this);

  43. btn_first.setOnClickListener(this);

  44. btn_second.setOnClickListener(this);

  45. btn_third.setOnClickListener(this);

  46. btn_four.setOnClickListener(this);

  47. btn_fifth.setOnClickListener(this);

  48.  
  49. fragments = new ArrayList<Fragment>();

  50. fragments.add(new FirstFragment());

  51. fragments.add(new SecondFragment());

  52. fragments.add(new ThridFragment());

  53. fragments.add(new FourFragment());

  54. fragments.add(new FifthFragment());

  55.  
  56. MyFragmentPagerAdapter adapter = new MyFragmentPagerAdapter(getSupportFragmentManager(),fragments);

  57. myviewpager.setAdapter(adapter);

  58.  
  59. resetButtonColor();

  60. btn_first.setTextColor(Color.RED);

  61.  
  62. }

  63.  
  64. //重置所有按钮的颜色

  65. public void resetButtonColor(){

  66. btn_first.setBackgroundColor(Color.parseColor("#DCDCDC"));

  67. btn_second.setBackgroundColor(Color.parseColor("#DCDCDC"));

  68. btn_third.setBackgroundColor(Color.parseColor("#DCDCDC"));

  69. btn_four.setBackgroundColor(Color.parseColor("#DCDCDC"));

  70. btn_fifth.setBackgroundColor(Color.parseColor("#DCDCDC"));

  71. btn_first.setTextColor(Color.BLACK);

  72. btn_second.setTextColor(Color.BLACK);

  73. btn_third.setTextColor(Color.BLACK);

  74. btn_four.setTextColor(Color.BLACK);

  75. btn_fifth.setTextColor(Color.BLACK);

  76. }

  77.  
  78. @Override

  79. public void onClick(View whichbtn) {

  80. // TODO Auto-generated method stub

  81.  
  82. switch (whichbtn.getId()) {

  83. case R.id.btn_first:

  84. myviewpager.setCurrentItem(0);

  85. cursorAnim(0);

  86.  
  87. break;

  88. case R.id.btn_second:

  89. myviewpager.setCurrentItem(1);

  90. cursorAnim(1);

  91.  
  92. break;

  93. case R.id.btn_third:

  94. myviewpager.setCurrentItem(2);

  95. cursorAnim(2);

  96.  
  97. break;

  98. case R.id.btn_four:

  99. myviewpager.setCurrentItem(3);

  100. cursorAnim(3);

  101.  
  102. break;

  103. case R.id.btn_fifth:

  104. myviewpager.setCurrentItem(4);

  105. cursorAnim(4);

  106.  
  107. break;

  108. }

  109. }

  110.  
  111. @Override

  112. public void onPageScrollStateChanged(int arg0) {

  113. // TODO Auto-generated method stub

  114.  
  115. }

  116.  
  117. @Override

  118. public void onPageScrolled(int arg0, float arg1, int arg2) {

  119. // TODO Auto-generated method stub

  120.  
  121. }

  122.  
  123. @Override

  124. public void onPageSelected(int arg0) {

  125. // TODO Auto-generated method stub

  126. if(widthArgs==null){

  127. widthArgs = new int[]{btn_first.getWidth(),

  128. btn_second.getWidth(),

  129. btn_third.getWidth(),

  130. btn_four.getWidth(),

  131. btn_fifth.getWidth()};

  132. }

  133. //每次滑动首先重置所有按钮的颜色

  134. resetButtonColor();

  135. //将滑动到的当前按钮颜色设置为红色

  136. btnArgs[arg0].setTextColor(Color.RED);

  137. cursorAnim(arg0);

  138. }

  139.  
  140. //指示器的跳转,传入当前所处的页面的下标

  141. public void cursorAnim(int curItem){

  142. //每次调用,就将指示器的横坐标设置为0,即开始的位置

  143. cursorX = 0;

  144. //再根据当前的curItem来设置指示器的宽度

  145. LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams)cursor.getLayoutParams();

  146. //减去边距*2,以对齐标题栏文字

  147. lp.width = widthArgs[curItem]-btnArgs[0].getPaddingLeft()*2;

  148. cursor.setLayoutParams(lp);

  149. //循环获取当前页之前的所有页面的宽度

  150. for(int i=0; i<curItem; i++){

  151. cursorX = cursorX + btnArgs[i].getWidth();

  152. }

  153. //再加上当前页面的左边距,即为指示器当前应处的位置

  154. cursor.setX(cursorX+btnArgs[curItem].getPaddingLeft());

  155. }

  156. }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值