admin
2022-08-09 399ac289f80b7a40aa4210341db6b447cacdcf14
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
package com.tejia.lijin.app.ui.sellwellcommodity;
 
import android.content.Context;
import androidx.viewpager.widget.ViewPager;
import android.util.AttributeSet;
import android.view.MotionEvent;
 
public class BannerViewPager extends ViewPager {
 
    /**
     * the last x position
     */
    private float lastX;
 
    /**
     * if the first swipe was from left to right (->), dont listen to swipes from the right
     */
    private boolean slidingLeft;
 
    /**
     * if the first swipe was from right to left (<-), dont listen to swipes from the left
     */
    private boolean slidingRight;
 
    public BannerViewPager(final Context context, final AttributeSet attrs) {
        super(context, attrs);
    }
 
    public BannerViewPager(final Context context) {
        super(context);
    }
 
    @Override
    public boolean onTouchEvent(final MotionEvent ev) {
        final int action = ev.getAction();
        switch (action) {
            case MotionEvent.ACTION_DOWN:
 
                // Disallow parent ViewPager to intercept touch events.
                this.getParent().requestDisallowInterceptTouchEvent(true);
 
                // save the current x position
                this.lastX = ev.getX();
 
                break;
 
            case MotionEvent.ACTION_UP:
                // Allow parent ViewPager to intercept touch events.
                this.getParent().requestDisallowInterceptTouchEvent(false);
 
                // save the current x position
                this.lastX = ev.getX();
 
                // reset swipe actions
                this.slidingLeft = false;
                this.slidingRight = false;
 
                break;
 
            case MotionEvent.ACTION_MOVE:
                /*
                 * if this is the first item, scrolling from left to
                 * right should navigate in the surrounding ViewPager
                 */
                if (this.getCurrentItem() == 0) {
                    // swiping from left to right (->)?
                    if (this.lastX <= ev.getX() && !this.slidingRight) {
                        // make the parent touch interception active -> parent pager can swipe
                        this.getParent().requestDisallowInterceptTouchEvent(false);
                    } else {
                        /*
                         * if the first swipe was from right to left, dont listen to swipes
                         * from left to right. this fixes glitches where the user first swipes
                         * right, then left and the scrolling state gets reset
                         */
                        this.slidingRight = true;
 
                        // save the current x position
                        this.lastX = ev.getX();
                        this.getParent().requestDisallowInterceptTouchEvent(true);
                    }
                } else
                    /*
                     * if this is the last item, scrolling from right to
                     * left should navigate in the surrounding ViewPager
                     */
                    if (this.getCurrentItem() == this.getAdapter().getCount() - 1) {
                        // swiping from right to left (<-)?
                        if (this.lastX >= ev.getX() && !this.slidingLeft) {
                            // make the parent touch interception active -> parent pager can swipe
                            this.getParent().requestDisallowInterceptTouchEvent(false);
                        } else {
                            /*
                             * if the first swipe was from left to right, dont listen to swipes
                             * from right to left. this fixes glitches where the user first swipes
                             * left, then right and the scrolling state gets reset
                             */
                            this.slidingLeft = true;
 
                            // save the current x position
                            this.lastX = ev.getX();
                            this.getParent().requestDisallowInterceptTouchEvent(true);
                        }
                    }
 
                break;
        }
 
        super.onTouchEvent(ev);
        return true;
    }
}