/
PullRefreshContainerView.java
523 lines (478 loc) · 17.8 KB
/
PullRefreshContainerView.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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
/**
* Use this however you want, but it might not work how you'd like.
* If you fix it or make it better, share the wealth.
* If you find a problem, let it be known.
*
* https://github.com/timahoney/Android-Pull-To-Refresh
*/
package com.your.package;
import android.content.Context;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.ScrollView;
import android.widget.TextView;
/**
* A container for a ListView that can be pulled to refresh.
* This will create a ListView and refresh header automatically, but you can
* customize them by using {@link #setList(ListView)} and {@link #setRefreshHeader(View, int)}
* <p>
* To use, put this where you would normally put your ListView. Since this does not extend
* ListView, you must use {@link #getList()} to modify the list or provide your own.
* <p>
* To get the actions of the list, use a {@link OnChangeStateListener} with {@link #setOnChangeStateListener(OnChangeStateListener)}.
* If you want to change how the refresh header looks, you should do it during these state changes.
*/
public class PullRefreshContainerView extends ScrollView {
/**
* Interface for listening to when the refresh container changes state.
*/
public interface OnChangeStateListener {
/**
* Notifies a listener when the refresh view's state changes.
* @param container The container that contains the header
* @param state The state of the header. May be STATE_IDLE, STATE_READY,
* or STATE_REFRESHING.
*/
public void onChangeState(PullRefreshContainerView container, int state);
}
/**
* State of the refresh header when it is doing nothing or being pulled down slightly.
*/
public static final int STATE_IDLE = 0;
/**
* State of the refresh header when it has been pulled down enough to start refreshing, but
* has not yet been released.
*/
public static final int STATE_READY = 1;
/**
* State of the refresh header when the list should be refreshing.
*/
public static final int STATE_LOADING = 2;
private static final float DEFAULT_RESISTANCE = 1.5f;
private View mHeaderContainer;
private View mHeaderView;
private ListView mList;
private int mState;
//private float mTouchY;
private ViewGroup mContainer;
private boolean mScrollingList;
//private boolean mForceHeaderDown;
private boolean mApplyResistance;
private float mResistance;
private OnChangeStateListener mOnChangeStateListener;
//private boolean mWaitingForMove;
private float mInterceptY;
private boolean mNeedScroll;
/**
* Creates a new pull to refresh container.
*
* @param context the application context
*/
public PullRefreshContainerView(Context context) {
super(context);
init(context);
}
/**
* Creates a new pull to refresh container.
*
* @param context the application context
* @param attrs the XML attribute set
*/
public PullRefreshContainerView(Context context, AttributeSet attrs) {
super(context, attrs);
init(context);
}
/**
* Creates a new pull to refresh container.
*
* @param context the application context
* @param attrs the XML attribute set
* @param defStyle the style for this view
*/
public PullRefreshContainerView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs);
init(context);
}
private void init(Context context) {
mResistance = DEFAULT_RESISTANCE;
// Add the list and the header to the container.
mContainer = new LinearLayout(context);
mContainer.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
((LinearLayout) mContainer).setOrientation(LinearLayout.VERTICAL);
addView(mContainer);
mState = STATE_IDLE; // Start out as idle.
// We don't want to see the fading edge on the container.
setVerticalFadingEdgeEnabled(false);
setVerticalScrollBarEnabled(false);
// Set the default list and header.
TextView headerView = new TextView(context);
headerView.setText("Default refresh header.");
int headerViewId = 15134;
headerView.setId(headerViewId);
LinearLayout headerContainer = new LinearLayout(context);
headerContainer.setPadding(0, 100, 0, 0);
headerContainer.addView(headerView);
setRefreshHeader(headerContainer, headerViewId);
ListView list = new ListView(context);
setList(list);
}
@Override
public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
// We want the list to fill up the entire container.
// FIXME This does not yet work perfectly. When you first show this View, the ListView does not fill the container.
if (mList.getHeight() != getHeight()) {
mList.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, getHeight()));
mNeedScroll = true;
}
if (mContainer.getHeight() != (getHeight() + mHeaderContainer.getHeight())) {
mContainer.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, getHeight() + mHeaderContainer.getHeight()));
mNeedScroll = true;
}
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
// If we were just measured, then we probably need to scroll away from the header view.
// FIXME This doesn't actually work as planned, but it somewhat works.
// There has to be a better way.
if (mNeedScroll) {
if (getScrollY() == mHeaderContainer.getBottom()) {
mNeedScroll = false;
} else {
hideRefreshView();
}
}
}
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
float oldLastY = mInterceptY;
mInterceptY = ev.getY();
switch (ev.getAction()) {
case MotionEvent.ACTION_DOWN:
if (mList.getFirstVisiblePosition() == 0 && getScrollY() <= mHeaderContainer.getBottom()
&& (mList.getChildCount() == 0 || mList.getChildAt(0).getTop() == 0)) {
// We are already scrolled into the header.
return super.onInterceptTouchEvent(ev);
} else {
// Check in MOVE for what we want to do.
return false;
}
case MotionEvent.ACTION_MOVE:
if (mList.getFirstVisiblePosition() == 0 && getScrollY() >= mHeaderContainer.getBottom()
&& (mList.getChildCount() == 0 || mList.getChildAt(0).getTop() == 0)) {
if (mInterceptY > oldLastY) {
mScrollingList = false;
return super.onInterceptTouchEvent(ev);
} else {
mScrollingList = true;
return false;
}
} else if (mScrollingList) {
return false;
} else {
return super.onInterceptTouchEvent(ev);
}
case MotionEvent.ACTION_UP:
return !mScrollingList;
default:
return super.onInterceptTouchEvent(ev);
}
}
// TODO Remove this. Previously, touch events were handled using dispatchTouchEvent.
// This was a little more ambitious, but not as simple.
// @Override
// public boolean dispatchTouchEvent(MotionEvent ev) {
// float oldLastY = mTouchY;
// mTouchY = ev.getY();
//
// switch (ev.getAction()) {
// case MotionEvent.ACTION_DOWN:
// // All down events go to the list.
// // If the user wants to pull down, we will send a fake down event to our scroller.
// if (mState == STATE_IDLE &&
// getScrollY() > mHeaderContainer.getBottom() || mForceHeaderDown) {
// //Log.d("TOUCH DOWN HEADER");
// // If we are already viewing the header, we want touches to go to the container.
// // Also, this event may have come from a move at the border.
// // In this case, mForceHeaderDown would be true, and we want to start pulling down.
// // We dispatch this event to the container directly through onTouchEvent, otherwise
// // the event may go to the list, and we don't want that.
// mScrollingList = false;
// mForceHeaderDown = false;
// return super.onTouchEvent(ev);
// } else {
// //Log.d("TOUCH DOWN LIST");
// mScrollingList = true;
// return mList.dispatchTouchEvent(ev);
// }
//
// case MotionEvent.ACTION_MOVE:
// if (mScrollingList) {
// // Currently scrolling the list.
// // The most common situation is that we are not at the top.
// if (mState != STATE_LOADING && mList.getFirstVisiblePosition() == 0 &&
// (mList.getChildCount() == 0 || mList.getChildAt(0).getTop() == 0)
// && mTouchY > oldLastY) {
// // We are at the top and pulling down.
// // Switch from moving the list to moving the container.
// // We fire a cancel event to the list to stop anything there.
// // If we were to fire an ACTION_UP instead, then it would act as a click.
// MotionEvent listUp = MotionEvent.obtain(ev);
// listUp.setAction(MotionEvent.ACTION_CANCEL);
// mForceHeaderDown = true;
// mScrollingList = false;
// mList.dispatchTouchEvent(listUp);
// MotionEvent scrollDown = MotionEvent.obtainNoHistory(ev);
// scrollDown.setAction(MotionEvent.ACTION_DOWN);
// dispatchTouchEvent(scrollDown);
// MotionEvent newMove = MotionEvent.obtain(ev);
// //Log.d("MOVE WAS LIST");
// return super.dispatchTouchEvent(newMove);
// } else {
// // We are not showing the first child.
// // Or we are showing the first child but the first child above the top.
// // Or we are showing the first child at the top, but we are pushing up.
// // We want to scroll the list.
// //Log.d("MOVE LIST");
// return mList.dispatchTouchEvent(ev);
// }
// } else {
// // Currently not scrolling the list.
// // If we are refreshing, or if we are at the border, just
// // If the header is showing, keep scrolling the container.
// // Otherwise, determine whether we are trying to scroll the list again.
// if (getScrollY() == mHeaderContainer.getBottom() && mTouchY < oldLastY) {
// // The header is completely hidden and the user is pushing up.
// // We want to start scrolling the list again.
// MotionEvent scrollUp = MotionEvent.obtain(ev);
// scrollUp.setAction(MotionEvent.ACTION_UP);
// dispatchTouchEvent(scrollUp);
// MotionEvent listDown = MotionEvent.obtainNoHistory(ev);
// listDown.setAction(MotionEvent.ACTION_DOWN);
// mScrollingList = true;
// mList.dispatchTouchEvent(listDown);
// MotionEvent newMove = MotionEvent.obtain(ev);
// //Log.d("MOVE WAS HEADER");
// return mList.dispatchTouchEvent(newMove);
// } else {
// // We are scrolling the header. Remember to apply some resistance.
// // Also make sure we are currently in the correct state.
// mApplyResistance = true;
// if (mState == STATE_IDLE && getScrollY() <= mHeaderView.getTop()) {
// mState = STATE_READY;
// notifyStateChanged();
// } else if (mState == STATE_READY && getScrollY() > mHeaderView.getTop()) {
// mState = STATE_IDLE;
// notifyStateChanged();
// }
// //Log.d("MOVE HEADER");
// return super.dispatchTouchEvent(ev);
// }
// }
//
// case MotionEvent.ACTION_UP:
// case MotionEvent.ACTION_CANCEL:
// mList.setVerticalScrollBarEnabled(true);
// if (mScrollingList) {
// //Log.d("UP LIST");
// // We were scrolling the list, now we stopped.
// mScrollingList = false;
// hideRefreshView(); // Make sure the refresh view is hidden.
// return mList.dispatchTouchEvent(ev);
// } else {
// // The user lifted their finger up while scrolling the header.
// // If the user is not refreshing and they pulled past the header,
// // then we want to refresh.
// if (mState == STATE_READY) {
// showHeader();
// refresh();
// } else {
// hideRefreshView();
// }
//
// //Log.d("UP HEADER");
// return super.dispatchTouchEvent(ev);
// }
//
// default:
// //Log.d("DEFAULT");
// return super.dispatchTouchEvent(ev);
// }
// }
// @Override
// public void fling(int velocityY) {
// // Do nothing.
// }
@Override
public boolean onTouchEvent(MotionEvent ev) {
switch (ev.getAction()) {
case MotionEvent.ACTION_MOVE:
mApplyResistance = true;
if (mState == STATE_IDLE && getScrollY() <= mHeaderView.getTop()) {
mState = STATE_READY;
notifyStateChanged();
} else if (mState == STATE_READY && getScrollY() > mHeaderView.getTop()) {
mState = STATE_IDLE;
notifyStateChanged();
}
return super.onTouchEvent(ev);
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_CANCEL:
// We override this so we stop any flinging of the scroller.
// Usually, an ACTION_UP event in a scroll view would result in some extra scrolling
// because the user wants to fling through the scroller. However, in our case, we
// want to handle all touch up events.
// We also want to hide the refresh view.
if (mState == STATE_READY) {
showHeader();
refresh();
} else {
hideRefreshView();
}
return true;
default:
return super.onTouchEvent(ev);
}
}
/**
* Sets the list to be used in this pull to refresh container.
* @param list the list to use
*/
public void setList(ListView list) {
if (mList != null) {
mContainer.removeView(mList);
}
mList = list;
if (mList.getParent() != null) {
ViewGroup parent = (ViewGroup) mList.getParent();
parent.removeView(mList);
}
mList.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, getHeight()));
mContainer.addView(mList, 1);
}
/**
* Sets a resistance for when the user is pulling down.
*
* @param resistance The pulling resistance. The default is 1.5.
* The higher this value, the more resistance the user will feel when pulling.
*/
public void setResistance(float resistance) {
mResistance = resistance;
}
/**
* @return the list inside this pull to refresh container
*/
public ListView getList() {
return mList;
}
/**
* Sets the view to use as the refresh header.
* <p />
* Two views are involved, the header view and
* the header container. The header view is the view at the top that will show while the list
* is refreshing. Usually, this will be a simple rectangle that says "refreshing" and the like.
* <p />
* The header container is the entire view that will be shown while the user is pulling down.
* Usually, the header container is a simple wrapper for the header view that has some padding
* at the top. That way, the user can pull down a little further than the header view.
* <p />
* If the header view is the header container, then there will be no padding at the top,
* and the user will need to scroll to the very top in order to refresh.
*
* @param headerContainer the view to use as the whole header container.
* @param headerView the ID of the view inside this container that is the header that should should while loading
*/
public void setRefreshHeader(View headerContainer, int headerId) {
if (mHeaderContainer != null) {
mContainer.removeView(mHeaderContainer);
}
if (headerContainer == null) {
throw new RuntimeException("Please supply a non-null header container.");
}
else if (headerContainer.getId() != headerId) {
// The header container is not the same as the header view.
// Make sure the container contains the header view.
if (!(headerContainer instanceof ViewGroup)) {
throw new RuntimeException(
"The header container view supplied is not a container. " +
"Please supply a header container that is a subclass of ViewGroup. " +
"Or supply a header view ID that is the same as the container's.");
} else {
mHeaderContainer = headerContainer;
mHeaderView = ((ViewGroup) mHeaderContainer).findViewById(headerId);
if (mHeaderView == null) {
throw new RuntimeException("The header view ID supplied was not part of " +
"the supplied header container.");
}
}
} else {
mHeaderView = mHeaderContainer = headerContainer;
}
mContainer.addView(mHeaderContainer, 0);
requestLayout();
}
public void refresh() {
mState = STATE_LOADING;
showHeader();
notifyStateChanged();
}
/**
* Notifies the pull-to-refresh view that the refreshing is complete.
* This will hide the refreshing header.
*/
public void completeRefresh() {
mState = STATE_IDLE;
hideRefreshView();
notifyStateChanged();
}
/**
* Notifies the listener that the state has changed.
*/
private void notifyStateChanged() {
if (mOnChangeStateListener != null) {
mOnChangeStateListener.onChangeState(this, mState);
}
}
/**
* Hides the header at the top if it is visible.
*/
protected void hideRefreshView() {
smoothScrollTo(0, mHeaderContainer.getBottom());
}
/**
* Scrolls to the top of the header.
*/
protected void showHeader() {
smoothScrollTo(0, mHeaderView.getTop());
}
@Override
protected void onScrollChanged(int l, int t, int oldl, int oldt) {
super.onScrollChanged(l, t, oldl, oldt);
if (mApplyResistance) {
// Simulate resistance on the pull down.
// We have to round the result because otherwise we would never reach the end.
// Vary the resistance depending on how close we are to the top.
// We need to set the mApplyResistance to false before scrolling again, otherwise
// we would get an overflow of recursive calls.
mApplyResistance = false;
int changeY = t - oldt;
int headerBottom = mHeaderContainer.getBottom();
float resistance = (mResistance * (headerBottom - t) / headerBottom) + 1;
int newChangeY = (int) (changeY / (resistance * resistance));
int newt = oldt + newChangeY;
scrollTo(0, newt);
}
}
/**
* @param listener the listener to be notified when the header state should change
*/
public void setOnChangeStateListener(OnChangeStateListener listener) {
mOnChangeStateListener = listener;
}
}