ViewPager的简单使用

1、布局文件


a、主布局文件

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:tools="http://schemas.android.com/tools"
  4. android:layout_width="match_parent"
  5. android:layout_height="match_parent"
  6. android:orientation="vertical"
  7. tools:context="lpc.com.animation2demo.MainActivity">
  8. <LinearLayout
  9. android:layout_width="match_parent"
  10. android:layout_height="48dp">
  11. <TextView
  12. android:id="@+id/tv1"
  13. android:text="苹果"
  14. android:gravity="center"
  15. android:layout_weight="1"
  16. android:layout_width="match_parent"
  17. android:layout_height="match_parent" />
  18. <TextView
  19. android:id="@+id/tv2"
  20. android:text="香蕉"
  21. android:gravity="center"
  22. android:layout_weight="1"
  23. android:layout_width="match_parent"
  24. android:layout_height="match_parent" />
  25. <TextView
  26. android:text="梨"
  27. android:id="@+id/tv3"
  28. android:gravity="center"
  29. android:layout_weight="1"
  30. android:layout_width="match_parent"
  31. android:layout_height="match_parent" />
  32. </LinearLayout>
  33. <ImageView
  34. android:id="@+id/iv"
  35. android:src="@mipmap/line"
  36. android:scaleType="matrix"
  37. android:layout_width="match_parent"
  38. android:layout_height="wrap_content" />
  39. <android.support.v4.view.ViewPager
  40. android:id="@+id/viewPager1"
  41. android:layout_weight="1"
  42. android:layout_width="match_parent"
  43. android:flipInterval="30"
  44. android:layout_gravity="center"
  45. android:persistentDrawingCache="animation"
  46. android:layout_height="0dp">
  47. </android.support.v4.view.ViewPager>
  48. </LinearLayout>


b、子布局

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:orientation="vertical" android:layout_width="match_parent"
  4. android:layout_height="match_parent">
  5. <ImageView
  6. android:background="@drawable/a1"
  7. android:layout_width="match_parent"
  8. android:layout_height="match_parent" />
  9. </LinearLayout>

另外两子布局类似,只是图片和id不一样

