Android编程中ViewPage判断左右滑动方向的方法

时间:2022-01-14 09:43:03

本文实例讲述了Android编程中ViewPage判断左右滑动方向的方法。分享给大家供大家参考,具体如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package com.meityitian.app.views;
import android.content.Context;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.util.Log;
import com.meityitian.app.utils.Debug;
/**
 * 重写,添加了判定滑动方向的方法
 * @author zxy
 *
 */
public class meityitianViewPager extends ViewPager {
 private boolean left = false;
 private boolean right = false;
 private boolean isScrolling = false;
 private int lastValue = -1;
 private ChangeViewCallback changeViewCallback = null;
 public meityitianViewPager(Context context, AttributeSet attrs) {
  super(context, attrs);
  init();
 }
 public meityitianViewPager(Context context) {
  super(context);
  init();
 }
 /**
  * init method .
*/
 private void init() {
  setOnPageChangeListener(listener);
 }
 /**
  * listener ,to get move direction .
*/
 public OnPageChangeListener listener = new OnPageChangeListener() {
  @Override
  public void onPageScrollStateChanged(int arg0) {
   if (arg0 == 1) {
    isScrolling = true;
   } else {
    isScrolling = false;
   }
   Debug.infoByTag("meityitianViewPager",
     "meityitianViewPager onPageScrollStateChanged : arg0:"
       + arg0);
   if (arg0 == 2) {
    Debug.infoByTag("meityitianViewPager",
      "meityitianViewPager onPageScrollStateChanged direction left ? "
        + left);
    Debug.infoByTag("meityitianViewPager",
      "meityitianViewPager onPageScrollStateChanged direction right ? "
        + right);
    //notify ....
    if(changeViewCallback!=null){
     changeViewCallback.changeView(left, right);
    }
    right = left = false;
   }
  }
  @Override
  public void onPageScrolled(int arg0, float arg1, int arg2) {
   if (isScrolling) {
    if (lastValue > arg2) {
     // 递减,向右侧滑动
     right = true;
     left = false;
    } else if (lastValue < arg2) {
     // 递减,向右侧滑动
     right = false;
     left = true;
    } else if (lastValue == arg2) {
     right = left = false;
    }
   }
   Log.i("meityitianViewPager",
     "meityitianViewPager onPageScrolled last :arg2 ,"
       + lastValue + ":" + arg2);
   lastValue = arg2;
  }
  @Override
  public void onPageSelected(int arg0) {
   if(changeViewCallback!=null){
    changeViewCallback.getCurrentPageIndex(arg0);
   }
  }
 };
 /**
  * 得到是否向右侧滑动
  * @return true 为右滑动
*/
 public boolean getMoveRight(){
  return right;
 }
 /**
  * 得到是否向左侧滑动
  * @return true 为左做滑动
*/
 public boolean getMoveLeft(){
  return left;
 }
 /**
  * 滑动状态改变回调
  * @author zxy
  *
*/
 public interface ChangeViewCallback{
  /**
   * 切换视图 ?决定于left和right 。
   * @param left
   * @param right
*/
  public void changeView(boolean left,boolean right);
  public void getCurrentPageIndex(int index);
 }
 /**
  * set ...
  * @param callback
*/
 public void setChangeViewCallback(ChangeViewCallback callback){
  changeViewCallback = callback;
 }
}

希望本文所述对大家Android程序设计有所帮助。