服务器之家

服务器之家 > 正文

Android自定义ViewPager实例

时间:2021-06-03 16:00     来源/作者:xurong

本文实例讲述了Android自定义ViewPager的方法。分享给大家供大家参考,具体如下:

?
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.rong.activity;
import android.content.Context;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.VelocityTracker;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.Scroller;
public class CustomViewPager extends ViewGroup {
  private Context mContext;
  private int screenWidth;
  private int screenHight;
  private int lastMoveX = 0;
  private VelocityTracker velocityTracker;
  private int MAX_VELOCITY=600;
  private int curScreen=0;
  private Scroller scroller;
  public CustomViewPager(Context context) {
    super(context);
    mContext = context;
    initView();
  }
  public CustomViewPager(Context context, AttributeSet attrs) {
    super(context, attrs);
    mContext = context;
    initView();
  }
  public CustomViewPager(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    mContext = context;
    initView();
  }
  @Override
  public boolean onTouchEvent(MotionEvent event) {
    if (velocityTracker == null) {
      velocityTracker = VelocityTracker.obtain();
    }
    velocityTracker.addMovement(event);
    int x = (int) event.getX();
    switch (event.getAction()) {
    case MotionEvent.ACTION_DOWN:
      lastMoveX = x;
      break;
    case MotionEvent.ACTION_MOVE:
      int dis = lastMoveX - x;
      scrollBy(dis, 0);
      lastMoveX = x;
      break;
    case MotionEvent.ACTION_UP:
      velocityTracker.computeCurrentVelocity(1000);
      int velocityX=(int)velocityTracker.getXVelocity();
      if(velocityX>MAX_VELOCITY&&curScreen>0){
        jump2Screen(curScreen-1);
      }else if(velocityX<-MAX_VELOCITY&&curScreen<getChildCount()-1){
        jump2Screen(curScreen+1);
      }else{
        int screen=(getScrollX()+screenWidth/2)/screenWidth;
        jump2Screen(screen);
      }
      if (velocityTracker != null) {
        velocityTracker.recycle();
        velocityTracker = null;
      }
      break;
    }
    return true;
  }
  /**
   * 跳转到指定Screen
   * @param screen
   */
  public void jump2Screen(int screen){
    curScreen=screen;
    if(curScreen>getChildCount()-1){
      curScreen=getChildCount()-1;
    }
    int dis=curScreen*screenWidth-getScrollX();
    scroller.startScroll(getScrollX(), 0, dis, 0);
    invalidate();
  }
  @Override
  public void computeScroll() {
    if(scroller.computeScrollOffset()){
      scrollTo(scroller.getCurrX(), 0);
      postInvalidate();
    }
  }
  public void initView() {
    scroller=new Scroller(mContext);
    LinearLayout layout1 = new LinearLayout(getContext());
    layout1.setBackgroundColor(Color.RED);
    addView(layout1);
    LinearLayout layout2 = new LinearLayout(getContext());
    layout2.setBackgroundColor(Color.GREEN);
    addView(layout2);
    LinearLayout layout3 = new LinearLayout(getContext());
    layout3.setBackgroundColor(Color.BLUE);
    addView(layout3);
  }
  @Override
  protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    screenWidth = MeasureSpec.getSize(widthMeasureSpec);
    screenHight = MeasureSpec.getSize(heightMeasureSpec);
    setMeasuredDimension(screenWidth, screenHight);
    for (int i = 0; i < getChildCount(); i++) {
      View view = getChildAt(i);
      view.measure(screenWidth, screenHight);
    }
  }
  @Override
  protected void onLayout(boolean changed, int l, int t, int r, int b) {
    int leftWidth = 0;
    for (int i = 0; i < getChildCount(); i++) {
      View view = getChildAt(i);
      view.layout(leftWidth, 0, leftWidth + screenWidth, screenHight);
      leftWidth = leftWidth + screenWidth;
    }
  }
}

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

标签:

相关文章

热门资讯

2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全 2019-12-26
yue是什么意思 网络流行语yue了是什么梗
yue是什么意思 网络流行语yue了是什么梗 2020-10-11
背刺什么意思 网络词语背刺是什么梗
背刺什么意思 网络词语背刺是什么梗 2020-05-22
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总 2020-11-13
2021德云社封箱演出完整版 2021年德云社封箱演出在线看
2021德云社封箱演出完整版 2021年德云社封箱演出在线看 2021-03-15
返回顶部