lmw
2025-04-24 718f31c92e2029d05260810435a2c70cef6e6ce5
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
package com.ypx.imagepicker.widget;
 
import android.content.Context;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.RecyclerView;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
 
/**
 * Description: 可监听滑动的recyclerView
 * <p>
 * Author: peixing.yang
 * Date: 2019/2/22
 */
public class TouchRecyclerView extends RecyclerView {
    public TouchRecyclerView(@NonNull Context context) {
        super(context);
    }
 
    public TouchRecyclerView(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }
 
    public TouchRecyclerView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }
 
    float firstY = 0;
    float lastY = 0;
 
    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        switch (ev.getAction()) {
            case MotionEvent.ACTION_DOWN:
                lastY = ev.getY();
                break;
            case MotionEvent.ACTION_MOVE:
                float y = ev.getY();
 
                if (y < lastY) {
                    if (isTouchPointInView(touchView, ev.getX(), ev.getY())) {
                        if (dragScrollListener != null) {
                            int distance = (int) ((y - lastY));
                            int defaultDis = (int) (lastY - getPaddingTop());
                            dragScrollListener.onScrollOverTop(Math.abs(distance + defaultDis));
                        }
                    }
                } else {
                    if (dragScrollListener != null) {
                        dragScrollListener.onScrollDown((int) (y - lastY));
                    }
                }
                break;
            case MotionEvent.ACTION_UP:
            case MotionEvent.ACTION_CANCEL:
                lastY = 0;
                if (dragScrollListener != null) {
                    dragScrollListener.onScrollUp();
                }
                break;
        }
        return super.dispatchTouchEvent(ev);
    }
 
    private View touchView;
 
    public void setTouchView(View view) {
        this.touchView = view;
    }
 
    //(x,y)是否在view的区域内
    private boolean isTouchPointInView(View view, float x, float y) {
        if (view == null) {
            return false;
        }
        int[] location = new int[2];
        view.getLocationOnScreen(location);
        int left = location[0];
        int top = location[1];
        int right = left + view.getMeasuredWidth();
        int bottom = top + view.getMeasuredHeight();
        //view.isClickable() &&
        if (y >= top && y <= bottom && x >= left
                && x <= right) {
            return true;
        }
        return false;
    }
 
    private onDragScrollListener dragScrollListener;
 
    public void setDragScrollListener(onDragScrollListener dragScrollListener) {
        this.dragScrollListener = dragScrollListener;
    }
 
    public interface onDragScrollListener {
        void onScrollOverTop(int distance);
 
        void onScrollDown(int distance);
 
        void onScrollUp();
    }
}