forked from h6ah4i/android-advancedrecyclerview
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RecyclerViewTouchActionGuardManager.java
230 lines (193 loc) · 7.01 KB
/
RecyclerViewTouchActionGuardManager.java
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
/*
* Copyright (C) 2015 Haruki Hasegawa
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.h6ah4i.android.widget.advrecyclerview.touchguard;
import android.support.annotation.NonNull;
import android.support.v4.view.MotionEventCompat;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.MotionEvent;
import android.view.ViewConfiguration;
/**
* Hooks touch events to avoid unexpected scrolling.
*/
public class RecyclerViewTouchActionGuardManager {
private static final String TAG = "ARVTouchActionGuardMgr";
private static final boolean LOCAL_LOGV = false;
private static final boolean LOCAL_LOGD = false;
private RecyclerView.OnItemTouchListener mInternalUseOnItemTouchListener;
private RecyclerView mRecyclerView;
private boolean mGuarding;
private int mInitialTouchY;
private int mLastTouchY;
private int mTouchSlop;
private boolean mEnabled;
private boolean mInterceptScrollingWhileAnimationRunning;
/**
* Constructor.
*/
public RecyclerViewTouchActionGuardManager() {
mInternalUseOnItemTouchListener = new RecyclerView.OnItemTouchListener() {
@Override
public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent e) {
return RecyclerViewTouchActionGuardManager.this.onInterceptTouchEvent(rv, e);
}
@Override
public void onTouchEvent(RecyclerView rv, MotionEvent e) {
RecyclerViewTouchActionGuardManager.this.onTouchEvent(rv, e);
}
@Override
public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {
}
};
}
/**
* Indicates this manager instance has released or not.
*
* @return True if this manager instance has released
*/
public boolean isReleased() {
return (mInternalUseOnItemTouchListener == null);
}
/**
* Attaches {@link android.support.v7.widget.RecyclerView} instance.
*
* @param rv The {@link android.support.v7.widget.RecyclerView} instance
*/
public void attachRecyclerView(@NonNull RecyclerView rv) {
if (isReleased()) {
throw new IllegalStateException("Accessing released object");
}
if (mRecyclerView != null) {
throw new IllegalStateException("RecyclerView instance has already been set");
}
mRecyclerView = rv;
mRecyclerView.addOnItemTouchListener(mInternalUseOnItemTouchListener);
mTouchSlop = ViewConfiguration.get(rv.getContext()).getScaledTouchSlop();
}
/**
* Detach the {@link android.support.v7.widget.RecyclerView} instance and release internal field references.
*
* This method should be called in order to avoid memory leaks.
*/
public void release() {
if (mRecyclerView != null && mInternalUseOnItemTouchListener != null) {
mRecyclerView.removeOnItemTouchListener(mInternalUseOnItemTouchListener);
}
mInternalUseOnItemTouchListener = null;
mRecyclerView = null;
}
/*package*/ boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent e) {
if (!mEnabled) {
return false;
}
final int action = MotionEventCompat.getActionMasked(e);
if (LOCAL_LOGV) {
Log.v(TAG, "onInterceptTouchEvent() action = " + action);
}
switch (action) {
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_CANCEL:
handleActionUpOrCancel();
break;
case MotionEvent.ACTION_DOWN:
handleActionDown(e);
break;
case MotionEvent.ACTION_MOVE:
if (handleActionMove(rv, e)) {
return true;
}
break;
}
return false;
}
/*package*/ void onTouchEvent(RecyclerView rv, MotionEvent e) {
if (!mEnabled) {
return;
}
final int action = MotionEventCompat.getActionMasked(e);
if (LOCAL_LOGV) {
Log.v(TAG, "onTouchEvent() action = " + action);
}
switch (action) {
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_CANCEL:
handleActionUpOrCancel();
break;
}
}
private boolean handleActionMove(RecyclerView rv, MotionEvent e) {
if (!mGuarding) {
mLastTouchY = (int) (e.getY() + 0.5f);
final int distance = mLastTouchY - mInitialTouchY;
if (mInterceptScrollingWhileAnimationRunning && (Math.abs(distance) > mTouchSlop) && isAnimationRunning(rv)) {
// intercept vertical move touch events while animation is running
mGuarding = true;
}
}
return mGuarding;
}
private static boolean isAnimationRunning(RecyclerView rv) {
final RecyclerView.ItemAnimator itemAnimator = rv.getItemAnimator();
return (itemAnimator != null) && (itemAnimator.isRunning());
}
private void handleActionUpOrCancel() {
mGuarding = false;
mInitialTouchY = 0;
mLastTouchY = 0;
}
private void handleActionDown(MotionEvent e) {
mInitialTouchY = mLastTouchY = (int) (e.getY() + 0.5f);
mGuarding = false;
}
/**
* Sets whether to use touch guard feature. If set false, all touch event interceptions will be disabled.
*
* @param enabled enabled / disabled
*/
public void setEnabled(boolean enabled) {
if (mEnabled == enabled) {
return;
}
mEnabled = enabled;
if (!mEnabled) {
handleActionUpOrCancel();
}
}
/**
* Checks whether the touch guard feature is enabled.
*
* @return True for currently touch guard feature is enabled, otherwise false
*/
public boolean isEnabled() {
return mEnabled;
}
/**
* Sets whether to use interception of "vertical scroll while animation running".
*
* @param enabled enabled / disabled
*/
public void setInterceptVerticalScrollingWhileAnimationRunning(boolean enabled) {
mInterceptScrollingWhileAnimationRunning = enabled;
}
/**
* Checks whether the interception of "vertical scroll while animation running" is enabled.
*
* @return enabled / disabled
*/
public boolean isInterceptScrollingWhileAnimationRunning() {
return mInterceptScrollingWhileAnimationRunning;
}
}