-
Notifications
You must be signed in to change notification settings - Fork 144
Expand file tree
/
Copy pathGalleryLayoutManager.java
More file actions
1079 lines (978 loc) · 43.7 KB
/
Copy pathGalleryLayoutManager.java
File metadata and controls
1079 lines (978 loc) · 43.7 KB
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 github.hellocsl.layoutmanager.gallery;
import android.content.Context;
import android.graphics.PointF;
import android.graphics.Rect;
import android.support.v7.widget.LinearSmoothScroller;
import android.support.v7.widget.LinearSnapHelper;
import android.support.v7.widget.OrientationHelper;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.util.Log;
import android.util.SparseArray;
import android.view.View;
import android.view.ViewGroup;
import static android.support.v7.widget.RecyclerView.SCROLL_STATE_IDLE;
/**
* A custom LayoutManager to build a {@link android.widget.Gallery} or a {@link android.support.v4.view.ViewPager}like {@link RecyclerView} and
* support both {@link GalleryLayoutManager#HORIZONTAL} and {@link GalleryLayoutManager#VERTICAL} scroll.
* Created by chensuilun on 2016/11/18.
*/
public class GalleryLayoutManager extends RecyclerView.LayoutManager implements RecyclerView.SmoothScroller.ScrollVectorProvider {
private static final boolean DEBUG = false;
private static final String TAG = "GalleryLayoutManager";
final static int LAYOUT_START = -1;
final static int LAYOUT_END = 1;
public static final int HORIZONTAL = OrientationHelper.HORIZONTAL;
public static final int VERTICAL = OrientationHelper.VERTICAL;
private int mFirstVisiblePosition = 0;
private int mLastVisiblePos = 0;
private int mInitialSelectedPosition = 0;
int mCurSelectedPosition = -1;
View mCurSelectedView;
/**
* Scroll state
*/
private State mState;
private LinearSnapHelper mSnapHelper = new LinearSnapHelper();
private InnerScrollListener mInnerScrollListener = new InnerScrollListener();
private boolean mCallbackInFling = false;
/**
* Current orientation. Either {@link #HORIZONTAL} or {@link #VERTICAL}
*/
private int mOrientation = HORIZONTAL;
private OrientationHelper mHorizontalHelper;
private OrientationHelper mVerticalHelper;
public GalleryLayoutManager(int orientation) {
mOrientation = orientation;
}
public int getOrientation() {
return mOrientation;
}
public int getCurSelectedPosition() {
return mCurSelectedPosition;
}
@Override
public RecyclerView.LayoutParams generateDefaultLayoutParams() {
if (mOrientation == VERTICAL) {
return new GalleryLayoutManager.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
} else {
return new GalleryLayoutManager.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.MATCH_PARENT);
}
}
@Override
public RecyclerView.LayoutParams generateLayoutParams(Context c, AttributeSet attrs) {
return new LayoutParams(c, attrs);
}
@Override
public RecyclerView.LayoutParams generateLayoutParams(ViewGroup.LayoutParams lp) {
if (lp instanceof ViewGroup.MarginLayoutParams) {
return new LayoutParams((ViewGroup.MarginLayoutParams) lp);
} else {
return new LayoutParams(lp);
}
}
@Override
public boolean checkLayoutParams(RecyclerView.LayoutParams lp) {
return lp instanceof LayoutParams;
}
@Override
public void onLayoutChildren(RecyclerView.Recycler recycler, RecyclerView.State state) {
if (BuildConfig.DEBUG) {
Log.d(TAG, "onLayoutChildren() called with: state = [" + state + "]");
}
if (getItemCount() == 0) {
reset();
detachAndScrapAttachedViews(recycler);
return;
}
if (state.isPreLayout()) {
return;
}
if (state.getItemCount() != 0 && !state.didStructureChange()) {
if (BuildConfig.DEBUG) {
Log.d(TAG, "onLayoutChildren: ignore extra layout step");
}
return;
}
if (getChildCount() == 0 || state.didStructureChange()) {
reset();
}
mInitialSelectedPosition = Math.min(Math.max(0, mInitialSelectedPosition), getItemCount() - 1);
detachAndScrapAttachedViews(recycler);
firstFillCover(recycler, state, 0);
}
private void reset() {
if (BuildConfig.DEBUG) {
Log.d(TAG, "reset: ");
}
if (mState != null) {
mState.mItemsFrames.clear();
}
if (mCurSelectedPosition != -1) {
mInitialSelectedPosition = mCurSelectedPosition;
} else {
mInitialSelectedPosition = 0;
}
mFirstVisiblePosition = mInitialSelectedPosition;
mLastVisiblePos = mInitialSelectedPosition;
mCurSelectedPosition = -1;
if (mCurSelectedView != null) {
mCurSelectedView.setSelected(false);
mCurSelectedView = null;
}
}
private void firstFillCover(RecyclerView.Recycler recycler, RecyclerView.State state, int scrollDelta) {
if (mOrientation == HORIZONTAL) {
firstFillWithHorizontal(recycler, state);
} else {
firstFillWithVertical(recycler, state);
}
if (BuildConfig.DEBUG) {
Log.d(TAG, "firstFillCover finish:first: " + mFirstVisiblePosition + ",last:" + mLastVisiblePos);
}
if (mItemTransformer != null) {
View child;
for (int i = 0; i < getChildCount(); i++) {
child = getChildAt(i);
mItemTransformer.transformItem(this, child, calculateToCenterFraction(child, scrollDelta));
}
}
mInnerScrollListener.onScrolled(mRecyclerView, 0, 0);
}
/**
* Layout the item view witch position specified by {@link GalleryLayoutManager#mInitialSelectedPosition} first and then layout the other
*
* @param recycler
* @param state
*/
private void firstFillWithHorizontal(RecyclerView.Recycler recycler, RecyclerView.State state) {
detachAndScrapAttachedViews(recycler);
int leftEdge = getOrientationHelper().getStartAfterPadding();
int rightEdge = getOrientationHelper().getEndAfterPadding();
int startPosition = mInitialSelectedPosition;
int scrapWidth, scrapHeight;
Rect scrapRect = new Rect();
int height = getVerticalSpace();
int topOffset;
//layout the init position view
View scrap = recycler.getViewForPosition(mInitialSelectedPosition);
addView(scrap, 0);
measureChildWithMargins(scrap, 0, 0);
scrapWidth = getDecoratedMeasuredWidth(scrap);
scrapHeight = getDecoratedMeasuredHeight(scrap);
topOffset = (int) (getPaddingTop() + (height - scrapHeight) / 2.0f);
int left = (int) (getPaddingLeft() + (getHorizontalSpace() - scrapWidth) / 2.f);
scrapRect.set(left, topOffset, left + scrapWidth, topOffset + scrapHeight);
layoutDecorated(scrap, scrapRect.left, scrapRect.top, scrapRect.right, scrapRect.bottom);
if (getState().mItemsFrames.get(startPosition) == null) {
getState().mItemsFrames.put(startPosition, scrapRect);
} else {
getState().mItemsFrames.get(startPosition).set(scrapRect);
}
mFirstVisiblePosition = mLastVisiblePos = startPosition;
int leftStartOffset = getDecoratedLeft(scrap);
int rightStartOffset = getDecoratedRight(scrap);
//fill left of center
fillLeft(recycler, mInitialSelectedPosition - 1, leftStartOffset, leftEdge);
//fill right of center
fillRight(recycler, mInitialSelectedPosition + 1, rightStartOffset, rightEdge);
}
@Override
public void onItemsRemoved(RecyclerView recyclerView, int positionStart, int itemCount) {
super.onItemsRemoved(recyclerView, positionStart, itemCount);
}
/**
* Layout the item view witch position special by {@link GalleryLayoutManager#mInitialSelectedPosition} first and then layout the other
*
* @param recycler
* @param state
*/
private void firstFillWithVertical(RecyclerView.Recycler recycler, RecyclerView.State state) {
detachAndScrapAttachedViews(recycler);
int topEdge = getOrientationHelper().getStartAfterPadding();
int bottomEdge = getOrientationHelper().getEndAfterPadding();
int startPosition = mInitialSelectedPosition;
int scrapWidth, scrapHeight;
Rect scrapRect = new Rect();
int width = getHorizontalSpace();
int leftOffset;
//layout the init position view
View scrap = recycler.getViewForPosition(mInitialSelectedPosition);
addView(scrap, 0);
measureChildWithMargins(scrap, 0, 0);
scrapWidth = getDecoratedMeasuredWidth(scrap);
scrapHeight = getDecoratedMeasuredHeight(scrap);
leftOffset = (int) (getPaddingLeft() + (width - scrapWidth) / 2.0f);
int top = (int) (getPaddingTop() + (getVerticalSpace() - scrapHeight) / 2.f);
scrapRect.set(leftOffset, top, leftOffset + scrapWidth, top + scrapHeight);
layoutDecorated(scrap, scrapRect.left, scrapRect.top, scrapRect.right, scrapRect.bottom);
if (getState().mItemsFrames.get(startPosition) == null) {
getState().mItemsFrames.put(startPosition, scrapRect);
} else {
getState().mItemsFrames.get(startPosition).set(scrapRect);
}
mFirstVisiblePosition = mLastVisiblePos = startPosition;
int topStartOffset = getDecoratedTop(scrap);
int bottomStartOffset = getDecoratedBottom(scrap);
//fill left of center
fillTop(recycler, mInitialSelectedPosition - 1, topStartOffset, topEdge);
//fill right of center
fillBottom(recycler, mInitialSelectedPosition + 1, bottomStartOffset, bottomEdge);
}
/**
* Fill left of the center view
*
* @param recycler
* @param startPosition start position to fill left
* @param startOffset layout start offset
* @param leftEdge
*/
private void fillLeft(RecyclerView.Recycler recycler, int startPosition, int startOffset, int leftEdge) {
View scrap;
int topOffset;
int scrapWidth, scrapHeight;
Rect scrapRect = new Rect();
int height = getVerticalSpace();
for (int i = startPosition; i >= 0 && startOffset > leftEdge; i--) {
scrap = recycler.getViewForPosition(i);
addView(scrap, 0);
measureChildWithMargins(scrap, 0, 0);
scrapWidth = getDecoratedMeasuredWidth(scrap);
scrapHeight = getDecoratedMeasuredHeight(scrap);
topOffset = (int) (getPaddingTop() + (height - scrapHeight) / 2.0f);
scrapRect.set(startOffset - scrapWidth, topOffset, startOffset, topOffset + scrapHeight);
layoutDecorated(scrap, scrapRect.left, scrapRect.top, scrapRect.right, scrapRect.bottom);
startOffset = scrapRect.left;
mFirstVisiblePosition = i;
if (getState().mItemsFrames.get(i) == null) {
getState().mItemsFrames.put(i, scrapRect);
} else {
getState().mItemsFrames.get(i).set(scrapRect);
}
}
}
/**
* Fill right of the center view
*
* @param recycler
* @param startPosition start position to fill right
* @param startOffset layout start offset
* @param rightEdge
*/
private void fillRight(RecyclerView.Recycler recycler, int startPosition, int startOffset, int rightEdge) {
View scrap;
int topOffset;
int scrapWidth, scrapHeight;
Rect scrapRect = new Rect();
int height = getVerticalSpace();
for (int i = startPosition; i < getItemCount() && startOffset < rightEdge; i++) {
scrap = recycler.getViewForPosition(i);
addView(scrap);
measureChildWithMargins(scrap, 0, 0);
scrapWidth = getDecoratedMeasuredWidth(scrap);
scrapHeight = getDecoratedMeasuredHeight(scrap);
topOffset = (int) (getPaddingTop() + (height - scrapHeight) / 2.0f);
scrapRect.set(startOffset, topOffset, startOffset + scrapWidth, topOffset + scrapHeight);
layoutDecorated(scrap, scrapRect.left, scrapRect.top, scrapRect.right, scrapRect.bottom);
startOffset = scrapRect.right;
mLastVisiblePos = i;
if (getState().mItemsFrames.get(i) == null) {
getState().mItemsFrames.put(i, scrapRect);
} else {
getState().mItemsFrames.get(i).set(scrapRect);
}
}
}
/**
* Fill top of the center view
*
* @param recycler
* @param startPosition start position to fill top
* @param startOffset layout start offset
* @param topEdge top edge of the RecycleView
*/
private void fillTop(RecyclerView.Recycler recycler, int startPosition, int startOffset, int topEdge) {
View scrap;
int leftOffset;
int scrapWidth, scrapHeight;
Rect scrapRect = new Rect();
int width = getHorizontalSpace();
for (int i = startPosition; i >= 0 && startOffset > topEdge; i--) {
scrap = recycler.getViewForPosition(i);
addView(scrap, 0);
measureChildWithMargins(scrap, 0, 0);
scrapWidth = getDecoratedMeasuredWidth(scrap);
scrapHeight = getDecoratedMeasuredHeight(scrap);
leftOffset = (int) (getPaddingLeft() + (width - scrapWidth) / 2.0f);
scrapRect.set(leftOffset, startOffset - scrapHeight, leftOffset + scrapWidth, startOffset);
layoutDecorated(scrap, scrapRect.left, scrapRect.top, scrapRect.right, scrapRect.bottom);
startOffset = scrapRect.top;
mFirstVisiblePosition = i;
if (getState().mItemsFrames.get(i) == null) {
getState().mItemsFrames.put(i, scrapRect);
} else {
getState().mItemsFrames.get(i).set(scrapRect);
}
}
}
/**
* Fill bottom of the center view
*
* @param recycler
* @param startPosition start position to fill bottom
* @param startOffset layout start offset
* @param bottomEdge bottom edge of the RecycleView
*/
private void fillBottom(RecyclerView.Recycler recycler, int startPosition, int startOffset, int bottomEdge) {
View scrap;
int leftOffset;
int scrapWidth, scrapHeight;
Rect scrapRect = new Rect();
int width = getHorizontalSpace();
for (int i = startPosition; i < getItemCount() && startOffset < bottomEdge; i++) {
scrap = recycler.getViewForPosition(i);
addView(scrap);
measureChildWithMargins(scrap, 0, 0);
scrapWidth = getDecoratedMeasuredWidth(scrap);
scrapHeight = getDecoratedMeasuredHeight(scrap);
leftOffset = (int) (getPaddingLeft() + (width - scrapWidth) / 2.0f);
scrapRect.set(leftOffset, startOffset, leftOffset + scrapWidth, startOffset + scrapHeight);
layoutDecorated(scrap, scrapRect.left, scrapRect.top, scrapRect.right, scrapRect.bottom);
startOffset = scrapRect.bottom;
mLastVisiblePos = i;
if (getState().mItemsFrames.get(i) == null) {
getState().mItemsFrames.put(i, scrapRect);
} else {
getState().mItemsFrames.get(i).set(scrapRect);
}
}
}
private void fillCover(RecyclerView.Recycler recycler, RecyclerView.State state, int scrollDelta) {
if (getItemCount() == 0) {
return;
}
if (mOrientation == HORIZONTAL) {
fillWithHorizontal(recycler, state, scrollDelta);
} else {
fillWithVertical(recycler, state, scrollDelta);
}
if (mItemTransformer != null) {
View child;
for (int i = 0; i < getChildCount(); i++) {
child = getChildAt(i);
mItemTransformer.transformItem(this, child, calculateToCenterFraction(child, scrollDelta));
}
}
}
private float calculateToCenterFraction(View child, float pendingOffset) {
int distance = calculateDistanceCenter(child, pendingOffset);
int childLength = mOrientation == GalleryLayoutManager.HORIZONTAL ? child.getWidth() : child.getHeight();
if (BuildConfig.DEBUG) {
Log.d(TAG, "calculateToCenterFraction: distance:" + distance + ",childLength:" + childLength);
}
return Math.max(-1.f, Math.min(1.f, distance * 1.f / childLength));
}
/**
* @param child
* @param pendingOffset child view will scroll by
* @return
*/
private int calculateDistanceCenter(View child, float pendingOffset) {
OrientationHelper orientationHelper = getOrientationHelper();
int parentCenter = (orientationHelper.getEndAfterPadding() - orientationHelper.getStartAfterPadding()) / 2 + orientationHelper.getStartAfterPadding();
if (mOrientation == GalleryLayoutManager.HORIZONTAL) {
return (int) (child.getWidth() / 2 - pendingOffset + child.getLeft() - parentCenter);
} else {
return (int) (child.getHeight() / 2 - pendingOffset + child.getTop() - parentCenter);
}
}
/**
* @param recycler
* @param state
* @param dy
*/
private void fillWithVertical(RecyclerView.Recycler recycler, RecyclerView.State state, int dy) {
if (BuildConfig.DEBUG) {
Log.d(TAG, "fillWithVertical: dy:" + dy);
}
int topEdge = getOrientationHelper().getStartAfterPadding();
int bottomEdge = getOrientationHelper().getEndAfterPadding();
//1.remove and recycle the view that disappear in screen
View child;
if (getChildCount() > 0) {
if (dy >= 0) {
//remove and recycle the top off screen view
int fixIndex = 0;
for (int i = 0; i < getChildCount(); i++) {
child = getChildAt(i + fixIndex);
if (getDecoratedBottom(child) - dy < topEdge) {
if (BuildConfig.DEBUG) {
Log.v(TAG, "fillWithVertical: removeAndRecycleView:" + getPosition(child) + ",bottom:" + getDecoratedBottom(child));
}
removeAndRecycleView(child, recycler);
mFirstVisiblePosition++;
fixIndex--;
} else {
if (BuildConfig.DEBUG) {
Log.d(TAG, "fillWithVertical: break:" + getPosition(child) + ",bottom:" + getDecoratedBottom(child));
}
break;
}
}
} else { //dy<0
//remove and recycle the bottom off screen view
for (int i = getChildCount() - 1; i >= 0; i--) {
child = getChildAt(i);
if (getDecoratedTop(child) - dy > bottomEdge) {
if (BuildConfig.DEBUG) {
Log.v(TAG, "fillWithVertical: removeAndRecycleView:" + getPosition(child));
}
removeAndRecycleView(child, recycler);
mLastVisiblePos--;
} else {
break;
}
}
}
}
int startPosition = mFirstVisiblePosition;
int startOffset = -1;
int scrapWidth, scrapHeight;
Rect scrapRect;
int width = getHorizontalSpace();
int leftOffset;
View scrap;
//2.Add or reattach item view to fill screen
if (dy >= 0) {
if (getChildCount() != 0) {
View lastView = getChildAt(getChildCount() - 1);
startPosition = getPosition(lastView) + 1;
startOffset = getDecoratedBottom(lastView);
}
for (int i = startPosition; i < getItemCount() && startOffset < bottomEdge + dy; i++) {
scrapRect = getState().mItemsFrames.get(i);
scrap = recycler.getViewForPosition(i);
addView(scrap);
if (scrapRect == null) {
scrapRect = new Rect();
getState().mItemsFrames.put(i, scrapRect);
}
measureChildWithMargins(scrap, 0, 0);
scrapWidth = getDecoratedMeasuredWidth(scrap);
scrapHeight = getDecoratedMeasuredHeight(scrap);
leftOffset = (int) (getPaddingLeft() + (width - scrapWidth) / 2.0f);
if (startOffset == -1 && startPosition == 0) {
//layout the first position item in center
int top = (int) (getPaddingTop() + (getVerticalSpace() - scrapHeight) / 2.f);
scrapRect.set(leftOffset, top, leftOffset + scrapWidth, top + scrapHeight);
} else {
scrapRect.set(leftOffset, startOffset, leftOffset + scrapWidth, startOffset + scrapHeight);
}
layoutDecorated(scrap, scrapRect.left, scrapRect.top, scrapRect.right, scrapRect.bottom);
startOffset = scrapRect.bottom;
mLastVisiblePos = i;
if (BuildConfig.DEBUG) {
Log.d(TAG, "fillWithVertical: add view:" + i + ",startOffset:" + startOffset + ",mLastVisiblePos:" + mLastVisiblePos + ",bottomEdge" + bottomEdge);
}
}
} else {
//dy<0
if (getChildCount() > 0) {
View firstView = getChildAt(0);
startPosition = getPosition(firstView) - 1; //前一个View的position
startOffset = getDecoratedTop(firstView);
}
for (int i = startPosition; i >= 0 && startOffset > topEdge + dy; i--) {
scrapRect = getState().mItemsFrames.get(i);
scrap = recycler.getViewForPosition(i);
addView(scrap, 0);
if (scrapRect == null) {
scrapRect = new Rect();
getState().mItemsFrames.put(i, scrapRect);
}
measureChildWithMargins(scrap, 0, 0);
scrapWidth = getDecoratedMeasuredWidth(scrap);
scrapHeight = getDecoratedMeasuredHeight(scrap);
leftOffset = (int) (getPaddingLeft() + (width - scrapWidth) / 2.0f);
scrapRect.set(leftOffset, startOffset - scrapHeight, leftOffset + scrapWidth, startOffset);
layoutDecorated(scrap, scrapRect.left, scrapRect.top, scrapRect.right, scrapRect.bottom);
startOffset = scrapRect.top;
mFirstVisiblePosition = i;
}
}
}
/**
* @param recycler
* @param state
*/
private void fillWithHorizontal(RecyclerView.Recycler recycler, RecyclerView.State state, int dx) {
int leftEdge = getOrientationHelper().getStartAfterPadding();
int rightEdge = getOrientationHelper().getEndAfterPadding();
if (BuildConfig.DEBUG) {
Log.v(TAG, "fillWithHorizontal() called with: dx = [" + dx + "],leftEdge:" + leftEdge + ",rightEdge:" + rightEdge);
}
//1.remove and recycle the view that disappear in screen
View child;
if (getChildCount() > 0) {
if (dx >= 0) {
//remove and recycle the left off screen view
int fixIndex = 0;
for (int i = 0; i < getChildCount(); i++) {
child = getChildAt(i + fixIndex);
if (getDecoratedRight(child) - dx < leftEdge) {
removeAndRecycleView(child, recycler);
mFirstVisiblePosition++;
fixIndex--;
if (BuildConfig.DEBUG) {
Log.v(TAG, "fillWithHorizontal:removeAndRecycleView:" + getPosition(child) + " mFirstVisiblePosition change to:" + mFirstVisiblePosition);
}
} else {
break;
}
}
} else { //dx<0
//remove and recycle the right off screen view
for (int i = getChildCount() - 1; i >= 0; i--) {
child = getChildAt(i);
if (getDecoratedLeft(child) - dx > rightEdge) {
removeAndRecycleView(child, recycler);
mLastVisiblePos--;
if (BuildConfig.DEBUG) {
Log.v(TAG, "fillWithHorizontal:removeAndRecycleView:" + getPosition(child) + "mLastVisiblePos change to:" + mLastVisiblePos);
}
}
}
}
}
//2.Add or reattach item view to fill screen
int startPosition = mFirstVisiblePosition;
int startOffset = -1;
int scrapWidth, scrapHeight;
Rect scrapRect;
int height = getVerticalSpace();
int topOffset;
View scrap;
if (dx >= 0) {
if (getChildCount() != 0) {
View lastView = getChildAt(getChildCount() - 1);
startPosition = getPosition(lastView) + 1; //start layout from next position item
startOffset = getDecoratedRight(lastView);
if (BuildConfig.DEBUG) {
Log.d(TAG, "fillWithHorizontal:to right startPosition:" + startPosition + ",startOffset:" + startOffset + ",rightEdge:" + rightEdge);
}
}
for (int i = startPosition; i < getItemCount() && startOffset < rightEdge + dx; i++) {
scrapRect = getState().mItemsFrames.get(i);
scrap = recycler.getViewForPosition(i);
addView(scrap);
if (scrapRect == null) {
scrapRect = new Rect();
getState().mItemsFrames.put(i, scrapRect);
}
measureChildWithMargins(scrap, 0, 0);
scrapWidth = getDecoratedMeasuredWidth(scrap);
scrapHeight = getDecoratedMeasuredHeight(scrap);
topOffset = (int) (getPaddingTop() + (height - scrapHeight) / 2.0f);
if (startOffset == -1 && startPosition == 0) {
// layout the first position item in center
int left = (int) (getPaddingLeft() + (getHorizontalSpace() - scrapWidth) / 2.f);
scrapRect.set(left, topOffset, left + scrapWidth, topOffset + scrapHeight);
} else {
scrapRect.set(startOffset, topOffset, startOffset + scrapWidth, topOffset + scrapHeight);
}
layoutDecorated(scrap, scrapRect.left, scrapRect.top, scrapRect.right, scrapRect.bottom);
startOffset = scrapRect.right;
mLastVisiblePos = i;
if (BuildConfig.DEBUG) {
Log.d(TAG, "fillWithHorizontal,layout:mLastVisiblePos: " + mLastVisiblePos);
}
}
} else {
//dx<0
if (getChildCount() > 0) {
View firstView = getChildAt(0);
startPosition = getPosition(firstView) - 1; //start layout from previous position item
startOffset = getDecoratedLeft(firstView);
if (BuildConfig.DEBUG) {
Log.d(TAG, "fillWithHorizontal:to left startPosition:" + startPosition + ",startOffset:" + startOffset + ",leftEdge:" + leftEdge + ",child count:" + getChildCount());
}
}
for (int i = startPosition; i >= 0 && startOffset > leftEdge + dx; i--) {
scrapRect = getState().mItemsFrames.get(i);
scrap = recycler.getViewForPosition(i);
addView(scrap, 0);
if (scrapRect == null) {
scrapRect = new Rect();
getState().mItemsFrames.put(i, scrapRect);
}
measureChildWithMargins(scrap, 0, 0);
scrapWidth = getDecoratedMeasuredWidth(scrap);
scrapHeight = getDecoratedMeasuredHeight(scrap);
topOffset = (int) (getPaddingTop() + (height - scrapHeight) / 2.0f);
scrapRect.set(startOffset - scrapWidth, topOffset, startOffset, topOffset + scrapHeight);
layoutDecorated(scrap, scrapRect.left, scrapRect.top, scrapRect.right, scrapRect.bottom);
startOffset = scrapRect.left;
mFirstVisiblePosition = i;
}
}
}
private int getHorizontalSpace() {
return getWidth() - getPaddingRight() - getPaddingLeft();
}
private int getVerticalSpace() {
return getHeight() - getPaddingBottom() - getPaddingTop();
}
public State getState() {
if (mState == null) {
mState = new State();
}
return mState;
}
private int calculateScrollDirectionForPosition(int position) {
if (getChildCount() == 0) {
return LAYOUT_START;
}
final int firstChildPos = mFirstVisiblePosition;
return position < firstChildPos ? LAYOUT_START : LAYOUT_END;
}
@Override
public PointF computeScrollVectorForPosition(int targetPosition) {
final int direction = calculateScrollDirectionForPosition(targetPosition);
PointF outVector = new PointF();
if (direction == 0) {
return null;
}
if (mOrientation == HORIZONTAL) {
outVector.x = direction;
outVector.y = 0;
} else {
outVector.x = 0;
outVector.y = direction;
}
return outVector;
}
/**
* @author chensuilun
*/
class State {
/**
* Record all item view 's last position after last layout
*/
SparseArray<Rect> mItemsFrames;
/**
* RecycleView 's current scroll distance since first layout
*/
int mScrollDelta;
public State() {
mItemsFrames = new SparseArray<Rect>();
mScrollDelta = 0;
}
}
@Override
public boolean canScrollHorizontally() {
return mOrientation == HORIZONTAL;
}
@Override
public boolean canScrollVertically() {
return mOrientation == VERTICAL;
}
@Override
public int scrollHorizontallyBy(int dx, RecyclerView.Recycler recycler, RecyclerView.State state) {
// When dx is positive,finger fling from right to left(←),scrollX+
if (getChildCount() == 0 || dx == 0) {
return 0;
}
int delta = -dx;
int parentCenter = (getOrientationHelper().getEndAfterPadding() - getOrientationHelper().getStartAfterPadding()) / 2 + getOrientationHelper().getStartAfterPadding();
View child;
if (dx > 0) {
//If we've reached the last item, enforce limits
if (getPosition(getChildAt(getChildCount() - 1)) == getItemCount() - 1) {
child = getChildAt(getChildCount() - 1);
delta = -Math.max(0, Math.min(dx, (child.getRight() - child.getLeft()) / 2 + child.getLeft() - parentCenter));
}
} else {
//If we've reached the first item, enforce limits
if (mFirstVisiblePosition == 0) {
child = getChildAt(0);
delta = -Math.min(0, Math.max(dx, ((child.getRight() - child.getLeft()) / 2 + child.getLeft()) - parentCenter));
}
}
if (BuildConfig.DEBUG) {
Log.d(TAG, "scrollHorizontallyBy: dx:" + dx + ",fixed:" + delta);
}
getState().mScrollDelta = -delta;
fillCover(recycler, state, -delta);
offsetChildrenHorizontal(delta);
return -delta;
}
@Override
public int scrollVerticallyBy(int dy, RecyclerView.Recycler recycler, RecyclerView.State state) {
if (getChildCount() == 0 || dy == 0) {
return 0;
}
int delta = -dy;
int parentCenter = (getOrientationHelper().getEndAfterPadding() - getOrientationHelper().getStartAfterPadding()) / 2 + getOrientationHelper().getStartAfterPadding();
View child;
if (dy > 0) {
//If we've reached the last item, enforce limits
if (getPosition(getChildAt(getChildCount() - 1)) == getItemCount() - 1) {
child = getChildAt(getChildCount() - 1);
delta = -Math.max(0, Math.min(dy, (getDecoratedBottom(child) - getDecoratedTop(child)) / 2 + getDecoratedTop(child) - parentCenter));
}
} else {
//If we've reached the first item, enforce limits
if (mFirstVisiblePosition == 0) {
child = getChildAt(0);
delta = -Math.min(0, Math.max(dy, (getDecoratedBottom(child) - getDecoratedTop(child)) / 2 + getDecoratedTop(child) - parentCenter));
}
}
if (BuildConfig.DEBUG) {
Log.d(TAG, "scrollVerticallyBy: dy:" + dy + ",fixed:" + delta);
}
getState().mScrollDelta = -delta;
fillCover(recycler, state, -delta);
offsetChildrenVertical(delta);
return -delta;
}
public OrientationHelper getOrientationHelper() {
if (mOrientation == HORIZONTAL) {
if (mHorizontalHelper == null) {
mHorizontalHelper = OrientationHelper.createHorizontalHelper(this);
}
return mHorizontalHelper;
} else {
if (mVerticalHelper == null) {
mVerticalHelper = OrientationHelper.createVerticalHelper(this);
}
return mVerticalHelper;
}
}
/**
* @author chensuilun
*/
public static class LayoutParams extends RecyclerView.LayoutParams {
public LayoutParams(Context c, AttributeSet attrs) {
super(c, attrs);
}
public LayoutParams(int width, int height) {
super(width, height);
}
public LayoutParams(ViewGroup.MarginLayoutParams source) {
super(source);
}
public LayoutParams(ViewGroup.LayoutParams source) {
super(source);
}
public LayoutParams(RecyclerView.LayoutParams source) {
super(source);
}
}
private ItemTransformer mItemTransformer;
public void setItemTransformer(ItemTransformer itemTransformer) {
mItemTransformer = itemTransformer;
}
/**
* A ItemTransformer is invoked whenever a attached item is scrolled.
* This offers an opportunity for the application to apply a custom transformation
* to the item views using animation properties.
*/
public interface ItemTransformer {
/**
* Apply a property transformation to the given item.
*
* @param layoutManager Current LayoutManager
* @param item Apply the transformation to this item
* @param fraction of page relative to the current front-and-center position of the pager.
* 0 is front and center. 1 is one full
* page position to the right, and -1 is one page position to the left.
*/
void transformItem(GalleryLayoutManager layoutManager, View item, float fraction);
}
/**
* Listen for changes to the selected item
*
* @author chensuilun
*/
public interface OnItemSelectedListener {
/**
* @param recyclerView The RecyclerView which item view belong to.
* @param item The current selected view
* @param position The current selected view's position
*/
void onItemSelected(RecyclerView recyclerView, View item, int position);
}
private OnItemSelectedListener mOnItemSelectedListener;
public void setOnItemSelectedListener(OnItemSelectedListener onItemSelectedListener) {
mOnItemSelectedListener = onItemSelectedListener;
}
public void attach(RecyclerView recyclerView) {
this.attach(recyclerView, -1);
}
/**
* @param recyclerView
* @param selectedPosition
*/
public void attach(RecyclerView recyclerView, int selectedPosition) {
if (recyclerView == null) {
throw new IllegalArgumentException("The attach RecycleView must not null!!");
}
mRecyclerView = recyclerView;
mInitialSelectedPosition = Math.max(0, selectedPosition);
recyclerView.setLayoutManager(this);
mSnapHelper.attachToRecyclerView(recyclerView);
recyclerView.addOnScrollListener(mInnerScrollListener);
//SnapHelper dft implement an RecyclerView.OnFlingListener, so if there
//is only 3 item, fling from 1 to 0, the onItemSelected callback will not call.
//so we set an OnFlingListener with no process, that is ok.
RecyclerView.OnFlingListener onFlingListener = new RecyclerView.OnFlingListener() {
@Override
public boolean onFling(final int velocityX, final int velocityY) {
Log.e(TAG, "onFling: onTouchEvent 1 mState=" + mState);
return false;
}
};
recyclerView.setOnFlingListener(onFlingListener);
}
RecyclerView mRecyclerView;
public void setCallbackInFling(boolean callbackInFling) {
mCallbackInFling = callbackInFling;
}
/**
* Inner Listener to listen for changes to the selected item
*
* @author chensuilun
*/
private class InnerScrollListener extends RecyclerView.OnScrollListener {
int mState;
boolean mCallbackOnIdle;
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
View snap = mSnapHelper.findSnapView(recyclerView.getLayoutManager());
if (snap != null) {
int selectedPosition = recyclerView.getLayoutManager().getPosition(snap);
if (selectedPosition != mCurSelectedPosition) {
if (mCurSelectedView != null) {
mCurSelectedView.setSelected(false);
}
mCurSelectedView = snap;
mCurSelectedView.setSelected(true);
mCurSelectedPosition = selectedPosition;
if (!mCallbackInFling && mState != SCROLL_STATE_IDLE) {
if (BuildConfig.DEBUG) {
Log.v(TAG, "ignore selection change callback when fling ");
}
mCallbackOnIdle = true;
return;
}
if (mOnItemSelectedListener != null) {
mOnItemSelectedListener.onItemSelected(recyclerView, snap, mCurSelectedPosition);
}
}
}
if (BuildConfig.DEBUG) {
Log.v(TAG, "onScrolled: dx:" + dx + ",dy:" + dy);
}
}
@Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
super.onScrollStateChanged(recyclerView, newState);
mState = newState;
if (BuildConfig.DEBUG) {
Log.v(TAG, "onScrollStateChanged: " + newState);
}
if (mState == SCROLL_STATE_IDLE) {
View snap = mSnapHelper.findSnapView(recyclerView.getLayoutManager());
if (snap != null) {
int selectedPosition = recyclerView.getLayoutManager().getPosition(snap);
if (selectedPosition != mCurSelectedPosition) {
if (mCurSelectedView != null) {
mCurSelectedView.setSelected(false);
}
mCurSelectedView = snap;
mCurSelectedView.setSelected(true);
mCurSelectedPosition = selectedPosition;
if (mOnItemSelectedListener != null) {
mOnItemSelectedListener.onItemSelected(recyclerView, snap, mCurSelectedPosition);
}
} else if (!mCallbackInFling && mOnItemSelectedListener != null && mCallbackOnIdle) {
mCallbackOnIdle = false;
mOnItemSelectedListener.onItemSelected(recyclerView, snap, mCurSelectedPosition);
}
} else {