-
Notifications
You must be signed in to change notification settings - Fork 444
/
Copy pathSwipeToLoadLayout.java
executable file
·1771 lines (1533 loc) · 60.9 KB
/
SwipeToLoadLayout.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
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package com.aspsine.swipetoloadlayout;
import android.content.Context;
import android.content.res.TypedArray;
import android.support.v4.view.MotionEventCompat;
import android.support.v4.view.ViewCompat;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewConfiguration;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.Scroller;
/**
* Created by Aspsine on 2015/8/13.
*/
public class SwipeToLoadLayout extends ViewGroup {
private static final String TAG = SwipeToLoadLayout.class.getSimpleName();
private static final int DEFAULT_SWIPING_TO_REFRESH_TO_DEFAULT_SCROLLING_DURATION = 200;
private static final int DEFAULT_RELEASE_TO_REFRESHING_SCROLLING_DURATION = 200;
private static final int DEFAULT_REFRESH_COMPLETE_DELAY_DURATION = 300;
private static final int DEFAULT_REFRESH_COMPLETE_TO_DEFAULT_SCROLLING_DURATION = 500;
private static final int DEFAULT_DEFAULT_TO_REFRESHING_SCROLLING_DURATION = 500;
private static final int DEFAULT_SWIPING_TO_LOAD_MORE_TO_DEFAULT_SCROLLING_DURATION = 200;
private static final int DEFAULT_RELEASE_TO_LOADING_MORE_SCROLLING_DURATION = 200;
private static final int DEFAULT_LOAD_MORE_COMPLETE_DELAY_DURATION = 300;
private static final int DEFAULT_LOAD_MORE_COMPLETE_TO_DEFAULT_SCROLLING_DURATION = 300;
private static final int DEFAULT_DEFAULT_TO_LOADING_MORE_SCROLLING_DURATION = 300;
/**
* how hard to drag
*/
private static final float DEFAULT_DRAG_RATIO = 0.5f;
private static final int INVALID_POINTER = -1;
private static final int INVALID_COORDINATE = -1;
private AutoScroller mAutoScroller;
private OnRefreshListener mRefreshListener;
private OnLoadMoreListener mLoadMoreListener;
private View mHeaderView;
private View mTargetView;
private View mTargetViewContainer;
private View mFooterView;
private int mHeaderHeight;
private int mFooterHeight;
private boolean mHasHeaderView;
private boolean mHasFooterView;
/**
* indicate whether in debug mode
*/
private boolean mDebug;
private float mDragRatio = DEFAULT_DRAG_RATIO;
private boolean mAutoLoading;
/**
* the threshold of the touch event
*/
private final int mTouchSlop;
/**
* status of SwipeToLoadLayout
*/
private int mStatus = STATUS.STATUS_DEFAULT;
/**
* target view top offset
*/
private int mHeaderOffset;
/**
* target offset
*/
private int mTargetOffset;
/**
* target view bottom offset
*/
private int mFooterOffset;
/**
* init touch action down point.y
*/
private float mInitDownY;
/**
* init touch action down point.x
*/
private float mInitDownX;
/**
* last touch point.y
*/
private float mLastY;
/**
* last touch point.x
*/
private float mLastX;
/**
* action touch pointer's id
*/
private int mActivePointerId;
/**
* <b>ATTRIBUTE:</b>
* a switcher indicate whither refresh function is enabled
*/
private boolean mRefreshEnabled = true;
/**
* <b>ATTRIBUTE:</b>
* a switcher indicate whiter load more function is enabled
*/
private boolean mLoadMoreEnabled = true;
/**
* a switcher whither hiding headerView when refreshing
*/
private boolean isHideHeaderWhenRefreshing = true;
/**
* a switcher whither hiding footerView when loadingmore
*/
private boolean isHideFooterWhenLoadingMore = true;
/**
* <b>ATTRIBUTE:</b>
* the style default classic
*/
private int mStyle = STYLE.CLASSIC;
/**
* <b>ATTRIBUTE:</b>
* offset to trigger refresh
*/
private float mRefreshTriggerOffset;
/**
* <b>ATTRIBUTE:</b>
* offset to trigger load more
*/
private float mLoadMoreTriggerOffset;
/**
* <b>ATTRIBUTE:</b>
* the max value of top offset
*/
private float mRefreshFinalDragOffset;
/**
* <b>ATTRIBUTE:</b>
* the max value of bottom offset
*/
private float mLoadMoreFinalDragOffset;
/**
* <b>ATTRIBUTE:</b>
* Scrolling duration swiping to refresh -> default
*/
private int mSwipingToRefreshToDefaultScrollingDuration = DEFAULT_SWIPING_TO_REFRESH_TO_DEFAULT_SCROLLING_DURATION;
/**
* <b>ATTRIBUTE:</b>
* Scrolling duration status release to refresh -> refreshing
*/
private int mReleaseToRefreshToRefreshingScrollingDuration = DEFAULT_RELEASE_TO_REFRESHING_SCROLLING_DURATION;
/**
* <b>ATTRIBUTE:</b>
* Refresh complete delay duration
*/
private int mRefreshCompleteDelayDuration = DEFAULT_REFRESH_COMPLETE_DELAY_DURATION;
/**
* <b>ATTRIBUTE:</b>
* Scrolling duration status refresh complete -> default
* {@link #setRefreshing(boolean)} false
*/
private int mRefreshCompleteToDefaultScrollingDuration = DEFAULT_REFRESH_COMPLETE_TO_DEFAULT_SCROLLING_DURATION;
/**
* <b>ATTRIBUTE:</b>
* Scrolling duration status default -> refreshing, mainly for auto refresh
* {@link #setRefreshing(boolean)} true
*/
private int mDefaultToRefreshingScrollingDuration = DEFAULT_DEFAULT_TO_REFRESHING_SCROLLING_DURATION;
/**
* <b>ATTRIBUTE:</b>
* Scrolling duration status release to loading more -> loading more
*/
private int mReleaseToLoadMoreToLoadingMoreScrollingDuration = DEFAULT_RELEASE_TO_LOADING_MORE_SCROLLING_DURATION;
/**
* <b>ATTRIBUTE:</b>
* Load more complete delay duration
*/
private int mLoadMoreCompleteDelayDuration = DEFAULT_LOAD_MORE_COMPLETE_DELAY_DURATION;
/**
* <b>ATTRIBUTE:</b>
* Scrolling duration status load more complete -> default
* {@link #setLoadingMore(boolean)} false
*/
private int mLoadMoreCompleteToDefaultScrollingDuration = DEFAULT_LOAD_MORE_COMPLETE_TO_DEFAULT_SCROLLING_DURATION;
/**
* <b>ATTRIBUTE:</b>
* Scrolling duration swiping to load more -> default
*/
private int mSwipingToLoadMoreToDefaultScrollingDuration = DEFAULT_SWIPING_TO_LOAD_MORE_TO_DEFAULT_SCROLLING_DURATION;
/**
* <b>ATTRIBUTE:</b>
* Scrolling duration status default -> loading more, mainly for auto load more
* {@link #setLoadingMore(boolean)} true
*/
private int mDefaultToLoadingMoreScrollingDuration = DEFAULT_DEFAULT_TO_LOADING_MORE_SCROLLING_DURATION;
/**
* the style enum
*/
public static final class STYLE {
public static final int CLASSIC = 0;
public static final int ABOVE = 1;
public static final int BLEW = 2;
public static final int SCALE = 3;
}
public SwipeToLoadLayout(Context context) {
this(context, null);
}
public SwipeToLoadLayout(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public SwipeToLoadLayout(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
final TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.SwipeToLoadLayout, defStyleAttr, 0);
try {
final int N = a.getIndexCount();
for (int i = 0; i < N; i++) {
int attr = a.getIndex(i);
if (attr == R.styleable.SwipeToLoadLayout_refresh_enabled) {
setRefreshEnabled(a.getBoolean(attr, true));
} else if (attr == R.styleable.SwipeToLoadLayout_load_more_enabled) {
setLoadMoreEnabled(a.getBoolean(attr, true));
} else if (attr == R.styleable.SwipeToLoadLayout_swipe_style) {
setSwipeStyle(a.getInt(attr, STYLE.CLASSIC));
} else if (attr == R.styleable.SwipeToLoadLayout_drag_ratio) {
setDragRatio(a.getFloat(attr, DEFAULT_DRAG_RATIO));
} else if (attr == R.styleable.SwipeToLoadLayout_refresh_final_drag_offset) {
setRefreshFinalDragOffset(a.getDimensionPixelOffset(attr, 0));
} else if (attr == R.styleable.SwipeToLoadLayout_load_more_final_drag_offset) {
setLoadMoreFinalDragOffset(a.getDimensionPixelOffset(attr, 0));
} else if (attr == R.styleable.SwipeToLoadLayout_refresh_trigger_offset) {
setRefreshTriggerOffset(a.getDimensionPixelOffset(attr, 0));
} else if (attr == R.styleable.SwipeToLoadLayout_load_more_trigger_offset) {
setLoadMoreTriggerOffset(a.getDimensionPixelOffset(attr, 0));
} else if (attr == R.styleable.SwipeToLoadLayout_swiping_to_refresh_to_default_scrolling_duration) {
setSwipingToRefreshToDefaultScrollingDuration(a.getInt(attr, DEFAULT_SWIPING_TO_REFRESH_TO_DEFAULT_SCROLLING_DURATION));
} else if (attr == R.styleable.SwipeToLoadLayout_release_to_refreshing_scrolling_duration) {
setReleaseToRefreshingScrollingDuration(a.getInt(attr, DEFAULT_RELEASE_TO_REFRESHING_SCROLLING_DURATION));
} else if (attr == R.styleable.SwipeToLoadLayout_refresh_complete_delay_duration) {
setRefreshCompleteDelayDuration(a.getInt(attr, DEFAULT_REFRESH_COMPLETE_DELAY_DURATION));
} else if (attr == R.styleable.SwipeToLoadLayout_refresh_complete_to_default_scrolling_duration) {
setRefreshCompleteToDefaultScrollingDuration(a.getInt(attr, DEFAULT_REFRESH_COMPLETE_TO_DEFAULT_SCROLLING_DURATION));
} else if (attr == R.styleable.SwipeToLoadLayout_default_to_refreshing_scrolling_duration) {
setDefaultToRefreshingScrollingDuration(a.getInt(attr, DEFAULT_DEFAULT_TO_REFRESHING_SCROLLING_DURATION));
} else if (attr == R.styleable.SwipeToLoadLayout_swiping_to_load_more_to_default_scrolling_duration) {
setSwipingToLoadMoreToDefaultScrollingDuration(a.getInt(attr, DEFAULT_SWIPING_TO_LOAD_MORE_TO_DEFAULT_SCROLLING_DURATION));
} else if (attr == R.styleable.SwipeToLoadLayout_release_to_loading_more_scrolling_duration) {
setReleaseToLoadingMoreScrollingDuration(a.getInt(attr, DEFAULT_RELEASE_TO_LOADING_MORE_SCROLLING_DURATION));
} else if (attr == R.styleable.SwipeToLoadLayout_load_more_complete_delay_duration) {
setLoadMoreCompleteDelayDuration(a.getInt(attr, DEFAULT_LOAD_MORE_COMPLETE_DELAY_DURATION));
} else if (attr == R.styleable.SwipeToLoadLayout_load_more_complete_to_default_scrolling_duration) {
setLoadMoreCompleteToDefaultScrollingDuration(a.getInt(attr, DEFAULT_LOAD_MORE_COMPLETE_TO_DEFAULT_SCROLLING_DURATION));
} else if (attr == R.styleable.SwipeToLoadLayout_default_to_loading_more_scrolling_duration) {
setDefaultToLoadingMoreScrollingDuration(a.getInt(attr, DEFAULT_DEFAULT_TO_LOADING_MORE_SCROLLING_DURATION));
}
}
} finally {
a.recycle();
}
mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
mAutoScroller = new AutoScroller();
}
@Override
protected void onFinishInflate() {
super.onFinishInflate();
final int childNum = getChildCount();
if (childNum == 0) {
// no child return
return;
} else if (0 < childNum && childNum < 4) {
mHeaderView = findViewById(R.id.swipe_refresh_header);
mTargetView = findViewById(R.id.swipe_target);
try {
mTargetViewContainer = (View) mTargetView.getParent();
if(mTargetViewContainer instanceof SwipeToLoadLayout) {
mTargetViewContainer = null;
}else{
while(mTargetViewContainer.getParent() != null){
if(mTargetViewContainer.getParent() instanceof SwipeToLoadLayout){
break;
}
mTargetViewContainer = (View) mTargetViewContainer.getParent();
}
}
} catch (Exception e) {
e.printStackTrace();
mTargetViewContainer = null;
}
mFooterView = findViewById(R.id.swipe_load_more_footer);
} else {
// more than three children: unsupported!
throw new IllegalStateException("Children num must equal or less than 3");
}
if (mTargetView == null) {
return;
}
if (mHeaderView != null && mHeaderView instanceof SwipeTrigger) {
mHeaderView.setVisibility(GONE);
}
if (mFooterView != null && mFooterView instanceof SwipeTrigger) {
mFooterView.setVisibility(GONE);
}
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
// header
if (mHeaderView != null) {
final View headerView = mHeaderView;
measureChildWithMargins(headerView, widthMeasureSpec, 0, heightMeasureSpec, 0);
MarginLayoutParams lp = ((MarginLayoutParams) headerView.getLayoutParams());
mHeaderHeight = headerView.getMeasuredHeight() + lp.topMargin + lp.bottomMargin;
if (mRefreshTriggerOffset < mHeaderHeight) {
mRefreshTriggerOffset = mHeaderHeight;
}
}
// target
if (mTargetView != null) {
final View targetView = mTargetViewContainer == null ? mTargetView : mTargetViewContainer;
measureChildWithMargins(targetView, widthMeasureSpec, 0, heightMeasureSpec, 0);
}
// footer
if (mFooterView != null) {
final View footerView = mFooterView;
measureChildWithMargins(footerView, widthMeasureSpec, 0, heightMeasureSpec, 0);
MarginLayoutParams lp = ((MarginLayoutParams) footerView.getLayoutParams());
mFooterHeight = footerView.getMeasuredHeight() + lp.topMargin + lp.bottomMargin;
if (mLoadMoreTriggerOffset < mFooterHeight) {
mLoadMoreTriggerOffset = mFooterHeight;
}
}
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
layoutChildren();
mHasHeaderView = (mHeaderView != null);
mHasFooterView = (mFooterView != null);
}
/**
* TODO add gravity
* LayoutParams of RefreshLoadMoreLayout
*/
public static class LayoutParams extends MarginLayoutParams {
public LayoutParams(Context c, AttributeSet attrs) {
super(c, attrs);
}
public LayoutParams(int width, int height) {
super(width, height);
}
public LayoutParams(MarginLayoutParams source) {
super(source);
}
public LayoutParams(ViewGroup.LayoutParams source) {
super(source);
}
}
/**
* {@inheritDoc}
*/
@Override
protected ViewGroup.LayoutParams generateDefaultLayoutParams() {
return new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
}
/**
* {@inheritDoc}
*/
@Override
protected ViewGroup.LayoutParams generateLayoutParams(ViewGroup.LayoutParams p) {
return new LayoutParams(p);
}
/**
* {@inheritDoc}
*/
@Override
public ViewGroup.LayoutParams generateLayoutParams(AttributeSet attrs) {
return new LayoutParams(getContext(), attrs);
}
@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
final int action = MotionEventCompat.getActionMasked(ev);
switch (action) {
case MotionEvent.ACTION_CANCEL:
case MotionEvent.ACTION_UP:
// swipeToRefresh -> finger up -> finger down if the status is still swipeToRefresh
// in onInterceptTouchEvent ACTION_DOWN event will stop the scroller
// if the event pass to the child view while ACTION_MOVE(condition is false)
// in onInterceptTouchEvent ACTION_MOVE the ACTION_UP or ACTION_CANCEL will not be
// passed to onInterceptTouchEvent and onTouchEvent. Instead It will be passed to
// child view's onTouchEvent. So we must deal this situation in dispatchTouchEvent
onActivePointerUp();
break;
}
return super.dispatchTouchEvent(ev);
}
@Override
public boolean onInterceptTouchEvent(MotionEvent event) {
final int action = MotionEventCompat.getActionMasked(event);
switch (action) {
case MotionEvent.ACTION_DOWN:
mActivePointerId = MotionEventCompat.getPointerId(event, 0);
mInitDownY = mLastY = getMotionEventY(event, mActivePointerId);
mInitDownX = mLastX = getMotionEventX(event, mActivePointerId);
// if it isn't an ing status or default status
if (STATUS.isSwipingToRefresh(mStatus) || STATUS.isSwipingToLoadMore(mStatus) ||
STATUS.isReleaseToRefresh(mStatus) || STATUS.isReleaseToLoadMore(mStatus)) {
// abort autoScrolling, not trigger the method #autoScrollFinished()
mAutoScroller.abortIfRunning();
if (mDebug) {
Log.i(TAG, "Another finger down, abort auto scrolling, let the new finger handle");
}
}
if (STATUS.isSwipingToRefresh(mStatus) || STATUS.isReleaseToRefresh(mStatus)
|| STATUS.isSwipingToLoadMore(mStatus) || STATUS.isReleaseToLoadMore(mStatus)) {
return true;
}
// let children view handle the ACTION_DOWN;
// 1. children consumed:
// if at least one of children onTouchEvent() ACTION_DOWN return true.
// ACTION_DOWN event will not return to SwipeToLoadLayout#onTouchEvent().
// but the others action can be handled by SwipeToLoadLayout#onInterceptTouchEvent()
// 2. children not consumed:
// if children onTouchEvent() ACTION_DOWN return false.
// ACTION_DOWN event will return to SwipeToLoadLayout's onTouchEvent().
// SwipeToLoadLayout#onTouchEvent() ACTION_DOWN return true to consume the ACTION_DOWN event.
// anyway: handle action down in onInterceptTouchEvent() to init is an good option
break;
case MotionEvent.ACTION_MOVE:
if (mActivePointerId == INVALID_POINTER) {
return false;
}
//hide headerView when refreshing , it will improve the user experience
if (mHasHeaderView && isRefreshing() && isHideHeaderWhenRefreshing){
mRefreshCallback.onReset();
scrollRefreshingToDefault();
}
//hide footerView when loadingmore , it will improve the user experience
if (mHasFooterView && isLoadingMore() && isHideFooterWhenLoadingMore){
mLoadMoreCallback.onReset();
scrollLoadingMoreToDefault();
}
float y = getMotionEventY(event, mActivePointerId);
float x = getMotionEventX(event, mActivePointerId);
final float yInitDiff = y - mInitDownY;
final float xInitDiff = x - mInitDownX;
mLastY = y;
mLastX = x;
boolean moved = Math.abs(yInitDiff) > Math.abs(xInitDiff);
boolean triggerCondition =
// refresh trigger condition
(yInitDiff > 0 && moved && onCheckCanRefresh()) ||
//load more trigger condition
(yInitDiff < 0 && moved && onCheckCanLoadMore());
if (triggerCondition) {
// if the refresh's or load more's trigger condition is true,
// intercept the move action event and pass it to SwipeToLoadLayout#onTouchEvent()
return true;
}
break;
case MotionEvent.ACTION_POINTER_UP: {
onSecondaryPointerUp(event);
mInitDownY = mLastY = getMotionEventY(event, mActivePointerId);
mInitDownX = mLastX = getMotionEventX(event, mActivePointerId);
break;
}
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_CANCEL:
mActivePointerId = INVALID_POINTER;
break;
}
return super.onInterceptTouchEvent(event);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
final int action = MotionEventCompat.getActionMasked(event);
switch (action) {
case MotionEvent.ACTION_DOWN:
mActivePointerId = MotionEventCompat.getPointerId(event, 0);
return true;
case MotionEvent.ACTION_MOVE:
// take over the ACTION_MOVE event from SwipeToLoadLayout#onInterceptTouchEvent()
// if condition is true
final float y = getMotionEventY(event, mActivePointerId);
final float x = getMotionEventX(event, mActivePointerId);
final float yDiff = y - mLastY;
final float xDiff = x - mLastX;
mLastY = y;
mLastX = x;
if (Math.abs(xDiff) > Math.abs(yDiff) && Math.abs(xDiff) > mTouchSlop) {
return false;
}
if (STATUS.isStatusDefault(mStatus)) {
if (yDiff > 0 && onCheckCanRefresh()) {
mRefreshCallback.onPrepare();
setStatus(STATUS.STATUS_SWIPING_TO_REFRESH);
} else if (yDiff < 0 && onCheckCanLoadMore()) {
mLoadMoreCallback.onPrepare();
setStatus(STATUS.STATUS_SWIPING_TO_LOAD_MORE);
}
} else if (STATUS.isRefreshStatus(mStatus)) {
if (mTargetOffset <= 0) {
setStatus(STATUS.STATUS_DEFAULT);
fixCurrentStatusLayout();
return false;
}
} else if (STATUS.isLoadMoreStatus(mStatus)) {
if (mTargetOffset >= 0) {
setStatus(STATUS.STATUS_DEFAULT);
fixCurrentStatusLayout();
return false;
}
}
if (STATUS.isRefreshStatus(mStatus)) {
if (STATUS.isSwipingToRefresh(mStatus) || STATUS.isReleaseToRefresh(mStatus)) {
if (mTargetOffset >= mRefreshTriggerOffset) {
setStatus(STATUS.STATUS_RELEASE_TO_REFRESH);
} else {
setStatus(STATUS.STATUS_SWIPING_TO_REFRESH);
}
fingerScroll(yDiff);
}
} else if (STATUS.isLoadMoreStatus(mStatus)) {
if (STATUS.isSwipingToLoadMore(mStatus) || STATUS.isReleaseToLoadMore(mStatus)) {
if (-mTargetOffset >= mLoadMoreTriggerOffset) {
setStatus(STATUS.STATUS_RELEASE_TO_LOAD_MORE);
} else {
setStatus(STATUS.STATUS_SWIPING_TO_LOAD_MORE);
}
fingerScroll(yDiff);
}
}
return true;
case MotionEvent.ACTION_POINTER_DOWN: {
final int pointerIndex = MotionEventCompat.getActionIndex(event);
final int pointerId = MotionEventCompat.getPointerId(event, pointerIndex);
if (pointerId != INVALID_POINTER) {
mActivePointerId = pointerId;
}
mInitDownY = mLastY = getMotionEventY(event, mActivePointerId);
mInitDownX = mLastX = getMotionEventX(event, mActivePointerId);
break;
}
case MotionEvent.ACTION_POINTER_UP: {
onSecondaryPointerUp(event);
mInitDownY = mLastY = getMotionEventY(event, mActivePointerId);
mInitDownX = mLastX = getMotionEventX(event, mActivePointerId);
break;
}
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_CANCEL:
if (mActivePointerId == INVALID_POINTER) {
return false;
}
mActivePointerId = INVALID_POINTER;
break;
default:
break;
}
return super.onTouchEvent(event);
}
/**
* set debug mode(default value false)
*
* @param debug if true log on, false log off
*/
public void setDebug(boolean debug) {
this.mDebug = debug;
}
/**
* is refresh function is enabled
*
* @return
*/
public boolean isRefreshEnabled() {
return mRefreshEnabled;
}
/**
* switch refresh function on or off
*
* @param enable
*/
public void setRefreshEnabled(boolean enable) {
this.mRefreshEnabled = enable;
}
/**
* is load more function is enabled
*
* @return
*/
public boolean isLoadMoreEnabled() {
return mLoadMoreEnabled;
}
/**
* switch load more function on or off
*
* @param enable
*/
public void setLoadMoreEnabled(boolean enable) {
this.mLoadMoreEnabled = enable;
}
/**
* is current status refreshing
*
* @return
*/
public boolean isRefreshing() {
return STATUS.isRefreshing(mStatus);
}
/**
* is current status loading more
*
* @return
*/
public boolean isLoadingMore() {
return STATUS.isLoadingMore(mStatus);
}
/**
* a switcher whither hiding headerView when refreshing
*
* @param hideable
*/
public void setHideHeaderWhenRefreshing(boolean hideable) {
this.isHideHeaderWhenRefreshing = hideable;
}
/**
* a switcher whither hiding footerView when loadingmore
*
* @param hideable
*/
public void setHideFooterWhenLoadingMore(boolean hideable) {
this.isHideFooterWhenLoadingMore = hideable;
}
/**
* set refresh header view, the view must at lease be an implement of {@code SwipeRefreshTrigger}.
* the view can also implement {@code SwipeTrigger} for more extension functions
*
* @param view
*/
public void setRefreshHeaderView(View view) {
if (view instanceof SwipeRefreshTrigger) {
if (mHeaderView != null && mHeaderView != view) {
removeView(mHeaderView);
}
if (mHeaderView != view) {
this.mHeaderView = view;
addView(view);
}
} else {
Log.e(TAG, "Refresh header view must be an implement of SwipeRefreshTrigger");
}
}
/**
* set load more footer view, the view must at least be an implement of SwipeLoadTrigger
* the view can also implement {@code SwipeTrigger} for more extension functions
*
* @param view
*/
public void setLoadMoreFooterView(View view) {
if (view instanceof SwipeLoadMoreTrigger) {
if (mFooterView != null && mFooterView != view) {
removeView(mFooterView);
}
if (mFooterView != view) {
this.mFooterView = view;
addView(mFooterView);
}
} else {
Log.e(TAG, "Load more footer view must be an implement of SwipeLoadTrigger");
}
}
/**
* set the style of the refresh header
*
* @param style
*/
public void setSwipeStyle(int style) {
this.mStyle = style;
requestLayout();
}
/**
* set how hard to drag. bigger easier, smaller harder;
*
* @param dragRatio default value is {@link #DEFAULT_DRAG_RATIO}
*/
public void setDragRatio(float dragRatio) {
this.mDragRatio = dragRatio;
}
/**
* set the value of {@link #mRefreshTriggerOffset}.
* Default value is the refresh header view height {@link #mHeaderHeight}<p/>
* If the offset you set is smaller than {@link #mHeaderHeight} or not set,
* using {@link #mHeaderHeight} as default value
*
* @param offset
*/
public void setRefreshTriggerOffset(int offset) {
mRefreshTriggerOffset = offset;
}
/**
* set the value of {@link #mLoadMoreTriggerOffset}.
* Default value is the load more footer view height {@link #mFooterHeight}<p/>
* If the offset you set is smaller than {@link #mFooterHeight} or not set,
* using {@link #mFooterHeight} as default value
*
* @param offset
*/
public void setLoadMoreTriggerOffset(int offset) {
mLoadMoreTriggerOffset = offset;
}
/**
* Set the final offset you can swipe to refresh.<br/>
* If the offset you set is 0(default value) or smaller than {@link #mRefreshTriggerOffset}
* there no final offset
*
* @param offset
*/
public void setRefreshFinalDragOffset(int offset) {
mRefreshFinalDragOffset = offset;
}
/**
* Set the final offset you can swipe to load more.<br/>
* If the offset you set is 0(default value) or smaller than {@link #mLoadMoreTriggerOffset},
* there no final offset
*
* @param offset
*/
public void setLoadMoreFinalDragOffset(int offset) {
mLoadMoreFinalDragOffset = offset;
}
/**
* set {@link #mSwipingToRefreshToDefaultScrollingDuration} in milliseconds
*
* @param duration
*/
public void setSwipingToRefreshToDefaultScrollingDuration(int duration) {
this.mSwipingToRefreshToDefaultScrollingDuration = duration;
}
/**
* set {@link #mReleaseToRefreshToRefreshingScrollingDuration} in milliseconds
*
* @param duration
*/
public void setReleaseToRefreshingScrollingDuration(int duration) {
this.mReleaseToRefreshToRefreshingScrollingDuration = duration;
}
/**
* set {@link #mRefreshCompleteDelayDuration} in milliseconds
*
* @param duration
*/
public void setRefreshCompleteDelayDuration(int duration) {
this.mRefreshCompleteDelayDuration = duration;
}
/**
* set {@link #mRefreshCompleteToDefaultScrollingDuration} in milliseconds
*
* @param duration
*/
public void setRefreshCompleteToDefaultScrollingDuration(int duration) {
this.mRefreshCompleteToDefaultScrollingDuration = duration;
}
/**
* set {@link #mDefaultToRefreshingScrollingDuration} in milliseconds
*
* @param duration
*/
public void setDefaultToRefreshingScrollingDuration(int duration) {
this.mDefaultToRefreshingScrollingDuration = duration;
}
/**
* set {@link @mSwipingToLoadMoreToDefaultScrollingDuration} in milliseconds
*
* @param duration
*/
public void setSwipingToLoadMoreToDefaultScrollingDuration(int duration) {
this.mSwipingToLoadMoreToDefaultScrollingDuration = duration;
}
/**
* set {@link #mReleaseToLoadMoreToLoadingMoreScrollingDuration} in milliseconds
*
* @param duration
*/
public void setReleaseToLoadingMoreScrollingDuration(int duration) {
this.mReleaseToLoadMoreToLoadingMoreScrollingDuration = duration;
}
/**
* set {@link #mLoadMoreCompleteDelayDuration} in milliseconds
*
* @param duration
*/
public void setLoadMoreCompleteDelayDuration(int duration) {
this.mLoadMoreCompleteDelayDuration = duration;
}
/**
* set {@link #mLoadMoreCompleteToDefaultScrollingDuration} in milliseconds
*
* @param duration
*/
public void setLoadMoreCompleteToDefaultScrollingDuration(int duration) {
this.mLoadMoreCompleteToDefaultScrollingDuration = duration;
}
/**
* set {@link #mDefaultToLoadingMoreScrollingDuration} in milliseconds
*
* @param duration
*/
public void setDefaultToLoadingMoreScrollingDuration(int duration) {
this.mDefaultToLoadingMoreScrollingDuration = duration;
}
/**
* set an {@link OnRefreshListener} to listening refresh event
*
* @param listener
*/
public void setOnRefreshListener(OnRefreshListener listener) {
this.mRefreshListener = listener;
}
/**
* set an {@link OnLoadMoreListener} to listening load more event
*
* @param listener
*/
public void setOnLoadMoreListener(OnLoadMoreListener listener) {
this.mLoadMoreListener = listener;
}
/**
* auto refresh or cancel
*
* @param refreshing
*/
public void setRefreshing(boolean refreshing) {
if (!isRefreshEnabled() || mHeaderView == null) {
return;
}
this.mAutoLoading = refreshing;
if (refreshing) {
if (STATUS.isStatusDefault(mStatus)) {
setStatus(STATUS.STATUS_SWIPING_TO_REFRESH);
scrollDefaultToRefreshing();
}
} else {
if (STATUS.isRefreshing(mStatus)) {
mRefreshCallback.onComplete();
postDelayed(new Runnable() {
@Override
public void run() {
scrollRefreshingToDefault();
}
}, mRefreshCompleteDelayDuration);
}
}
}
/**
* auto loading more or cancel
*
* @param loadingMore
*/
public void setLoadingMore(boolean loadingMore) {
if (mFooterView == null) {