@@ -66,11 +66,6 @@ open class SpannedGridLayoutManager(
66
66
* by the number of columns in the layout, but you can set your own dimension with [customWidth].
67
67
*/
68
68
val itemWidth: Int get() = if (customWidth > 0 ) customWidth else ceil(decoratedWidth.toFloat() / columnCount.toFloat()).toInt()
69
- /* *
70
- * The height of each item. Normally this is the height of the RecyclerView, divided
71
- * by the number of rows in the layout, but you can set your own dimension with [customHeight].
72
- */
73
- val itemHeight: Int get() = if (customHeight > 0 ) customHeight else ceil(decoratedHeight.toFloat() / rowCount.toFloat()).toInt()
74
69
75
70
/* *
76
71
* Delegate some orientation-specific logic.
@@ -387,8 +382,8 @@ open class SpannedGridLayoutManager(
387
382
* TODO: Maybe add a new function to override instead.
388
383
*/
389
384
open class SpanSizeLookup (
390
- /* * Used to provide an SpanSize for each item. */
391
- private var lookupFunction : ((Int ) -> SpanSize )? = null
385
+ /* * Used to provide an SpanSize for each item. */
386
+ private var lookupFunction : ((Int ) -> SpanSize )? = null
392
387
) {
393
388
394
389
/* *
@@ -420,8 +415,8 @@ open class SpannedGridLayoutManager(
420
415
421
416
override fun generateDefaultLayoutParams (): RecyclerView .LayoutParams {
422
417
return RecyclerView .LayoutParams (
423
- RecyclerView .LayoutParams .MATCH_PARENT ,
424
- RecyclerView .LayoutParams .WRAP_CONTENT
418
+ RecyclerView .LayoutParams .MATCH_PARENT ,
419
+ RecyclerView .LayoutParams .WRAP_CONTENT
425
420
)
426
421
}
427
422
@@ -489,8 +484,6 @@ open class SpannedGridLayoutManager(
489
484
fillGap(Direction .END , recycler, state)
490
485
fillGap(Direction .START , recycler, state)
491
486
492
- recycleChildrenOutOfBounds(Direction .END , recycler)
493
- recycleChildrenOutOfBounds(Direction .START , recycler)
494
487
}
495
488
496
489
/* *
@@ -511,8 +504,8 @@ open class SpannedGridLayoutManager(
511
504
// Multiply the rect for item width and height to get positions
512
505
val left = rect.left * itemWidth
513
506
val right = rect.right * itemWidth
514
- val top = rect.top * itemHeight
515
- val bottom = rect.bottom * itemHeight
507
+ val top = rect.top * itemWidth
508
+ val bottom = rect.bottom * itemWidth
516
509
517
510
val insetsRect = Rect ()
518
511
calculateItemDecorationsForChild(view, insetsRect)
@@ -588,103 +581,11 @@ open class SpannedGridLayoutManager(
588
581
protected open fun updateEdgesWithNewChild (view : View ) {
589
582
val childStart = getChildStart(view) + scroll + getPaddingStartForOrientation()
590
583
591
- if (childStart < layoutStart) {
592
- layoutStart = childStart
593
- }
584
+ if (childStart < layoutStart) layoutStart = childStart
594
585
595
586
val newLayoutEnd = childStart + getItemSizeForOrientation()
596
587
597
- if (newLayoutEnd > layoutEnd) {
598
- layoutEnd = newLayoutEnd
599
- }
600
- }
601
-
602
- // ==============================================================================================
603
- // ~ Recycling methods
604
- // ==============================================================================================
605
-
606
- /* *
607
- * Recycle any views that are out of bounds
608
- */
609
- protected open fun recycleChildrenOutOfBounds (direction : Direction , recycler : RecyclerView .Recycler ) {
610
- // No more lag.
611
- /* *
612
- if (direction == Direction.END) {
613
- recycleChildrenFromStart(direction, recycler)
614
- } else {
615
- recycleChildrenFromEnd(direction, recycler)
616
- }
617
- */
618
- }
619
-
620
- /* *
621
- * Recycle views from start to first visible item
622
- */
623
- protected open fun recycleChildrenFromStart (direction : Direction , recycler : RecyclerView .Recycler ) {
624
- // No more lag.
625
- /* *
626
- val childCount = childCount
627
- val start = getPaddingStartForOrientation()
628
-
629
- val toDetach = mutableListOf<View>()
630
-
631
- for (i in 0 until childCount) {
632
- getChildAt(i)?.let { child ->
633
- val childEnd = getChildEnd(child)
634
-
635
- if (childEnd < start) {
636
- toDetach.add(child)
637
- }
638
- }
639
-
640
- }
641
-
642
- for (child in toDetach) {
643
- removeAndRecycleView(child, recycler)
644
- updateEdgesWithRemovedChild(child, direction)
645
- } */
646
- }
647
-
648
- /* *
649
- * Recycle views from end to last visible item
650
- */
651
- protected open fun recycleChildrenFromEnd (direction : Direction , recycler : RecyclerView .Recycler ) {
652
- val childCount = childCount
653
- val end = size + getPaddingEndForOrientation()
654
-
655
- val toDetach = mutableListOf<View >()
656
-
657
- for (i in (0 until childCount).reversed()) {
658
- getChildAt(i)?.let { child ->
659
- val childStart = getChildStart(child)
660
-
661
- if (childStart > end) {
662
- toDetach.add(child)
663
- }
664
- }
665
- }
666
-
667
- for (child in toDetach) {
668
- removeAndRecycleView(child, recycler)
669
- updateEdgesWithRemovedChild(child, direction)
670
- }
671
- }
672
-
673
- /* *
674
- * Update layout edges when views are recycled
675
- */
676
- protected open fun updateEdgesWithRemovedChild (view : View , direction : Direction ) {
677
- // No more lag.
678
- /* *
679
- val childStart = getChildStart(view) + scroll
680
- val childEnd = getChildEnd(view) + scroll
681
-
682
- if (direction == Direction.END) { // Removed from start
683
- layoutStart = getPaddingStartForOrientation() + childEnd
684
- } else if (direction == Direction.START) { // Removed from end
685
- layoutEnd = getPaddingStartForOrientation() + childStart
686
- }
687
- **/
588
+ if (newLayoutEnd > layoutEnd) layoutEnd = newLayoutEnd
688
589
}
689
590
690
591
override fun computeVerticalScrollOffset (state : RecyclerView .State ): Int {
@@ -747,9 +648,7 @@ open class SpannedGridLayoutManager(
747
648
748
649
protected open fun scrollBy (delta : Int , recycler : RecyclerView .Recycler , state : RecyclerView .State ): Int {
749
650
// If there are no view or no movement, return
750
- if (delta == 0 ) {
751
- return 0
752
- }
651
+ if (delta == 0 ) return 0
753
652
754
653
val (childEnd, _, _) = getGreatestChildEnd()
755
654
@@ -761,15 +660,11 @@ open class SpannedGridLayoutManager(
761
660
(size) < (childEnd) &&
762
661
delta > 0
763
662
// If can't scroll forward or backwards, return
764
- if (! (canScrollBackwards || canScrollForward)) {
765
- return 0
766
- }
663
+ if (! (canScrollBackwards || canScrollForward)) return 0
767
664
768
665
val correctedDistance = scrollBy(- delta, state, childEnd)
769
666
val direction = if (delta > 0 ) Direction .END else Direction .START
770
667
771
- recycleChildrenOutOfBounds(direction, recycler)
772
-
773
668
fillGap(direction, recycler, state)
774
669
775
670
return - correctedDistance
@@ -808,9 +703,7 @@ open class SpannedGridLayoutManager(
808
703
pendingScrollToPosition = position.coerceAtMost(itemCount - 1 )
809
704
.coerceAtLeast(0 )
810
705
811
- if (wasNull && recyclerView?.isInLayout == false ) {
812
- requestLayout()
813
- }
706
+ if (wasNull && recyclerView?.isInLayout == false ) requestLayout()
814
707
}
815
708
816
709
/* *
@@ -1062,11 +955,7 @@ open class SpannedGridLayoutManager(
1062
955
}
1063
956
1064
957
protected open fun getItemSizeForOrientation (): Int {
1065
- return if (orientation == VERTICAL ) {
1066
- itemHeight
1067
- } else {
1068
- itemWidth
1069
- }
958
+ return itemWidth
1070
959
}
1071
960
1072
961
open fun getSpanCountForOrientation (): Int {
@@ -1174,41 +1063,25 @@ open class RectsHelper(private val layoutManager: SpannedGridLayoutManager,
1174
1063
* Start row/column for free rects
1175
1064
*/
1176
1065
val start: Int get() {
1177
- return if (orientation == VERTICAL ) {
1178
- freeRects[0 ].top * layoutManager.itemHeight
1179
- } else {
1180
- freeRects[0 ].left * layoutManager.itemWidth
1181
- }
1066
+ return freeRects[0 ].left * layoutManager.itemWidth
1182
1067
}
1183
1068
1184
1069
/* *
1185
1070
* End row/column for free rects
1186
1071
*/
1187
1072
val end: Int get() {
1188
- return if (orientation == VERTICAL ) {
1189
- (freeRects.last().bottom + 1 ) * layoutManager.itemHeight
1190
- } else {
1191
- (freeRects.last().right + 1 ) * layoutManager.itemWidth
1192
- }
1073
+ return (freeRects.last().right + 1 ) * layoutManager.itemWidth
1193
1074
}
1194
1075
1195
1076
val lastStart: Int get() {
1196
- return if (orientation == VERTICAL ) {
1197
- (freeRects.last().top) * layoutManager.itemHeight
1198
- } else {
1199
- (freeRects.last().left) * layoutManager.itemWidth
1200
- }
1077
+ return (freeRects.last().left) * layoutManager.itemWidth
1201
1078
}
1202
1079
1203
1080
val largestStart: Int get() {
1204
1081
var largest = 0
1205
1082
1206
1083
freeRects.forEach {
1207
- val s = if (orientation == VERTICAL ) {
1208
- it.top * layoutManager.itemHeight
1209
- } else {
1210
- it.left * layoutManager.itemWidth
1211
- }
1084
+ val s = it.left * layoutManager.itemWidth
1212
1085
1213
1086
if (s > largest) {
1214
1087
largest = s
@@ -1222,12 +1095,7 @@ open class RectsHelper(private val layoutManager: SpannedGridLayoutManager,
1222
1095
var largest = 0
1223
1096
1224
1097
freeRects.forEach {
1225
- val s = if (orientation == VERTICAL ) {
1226
- it.bottom * layoutManager.itemHeight
1227
- } else {
1228
- it.right * layoutManager.itemWidth
1229
- }
1230
-
1098
+ val s = it.right * layoutManager.itemWidth
1231
1099
if (s > largest) {
1232
1100
largest = s
1233
1101
}
@@ -1261,19 +1129,11 @@ open class RectsHelper(private val layoutManager: SpannedGridLayoutManager,
1261
1129
}
1262
1130
1263
1131
fun getEndForPosition (position : Int ): Int {
1264
- return if (orientation == VERTICAL ) {
1265
- (rectsCache[position]!! .bottom) * layoutManager.itemHeight
1266
- } else {
1267
- (rectsCache[position]!! .right) * layoutManager.itemWidth
1268
- }
1132
+ return (rectsCache[position]!! .right) * layoutManager.itemWidth
1269
1133
}
1270
1134
1271
1135
fun getStartForPosition (position : Int ): Int {
1272
- return if (orientation == VERTICAL ) {
1273
- (rectsCache[position]!! .top) * layoutManager.itemHeight
1274
- } else {
1275
- (rectsCache[position]!! .left) * layoutManager.itemWidth
1276
- }
1136
+ return (rectsCache[position]!! .left) * layoutManager.itemWidth
1277
1137
}
1278
1138
1279
1139
/* *
@@ -1336,21 +1196,13 @@ open class RectsHelper(private val layoutManager: SpannedGridLayoutManager,
1336
1196
} else {
1337
1197
freeRects.remove(free)
1338
1198
1339
- if (free.left < subtractedRect.left) { // Left
1340
- possibleNewRects.add(Rect (free.left, free.top, subtractedRect.left, free.bottom))
1341
- }
1199
+ if (free.left < subtractedRect.left) possibleNewRects.add(Rect (free.left, free.top, subtractedRect.left, free.bottom))
1342
1200
1343
- if (free.right > subtractedRect.right) { // Right
1344
- possibleNewRects.add(Rect (subtractedRect.right, free.top, free.right, free.bottom))
1345
- }
1201
+ if (free.right > subtractedRect.right) possibleNewRects.add(Rect (subtractedRect.right, free.top, free.right, free.bottom))
1346
1202
1347
- if (free.top < subtractedRect.top) { // Top
1348
- possibleNewRects.add(Rect (free.left, free.top, free.right, subtractedRect.top))
1349
- }
1203
+ if (free.top < subtractedRect.top) possibleNewRects.add(Rect (free.left, free.top, free.right, subtractedRect.top))
1350
1204
1351
- if (free.bottom > subtractedRect.bottom) { // Bottom
1352
- possibleNewRects.add(Rect (free.left, subtractedRect.bottom, free.right, free.bottom))
1353
- }
1205
+ if (free.bottom > subtractedRect.bottom) possibleNewRects.add(Rect (free.left, subtractedRect.bottom, free.right, free.bottom))
1354
1206
}
1355
1207
}
1356
1208
0 commit comments