-
Notifications
You must be signed in to change notification settings - Fork 4
/
VerticalScrollHandler.java
370 lines (333 loc) · 12 KB
/
VerticalScrollHandler.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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
package cn.colintree.aix.ScrollArrangementHandlers;
import com.google.appinventor.components.annotations.DesignerComponent;
import com.google.appinventor.components.annotations.DesignerProperty;
import com.google.appinventor.components.annotations.PropertyCategory;
import com.google.appinventor.components.annotations.SimpleEvent;
import com.google.appinventor.components.annotations.SimpleFunction;
import com.google.appinventor.components.annotations.SimpleObject;
import com.google.appinventor.components.annotations.SimpleProperty;
import com.google.appinventor.components.common.ComponentCategory;
import com.google.appinventor.components.common.PropertyTypeConstants;
import com.google.appinventor.components.runtime.AndroidNonvisibleComponent;
import com.google.appinventor.components.runtime.Component;
import com.google.appinventor.components.runtime.ComponentContainer;
import com.google.appinventor.components.runtime.EventDispatcher;
import com.google.appinventor.components.runtime.VerticalScrollArrangement;
import android.util.Log;
import android.view.View;
import android.view.ViewTreeObserver;
import android.view.MotionEvent;
import android.widget.ScrollView;
@DesignerComponent(version = VerticalScrollHandler.VERSION,
description = "by ColinTree at http://aix.colintree.cn/",
category = ComponentCategory.EXTENSION,
nonVisible = true,
iconName = "aiwebres/iconV.png")
@SimpleObject(external = true)
public class VerticalScrollHandler extends AndroidNonvisibleComponent implements Component {
public static final int VERSION = 3;
private ComponentContainer container;
private static final String LOG_TAG = "VerticalScrollHandler";
private int oldScrollY = 0;
private boolean userControl = true;
private boolean scrollBarEnabled = true;
private boolean fadingEdgeEnabled = true;
private int overScrollMode = View.OVER_SCROLL_IF_CONTENT_SCROLLS;
private ScrollView scrollView = null;
public VerticalScrollHandler(ComponentContainer container) {
super(container.$form());
this.container = container;
Log.d(LOG_TAG, LOG_TAG+" Created" );
}
/**
* <p>
* greater than 1 usually, making the px numbers smaller than dx numbers.
* which means a px contains to more than one dx
* </p>
* e.g. on my P9 plus, once i got 3 from this, and width of an button in px is 30, and dx is 90.
*/
private float deviceDensity() {
return container.$form().deviceDensity();
}
/**
* px (appinventor unit) to dx (android unit)
*/
private int px2dx(int px) {
return Math.round(px * deviceDensity());
}
/**
* dx (android unit) to px (appinventor unit)
*/
private int dx2px(int dx) {
return Math.round(dx / deviceDensity());
}
private float dx2px(float dx) {
return dx / deviceDensity();
}
@SimpleFunction
public void RegisterScrollView(VerticalScrollArrangement verticalScrollArrangement) {
scrollView = (ScrollView) verticalScrollArrangement.getView();
scrollView.getViewTreeObserver().addOnScrollChangedListener(
new ViewTreeObserver.OnScrollChangedListener() {
@Override
public void onScrollChanged() {
onScroll();
}
});
scrollView.setOnTouchListener(new View.OnTouchListener() {
private boolean touchDownDetected = false;
private int touchDownScrollY = 0;
private int touchDownPointerId;
private float touchDownPointerY;
@Override
public boolean onTouch(View v, MotionEvent event) {
int action = event.getActionMasked();
if (action == MotionEvent.ACTION_DOWN) {
onTouchDown(event);
} else if (action == MotionEvent.ACTION_MOVE) {
if (onMove(event)) {
return true;
}
} else if (action == MotionEvent.ACTION_UP || action == MotionEvent.ACTION_CANCEL) {
onTouchUp(event);
}
return !UserControl(); // => (UserControl()?false:true);
}
private void onTouchDown(MotionEvent event) {
TouchDown();
touchDownDetected = true;
touchDownScrollY = dx2px(scrollView.getScrollY());
touchDownPointerId = dx2px(event.getPointerId(0));
touchDownPointerY = dx2px(event.getY(0));
}
private boolean onMove(MotionEvent event) {
if (touchDownDetected == true) {
int currentScrollY = dx2px(scrollView.getScrollY());
float currentPointerY = dx2px(event.getY(event.findPointerIndex(touchDownPointerId)));
if (touchDownScrollY <= 0 && currentScrollY <= 0) {
return OverScrollDown(currentPointerY - touchDownPointerY);
}
int max = MaxScrollPosition();
if (touchDownScrollY >= max && currentScrollY >= max) {
return OverScrollUp(touchDownPointerY - currentPointerY);
}
} else {
onTouchDown(event);
return onMove(event);
}
return false;
}
private void onTouchUp(MotionEvent event) {
TouchUp();
touchDownDetected = false;
}
});
OverScrollMode(OverScrollMode());
ScrollBarEnabled(ScrollBarEnabled());
FadingEdgeEnabled(FadingEdgeEnabled());
}
@SimpleEvent
public void ReachTop() {
EventDispatcher.dispatchEvent(this, "ReachTop");
}
@SimpleEvent
public void ReachBottom() {
EventDispatcher.dispatchEvent(this, "ReachBottom");
}
@SimpleEvent
public void ScrollChanged(int scrollY) {
EventDispatcher.dispatchEvent(this, "ScrollChanged", scrollY);
if (scrollY == 0) {
ReachTop();
} else {
if (MaxScrollPosition() - ScrollPosition()<=0) {
ReachBottom();
}
}
}
public void onScroll() {
int scrollY = dx2px(scrollView.getScrollY());
if (scrollY<0) {
scrollY = 0;
}
if (oldScrollY != scrollY) {
ScrollChanged(scrollY);
oldScrollY = scrollY;
}
}
@SimpleEvent
public void TouchDown() {
EventDispatcher.dispatchEvent(this, "TouchDown");
}
@SimpleEvent
public void TouchUp() {
EventDispatcher.dispatchEvent(this, "TouchUp");
}
@SimpleEvent
public boolean OverScrollDown(float displacement) {
if (displacement>0) {
return EventDispatcher.dispatchEvent(this, "OverScrollDown", displacement);
}
return false;
}
@SimpleEvent
public boolean OverScrollUp(float displacement) {
if (displacement>0) {
return EventDispatcher.dispatchEvent(this, "OverScrollUp", displacement);
}
return false;
}
@SimpleProperty
public boolean UserControl() {
return userControl;
}
@SimpleProperty
@DesignerProperty(editorType = PropertyTypeConstants.PROPERTY_TYPE_BOOLEAN, defaultValue = "True")
public void UserControl(boolean enable) {
userControl = enable;
}
@SimpleProperty(category = PropertyCategory.APPEARANCE)
public boolean ScrollBarEnabled() {
return scrollBarEnabled;
}
@SimpleProperty
@DesignerProperty(editorType = PropertyTypeConstants.PROPERTY_TYPE_BOOLEAN, defaultValue = "True")
public void ScrollBarEnabled(boolean enabled) {
this.scrollBarEnabled = enabled;
if (scrollView != null) {
((ScrollView) scrollView).setVerticalScrollBarEnabled(enabled);
}
}
@SimpleProperty(category = PropertyCategory.APPEARANCE)
public boolean FadingEdgeEnabled() {
return fadingEdgeEnabled;
}
@SimpleProperty
@DesignerProperty(editorType = PropertyTypeConstants.PROPERTY_TYPE_BOOLEAN, defaultValue = "True")
public void FadingEdgeEnabled(boolean enabled) {
this.fadingEdgeEnabled = enabled;
if (scrollView != null) {
((ScrollView) scrollView).setVerticalFadingEdgeEnabled(enabled);
}
}
@SimpleProperty(category = PropertyCategory.APPEARANCE)
public int OverScrollMode() {
return overScrollMode;
}
@SimpleProperty(description = "Can be:\n "+
View.OVER_SCROLL_ALWAYS+": ALWAYS\n"+
View.OVER_SCROLL_IF_CONTENT_SCROLLS+": OVER SCROLL IF CONTENT SCROLLS\n"+
View.OVER_SCROLL_NEVER+": NEVER")
@DesignerProperty(editorType = PropertyTypeConstants.PROPERTY_TYPE_NON_NEGATIVE_INTEGER, defaultValue = "1")
public void OverScrollMode(int mode) {
if (mode != View.OVER_SCROLL_ALWAYS && mode != View.OVER_SCROLL_IF_CONTENT_SCROLLS &&
mode != View.OVER_SCROLL_NEVER) {
mode = View.OVER_SCROLL_IF_CONTENT_SCROLLS;
}
this.overScrollMode = mode;
}
@SimpleProperty(category = PropertyCategory.APPEARANCE,
description = "The scroll position is the same as the number of pixels that "
+ "are hidden from view above the scrollable area. "
+ "If the scroll bar is at the very top, or if the element is not scrollable, this number will be 0.")
public int ScrollPosition() {
int dxPosition = dx2px(scrollView.getScrollY());
if (dxPosition < 0) {
return 0;
} else if (dxPosition > MaxScrollPosition()) {
return MaxScrollPosition();
}
return dxPosition;
}
@SimpleProperty(category = PropertyCategory.APPEARANCE,
description = "Return the maximum position that the ScrollArrangement can reach")
public int MaxScrollPosition() {
View view = (View) scrollView.getChildAt(scrollView.getChildCount() - 1);
return dx2px(view.getBottom() - scrollView.getHeight());
}
@SimpleFunction
public void ScrollTop() {
if (scrollView == null) {
return;
}
scrollView.fullScroll(ScrollView.FOCUS_UP);
}
@SimpleFunction
public void ScrollBottom() {
if (scrollView == null) {
return;
}
scrollView.fullScroll(ScrollView.FOCUS_DOWN);
}
/**
* Scroll up for half screen
*/
@SimpleFunction
public void ArrowScrollUpward() {
if (scrollView == null) {
return;
}
scrollView.arrowScroll(ScrollView.FOCUS_UP);
}
/**
* Scroll down for half screen
*/
@SimpleFunction
public void ArrowScrollDownward() {
if (scrollView == null) {
return;
}
scrollView.arrowScroll(ScrollView.FOCUS_DOWN);
}
/*
* Scroll up for one page
*/
@SimpleFunction
public void PageScrollUpward() {
if (scrollView == null) {
return;
}
scrollView.pageScroll(ScrollView.FOCUS_UP);
}
/*
* Scroll down for one page
*/
@SimpleFunction
public void PageScrollDownward() {
if (scrollView == null) {
return;
}
scrollView.pageScroll(ScrollView.FOCUS_DOWN);
}
/**
* Scroll to a specific location
*/
@SimpleFunction
public void ScrollTo(int px) {
if (scrollView == null) {
return;
}
scrollView.scrollTo(0, px2dx(px));
}
@SimpleFunction
public void ScrollBy(int px) {
if (scrollView == null) {
return;
}
scrollView.scrollBy(0, px2dx(px));
}
@SimpleFunction
public void SmoothScrollTo(int px) {
if (scrollView == null) {
return;
}
scrollView.smoothScrollTo(0, px2dx(px));
}
@SimpleFunction
public void SmoothScrollBy(int px) {
if (scrollView == null) {
return;
}
scrollView.smoothScrollBy(0, px2dx(px));
}
}