1
wxr
2023-04-23 2cd55265ccff3b0a267d7953b2dd9e5dca437aa6
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
/**  
 * MyGallery.java
 * @version 1.0
 * @author Haven
 * @createTime 2011-12-9 下午03:42:53
 * android.widget.Gallery的子函数。此类很重要。建议仔细看
 */
package com.videogo.widget;
 
import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.Gallery;
 
public class PagesGallery extends Gallery {
    private static final String TAG = "PagesGallery";
    
    private boolean setSelection = false;
    
    public PagesGallery(Context context) {
        super(context);
 
    }
 
    public PagesGallery(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }
 
    public PagesGallery(Context context, AttributeSet attrs) {
        super(context, attrs);
    }
 
    @Override
    public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
        if(setSelection) {
            setSelection = false;
            return false;
        }
        if (distanceX > 50 && this.getSelectedItemPosition() == this.getCount() - 1 && this.getCount() > 1)// 向左滑动
        {
            this.setSelection(0);
            setSelection = true;
        } else if (distanceX < -50 && this.getSelectedItemPosition() == 0 && this.getCount() > 1)// 向右滑动
        {
            this.setSelection(this.getCount() - 1);
            setSelection = true;
        } else {
            super.onScroll(e1, e2, distanceX * 2, distanceY);
        }
        return false;
    }
 
    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
        return false;
    }
}