34
34
* A {@link CarouselStrategy} that knows how to size and fit one large item and one small item into
35
35
* a container to create a layout to browse one 'hero' item at a time with a preview item.
36
36
*
37
- * <p>Note that this strategy resizes Carousel items to take up the full width of the Carousel, save
38
- * room for the small item.
37
+ * <p>Note that this strategy resizes Carousel items to take up the full width or height of the
38
+ * Carousel, save room for the small item.
39
39
*
40
40
* <p>This class will automatically be reversed by {@link CarouselLayoutManager} if being laid out
41
41
* right-to-left and does not need to make any account for layout direction itself.
@@ -48,52 +48,56 @@ public class HeroCarouselStrategy extends CarouselStrategy {
48
48
@ Override
49
49
@ NonNull
50
50
KeylineState onFirstChildMeasuredWithMargins (@ NonNull Carousel carousel , @ NonNull View child ) {
51
- float availableSpace = carousel .getContainerWidth ();
51
+ int availableSpace = carousel .getContainerHeight ();
52
+ if (carousel .isHorizontal ()) {
53
+ availableSpace = carousel .getContainerWidth ();
54
+ }
52
55
53
56
LayoutParams childLayoutParams = (LayoutParams ) child .getLayoutParams ();
54
- float childHorizontalMargins = childLayoutParams .leftMargin + childLayoutParams .rightMargin ;
57
+ float childMargins = childLayoutParams .topMargin + childLayoutParams .bottomMargin ;
58
+
59
+ float measuredChildSize = child .getMeasuredWidth () * 2 ;
55
60
56
- float smallChildWidthMin = getSmallSizeMin (child .getContext ()) + childHorizontalMargins ;
57
- float smallChildWidthMax = getSmallSizeMax (child .getContext ()) + childHorizontalMargins ;
61
+ if (carousel .isHorizontal ()) {
62
+ childMargins = childLayoutParams .leftMargin + childLayoutParams .rightMargin ;
63
+ measuredChildSize = child .getMeasuredHeight () * 2 ;
64
+ }
58
65
59
- float measuredChildHeight = child .getMeasuredHeight () ;
60
- float measuredChildWidth = measuredChildHeight * 2 ;
66
+ float smallChildSizeMin = getSmallSizeMin ( child .getContext ()) + childMargins ;
67
+ float smallChildSizeMax = getSmallSizeMax ( child . getContext ()) + childMargins ;
61
68
62
- float targetLargeChildWidth = min (measuredChildWidth + childHorizontalMargins , availableSpace );
69
+ float targetLargeChildSize = min (measuredChildSize + childMargins , availableSpace );
63
70
// Ideally we would like to create a balanced arrangement where a small item is 1/3 the size of
64
71
// the large item. Clamp the small target size within our min-max range and as close to 1/3 of
65
72
// the target large item size as possible.
66
- float targetSmallChildWidth =
73
+ float targetSmallChildSize =
67
74
MathUtils .clamp (
68
- measuredChildWidth / 3F + childHorizontalMargins ,
69
- getSmallSizeMin (child .getContext ()) + childHorizontalMargins ,
70
- getSmallSizeMax (child .getContext ()) + childHorizontalMargins );
71
- float targetMediumChildWidth = (targetLargeChildWidth + targetSmallChildWidth ) / 2F ;
75
+ measuredChildSize / 3F + childMargins ,
76
+ getSmallSizeMin (child .getContext ()) + childMargins ,
77
+ getSmallSizeMax (child .getContext ()) + childMargins );
78
+ float targetMediumChildSize = (targetLargeChildSize + targetSmallChildSize ) / 2F ;
72
79
73
80
// Find the minimum space left for large items after filling the carousel with the most
74
81
// permissible small items to determine a plausible minimum large count.
75
- float minAvailableLargeSpace =
76
- availableSpace
77
- - (smallChildWidthMax * maxValue (SMALL_COUNTS ));
78
- int largeCountMin = (int ) max (1 , floor (minAvailableLargeSpace / targetLargeChildWidth ));
79
- int largeCountMax = (int ) ceil (availableSpace / targetLargeChildWidth );
82
+ float minAvailableLargeSpace = availableSpace - (smallChildSizeMax * maxValue (SMALL_COUNTS ));
83
+ int largeCountMin = (int ) max (1 , floor (minAvailableLargeSpace / targetLargeChildSize ));
84
+ int largeCountMax = (int ) ceil (availableSpace / targetLargeChildSize );
80
85
int [] largeCounts = new int [largeCountMax - largeCountMin + 1 ];
81
86
for (int i = 0 ; i < largeCounts .length ; i ++) {
82
87
largeCounts [i ] = largeCountMin + i ;
83
88
}
84
-
85
89
Arrangement arrangement = Arrangement .findLowestCostArrangement (
86
- availableSpace ,
87
- targetSmallChildWidth ,
88
- smallChildWidthMin ,
89
- smallChildWidthMax ,
90
- SMALL_COUNTS ,
91
- targetMediumChildWidth ,
92
- MEDIUM_COUNTS ,
93
- targetLargeChildWidth ,
94
- largeCounts );
90
+ availableSpace ,
91
+ targetSmallChildSize ,
92
+ smallChildSizeMin ,
93
+ smallChildSizeMax ,
94
+ SMALL_COUNTS ,
95
+ targetMediumChildSize ,
96
+ MEDIUM_COUNTS ,
97
+ targetLargeChildSize ,
98
+ largeCounts );
95
99
return createLeftAlignedKeylineState (
96
- child .getContext (), childHorizontalMargins , availableSpace , arrangement );
100
+ child .getContext (), childMargins , availableSpace , arrangement );
97
101
}
98
102
}
99
103
0 commit comments