This repository has been archived by the owner on Dec 19, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathColoredScrollView.js
138 lines (128 loc) · 5.05 KB
/
ColoredScrollView.js
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
/**
* @file ModPE widget library for providing colored scroll view.
* @author Astro <[email protected]>
* @version 1.0
* @license Apache-2.0
*/
/** @namespace global */
($ => {
"use strict";
const ColorDrawable_ = android.graphics.drawable.ColorDrawable,
MotionEvent_ = android.view.MotionEvent,
View_ = android.view.View,
LinearLayout_ = android.widget.LinearLayout,
ScrollView_ = android.widget.ScrollView,
TextView_ = android.widget.TextView,
MainActivity_ = com.mojang.minecraftpe.MainActivity,
CONTEXT = MainActivity_.currentMainActivity.get(),
DP = CONTEXT.getResources().getDisplayMetrics().density;
/**
* Class representing a scroll view.
* @since 2016-09-06
* @class
* @memberOf global
*/
function ColoredScrollView() {
let thiz = this;
this._scrollView = new ScrollView_(CONTEXT);
this._scroller = new LinearLayout_(CONTEXT);
this._track = new TextView_(CONTEXT);
this._thumb = new TextView_(CONTEXT);
this._width = DP * 340;
this._scrollView.setLayoutParams(new LinearLayout_.LayoutParams(this._width - DP * 6, -1));
this._scrollView.setOnScrollChangeListener(new View_.OnScrollChangeListener({
onScrollChange(v, scrollX, scrollY, oldScrollX, oldScrollY) {
CONTEXT.runOnUiThread({
run() {
thiz._track.setLayoutParams(new LinearLayout_.LayoutParams(DP * 12, v.getHeight() * scrollY / v.getChildAt(0).getHeight()));
}
});
}
}));
this._scrollView.setOverScrollMode(View_.OVER_SCROLL_NEVER);
this._scrollView.setVerticalScrollBarEnabled(false);
this._scrollView.setHorizontalScrollBarEnabled(false);
this._scroller.addView(this._track);
this._scroller.addView(this._thumb);
this._scroller.setOrientation(1);
this._track.setLayoutParams(new LinearLayout_.LayoutParams(DP * 6, 0));
this._thumb.setBackgroundDrawable(new ColorDrawable_(-14575885));
this._thumb.setLayoutParams(new LinearLayout_.LayoutParams(DP * 6, DP * 36));
this._thumb.setOnTouchListener(new View_.OnTouchListener({
onTouch(v, event) {
let y = event.getRawY();
if (event.getAction() === MotionEvent_.ACTION_MOVE) {
thiz._track.setLayoutParams(new LinearLayout_.LayoutParams(DP * 12, thiz._scrollView.getChildAt(0).getHeight() * y / (thiz._scrollView.getHeight() - DP * 18)));
thiz._scrollView.scrollTo(0, thiz._scrollView.getChildAt(0).getHeight() * y / (thiz._scrollView.getHeight() - DP * 18));
}
return true;
}
}));
}
/**
* Adds a view on the scroll view.
* @since 2016-09-06
* @param {android.view.View} view View
*/
ColoredScrollView.prototype.addView = function (view) {
this._scrollView.addView(view);
return this;
};
/**
* Returns the scroll view.
* @since 2016-09-06
* @returns {android.widget.LinearLayout} Scroll view
*/
ColoredScrollView.prototype.getLayout = function () {
return this._layout;
};
/**
* Sets the color of the thumb of the scroll view.
* @since 2016-09-06
* @param {Number} color Color of the thumb of the scroll view
*/
ColoredScrollView.prototype.setThumbColor = function (color) {
this._thumb.setBackgroundDrawable(new ColorDrawable_(color));
return this;
};
/**
* Sets the drawable of the thumb of the scroll view.
* @since 2016-09-06
* @param {android.graphics.drawable.Drawable} drawable Drawable of the thumb of the scroll view
*/
ColoredScrollView.prototype.setThumbDrawable = function (drawable) {
this._thumb.setBackgroundDrawable(drawable);
return this;
};
/**
* Sets the color of the track of the scroll view.
* @since 2016-09-06
* @param {Number} color Color of the track of the scroll view
*/
ColoredScrollView.prototype.setTrackColor = function (color) {
this._track.setBackgroundDrawable(new ColorDrawable_(color));
return this;
};
/**
* Sets the drawable of the track of the scroll view.
* @since 2016-09-06
* @param {android.graphics.drawable.Drawable} drawable Drawable of the track of the scroll view
*/
ColoredScrollView.prototype.setTrackDrawable = function (drawable) {
this._track.setBackgroundDrawable(drawable);
return this;
};
/**
* Returns the scroll view.
* @since 2016-09-06
* @returns {android.widget.LinearLayout} Scroll view
*/
ColoredScrollView.prototype.show = function () {
let mainLayout = new LinearLayout_(CONTEXT);
mainLayout.addView(this._scrollView);
mainLayout.addView(this._scroller);
mainLayout.setLayoutParams(new LinearLayout_.LayoutParams(this._width, -1));
return mainLayout;
};
$.ColoredScrollView = ColoredScrollView;
})(this);