/
WizardView.java
312 lines (277 loc) · 8.88 KB
/
WizardView.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
package com.matburt.mobileorg.Gui.Wizard;
import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.util.AttributeSet;
import android.util.Log;
import android.view.GestureDetector;
import android.view.GestureDetector.SimpleOnGestureListener;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.HorizontalScrollView;
import com.matburt.mobileorg.R;
public class WizardView extends HorizontalScrollView implements
View.OnTouchListener {
private static final String TAG = "PageFlipView";
private static final int SWIPE_THRESHOLD_VELOCITY = 50;
public static final int FIRST_PAGE = 0;
public static final int MIDDLE_PAGE = 1;
public static final int LAST_PAGE = 2;
private GestureDetector mGestureDetector;
private WideLinearLayout container;
private NextPageButtonListener nextPageButtonListener;
private PreviousPageButtonListener previousPageButtonListener;
private int maxScrollX;
private int maxEnabledPage = -1;
private int currentPage = 0;
public WizardView(Context context) {
super(context);
}
public WizardView(Context context, AttributeSet attrs) {
super(context, attrs);
nextPageButtonListener = new NextPageButtonListener();
previousPageButtonListener = new PreviousPageButtonListener();
// see
// http://blog.velir.com/index.php/2010/11/17/android-snapping-horizontal-scroll/
// setup page swiping
mGestureDetector = new GestureDetector(getContext(),
new PageSwipeDetector());
setOnTouchListener(this);
}
// Make the child views the same size as the screen
@Override
protected void onMeasure(int w, int h) {
int width = MeasureSpec.getSize(w);
// Tell screen width to our only child
container.setWidth(width);
// default to w and h given in XML
super.onMeasure(w, h);
}
@Override
public void onFinishInflate() {
container = (WideLinearLayout) findViewById(R.id.wizard_container);
}
private static void setButtonState(Button b, boolean state,
View.OnClickListener e) {
b.setOnClickListener(e);
b.setEnabled(state);
}
public void enableNextButton(Button b) {
b.setOnClickListener(nextPageButtonListener);
b.setEnabled(true);
}
// starting from given page, disable all next buttons, and disable
// swiping to the right.
public void disableAllNextActions(int page) {
// first disable next buttons
if (container != null)
for (int i = page; i < container.getChildCount(); i++) {
// get the pageview container
View pageContainer = (View) container.getChildAt(i);
// last page doesn't have a next button
View nextButton = pageContainer
.findViewById(R.id.wizard_next_button);
if (nextButton != null)
nextButton.setEnabled(false);
}
// user can't scroll past current page
maxScrollX = page * getMeasuredWidth();
}
// disable/enable all buttons for given page
public void setNavButtonStateOnPage(int page, boolean state, int page_type) {
// get the pageview container
View pageContainer = (View) container.getChildAt(page);
Button prevButton = (Button) pageContainer
.findViewById(R.id.wizard_previous_button);
Button nextButton = (Button) pageContainer
.findViewById(R.id.wizard_next_button);
switch (page_type) {
case FIRST_PAGE:
setButtonState(nextButton, state, nextPageButtonListener);
break;
case MIDDLE_PAGE:
setButtonState(prevButton, state, previousPageButtonListener);
setButtonState(nextButton, state, nextPageButtonListener);
break;
case LAST_PAGE:
setButtonState(prevButton, state, previousPageButtonListener);
// Button done=(Button)
// pageContainer.findViewById(R.id.wizard_done_button);
// done.setEnabled(false);
break;
}
}
// enable prev/next buttons for give page and allow scrolling
// to next page
public void enablePage(int page) {
if (page == -1) {
maxScrollX = 0;
maxEnabledPage = -1;
return;
}
// figure out page type
int page_type = MIDDLE_PAGE;
if (page == getNumberOfPages() - 1)
page_type = LAST_PAGE;
else if (page == 0)
page_type = 0;
setNavButtonStateOnPage(page, true, page_type);
maxScrollX = (page + 1) * getMeasuredWidth();
maxEnabledPage = (page > maxEnabledPage) ? page : maxEnabledPage;
}
public int getNumberOfPages() {
return container.getChildCount();
}
public int getCurrentPage() {
return currentPage;
}
public void setCurrentPage(int i) {
currentPage = i;
}
// used for orientation change
public void restoreLastPage() {
SharedPreferences prefs = ((Activity) getContext()).getPreferences(0);
currentPage = prefs.getInt("currentPage", 0);
maxEnabledPage = prefs.getInt("maxEnabledPage", -1);
// scroll to last loaded page
post(new Runnable() {
@Override
public void run() {
enablePage(maxEnabledPage);
scrollTo(currentPage * getMeasuredWidth(), 0);
}
});
}
// ditto
public void saveCurrentPage() {
SharedPreferences prefs = ((Activity) getContext()).getPreferences(0);
SharedPreferences.Editor editor = prefs.edit();
// save current page
editor.putInt("currentPage", getCurrentPage());
editor.putInt("maxEnabledPage", maxEnabledPage);
editor.commit();
}
// adds a new page with the given resource id
public void addPage(int resId) {
View v = LayoutInflater.from(getContext()).inflate(resId, null);
container.addView(v);
}
public void addPage(View view) {
container.addView(view);
}
// delete all pages after and including given page
public void removePagesAfter(int page) {
for (int i = container.getChildCount() - 1; i >= page; i--)
container.removeViewAt(i);
}
// START OF CODE TO HANDLE SWIPING/SCROLLING
// if scroll more than %25 of screen width, then automagically
// scroll to next page
@Override
public boolean onTouch(View v, MotionEvent event) {
boolean actionUp = false;
// If the user swipes
if (mGestureDetector.onTouchEvent(event))
return true;
// else
switch (event.getAction()) {
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_POINTER_UP:
case MotionEvent.ACTION_CANCEL:
int scrollX = getScrollX();
int featureWidth = getMeasuredWidth();
// if scroll more than %25 of width, then go to next page
currentPage = ((scrollX + (featureWidth / 4 * 3)) / featureWidth);
Log.d(TAG, "scrollX: " + scrollX + " featureWidth: "
+ (featureWidth / 4 * 3) + " page: " + currentPage);
int scrollTo = currentPage * featureWidth;
smoothScrollTo(scrollTo, 0);
// if you don't return true, a child view will interfere
// with the scrolling
actionUp = true;
break;
default:
break;
}
return actionUp;
}
// Basically, keep HorizontalScrollView from scrolling past current
// page, until it's OK to do so (ex: user puts password)
@Override
public void onScrollChanged(int l, int t, int oldl, int oldt) {
// Log.d(TAG,"scroll: "+l+", "+oldl+" maxscroll: "+maxScrollX);
if (l > maxScrollX) {
// if trying to scroll past maximum allowed, then snap back
scrollTo(oldl, 0);
// recalculate current page if necessary
currentPage = (currentPage * getMeasuredWidth() == maxScrollX) ? --currentPage
: currentPage;
} else
super.onScrollChanged(l, t, oldl, oldt);
}
class NextPageButtonListener implements View.OnClickListener {
@Override
public void onClick(View v) {
scrollRight();
}
}
class PreviousPageButtonListener implements View.OnClickListener {
@Override
public void onClick(View v) {
scrollLeft();
}
}
// hide keyboard if showing
private void hideKeyboard() {
InputMethodManager imm = (InputMethodManager) ((Activity) getContext())
.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(getWindowToken(), 0);
}
private void scrollRight() {
hideKeyboard();
// unfocus login boxes
View selectedBox = findFocus();
if (selectedBox != null)
selectedBox.clearFocus();
// scroll
int featureWidth = getMeasuredWidth();
currentPage = (currentPage < (container.getChildCount() - 1)) ? currentPage + 1
: container.getChildCount() - 1;
smoothScrollTo(currentPage * featureWidth, 0);
}
private void scrollLeft() {
hideKeyboard();
// unfocus login boxes
View selectedBox = findFocus();
if (selectedBox != null)
selectedBox.clearFocus();
// scroll
int featureWidth = getMeasuredWidth();
currentPage = (currentPage > 0) ? currentPage - 1 : 0;
smoothScrollTo(currentPage * featureWidth, 0);
}
private class PageSwipeDetector extends SimpleOnGestureListener {
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY) {
if (velocityX < 0 && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
scrollRight();
return true;
}
// left to right
else if (velocityX > 0
&& Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
scrollLeft();
return true;
}
return false;
}
@Override
public boolean onDown(MotionEvent e) {
return true;
}
}
}