2、主Java文件


  1. package lpc.com.animation2demo;
  2. import android.graphics.BitmapFactory;
  3. import android.graphics.Matrix;
  4. import android.os.Bundle;
  5. import android.support.v4.view.ViewPager;
  6. import android.support.v7.app.AppCompatActivity;
  7. import android.util.DisplayMetrics;
  8. import android.view.LayoutInflater;
  9. import android.view.View;
  10. import android.view.animation.Animation;
  11. import android.view.animation.TranslateAnimation;
  12. import android.widget.ImageView;
  13. import android.widget.TextView;
  14. import java.util.ArrayList;
  15. public class MainActivity extends AppCompatActivity implements View.OnClickListener,ViewPager.OnPageChangeListener{
  16. private TextView tv1; //文本对象1
  17. private TextView tv2; //文本对象2
  18. private TextView tv3; //文本对象3
  19. private ImageView iv; //图片对象(用于移动条)
  20. private ViewPager vp; //Viewpager对象
  21. private ArrayList<View> viewArrayList; //创建一个View储存数组
  22. private int offset = 0; //移动条图片的偏移量
  23. private int currentIndex = 0; //当前页面
  24. private int bmpWidth ; //图片宽度
  25. private int one = 0; //移动条滑动一页的距离
  26. private int two = 0; //滑动条移动两页的距离
  27. @Override
  28. protected void onCreate(Bundle savedInstanceState) {
  29. super.onCreate(savedInstanceState);
  30. setContentView(R.layout.activity_main);
  31. initView();
  32. }
  33. private void initView() {
  34. tv1 = (TextView) findViewById(R.id.tv1);
  35. tv2 = (TextView) findViewById(R.id.tv2);
  36. tv3 = (TextView) findViewById(R.id.tv3);
  37. iv = (ImageView) findViewById(R.id.iv);
  38. vp = (ViewPager) findViewById(R.id.viewPager1);
  39. //下划线动画的相关设置:
  40. bmpWidth = BitmapFactory.decodeResource(getResources(), R.mipmap.line).getWidth();// 获取图片宽度
  41. DisplayMetrics dm = new DisplayMetrics();
  42. getWindowManager().getDefaultDisplay().getMetrics(dm);
  43. int screenW = dm.widthPixels;// 获取分辨率宽度
  44. offset = (screenW / 3 - bmpWidth) / 2;// 计算偏移量
  45. Matrix matrix = new Matrix();
  46. matrix.postTranslate(offset, 0);
  47. iv.setImageMatrix(matrix);// 设置动画初始位置
  48. //移动的距离
  49. one = offset * 2 + bmpWidth;// 移动一页的偏移量,比如1->2,或者2->3
  50. two = one * 2;// 移动两页的偏移量,比如1直接跳3
  51. //往ViewPager填充View,同时设置点击事件与页面切换事件
  52. viewArrayList = new ArrayList<View>();
  53. LayoutInflater inflater = getLayoutInflater();
  54. viewArrayList.add(inflater.inflate(R.layout.view1,null,false));
  55. viewArrayList.add(inflater.inflate(R.layout.view2,null,false));
  56. viewArrayList.add(inflater.inflate(R.layout.view3,null,false));
  57. vp.setAdapter(new MyAdapter(viewArrayList));
  58. vp.setCurrentItem(0); //设置ViewPager当前页,从0开始算
  59. tv1.setOnClickListener(this);
  60. tv2.setOnClickListener(this);
  61. tv3.setOnClickListener(this);
  62. vp.addOnPageChangeListener(this);
  63. }
  64. @Override
  65. public void onClick(View view) {
  66. switch (view.getId()){
  67. case R.id.tv1:
  68. vp.setCurrentItem(0);
  69. break;
  70. case R.id.tv2:
  71. vp.setCurrentItem(1);
  72. break;
  73. case R.id.tv3:
  74. vp.setCurrentItem(2);
  75. break;
  76. }
  77. }
  78. @Override
  79. public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
  80. }
  81. @Override
  82. public void onPageSelected(int position) {
  83. Animation animation = null;
  84. switch (position){
  85. case 0:
  86. if (currentIndex == 1){
  87. animation = new TranslateAnimation(one,0,0,0);
  88. }else if (currentIndex == 2){
  89. animation = new TranslateAnimation(two,0,0,0);
  90. }
  91. break;
  92. case 1:
  93. if (currentIndex == 0){
  94. animation = new TranslateAnimation(offset,one,0,0);
  95. }else if (currentIndex == 2){
  96. animation = new TranslateAnimation(two,one,0,0);
  97. }
  98. break;
  99. case 2:
  100. if (currentIndex == 0){
  101. animation = new TranslateAnimation(offset,two,0,0);
  102. }else if (currentIndex == 1){
  103. animation = new TranslateAnimation(one,two,0,0);
  104. }
  105. break;
  106. }
  107. currentIndex = position;
  108. animation.setFillAfter(true);// true表示图片停在动画结束位置
  109. animation.setDuration(300); //设置动画时间为300毫秒
  110. iv.startAnimation(animation);//开始动画
  111. }
  112. @Override
  113. public void onPageScrollStateChanged(int state) {
  114. }
  115. }

3、适配器文件


  1. package lpc.com.animation2demo;
  2. import android.support.v4.view.PagerAdapter;
  3. import android.view.View;
  4. import android.view.ViewGroup;
  5. import java.util.ArrayList;
  6. /**
  7. * Created by Administrator on 2016/3/16.
  8. * 适配器
  9. */
  10. public class MyAdapter extends PagerAdapter {
  11. //创建一个对象数组 存储View
  12. private ArrayList<View> viewArrayList;
  13. //空构造函数
  14. public MyAdapter() {
  15. }
  16. //初始化对象数组
  17. public MyAdapter(ArrayList<View> viewArrayList) {
  18. this.viewArrayList = viewArrayList;
  19. }
  20. //获取对象数组的大小
  21. @Override
  22. public int getCount() {
  23. return viewArrayList.size();
  24. }
  25. //将视图转化成对象
  26. @Override
  27. public boolean isViewFromObject(View view, Object object) {
  28. return view == object;
  29. }
  30. //获取选定 对象数组并将其添加到viewpager中
  31. @Override
  32. public Object instantiateItem(ViewGroup container, int position) {
  33. container.addView(viewArrayList.get(position));
  34. return viewArrayList.get(position);
  35. }
  36. //移除选定的对象数组对象
  37. @Override
  38. public void destroyItem(ViewGroup container, int position, Object object) {
  39. container.removeView(viewArrayList.get(position));
  40. }
  41. }

4、manifest文件

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  3. package="lpc.com.animation2demo">
  4. <application
  5. android:allowBackup="true"
  6. android:icon="@mipmap/ic_launcher"
  7. android:label="@string/app_name"
  8. android:supportsRtl="true"
  9. android:theme="@style/AppTheme">
  10. <activity android:name=".MainActivity">
  11. <intent-filter>
  12. <action android:name="android.intent.action.MAIN" />
  13. <category android:name="android.intent.category.LAUNCHER" />
  14. </intent-filter>
  15. </activity>
  16. </application>
  17. </manifest>

5、实现效果










原文地址:https://www.cnblogs.com/liupengcheng/p/5286486.html