You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
You can create a custom Carousel strategy by extending the `CarouselStrategy`
266
-
class, and overriding the `onFirstChildMeasuredWithMargins` method. This method
267
-
is called when the RecyclerView measures the first item to be added to its
268
-
scroll container. This method must return a `KeylineState` which tells the
269
-
Carousel how to fill the scroll container with items - how many are visible at
270
-
once, what their sizes are, and where they're placed.
271
-
272
-
This is done by using the `KeylineState.Builder` to add "keylines", which
273
-
represent items that will be shown. For example, if there are 3 non-anchor
274
-
keylines, 3 items will be shown in the carousel. As the items move through the
275
-
carousel, each item will be the exact size specified by each keyline when they
276
-
are directly at the center of the keyline.
277
-
278
-
Note that anchor keylines are used to control how small items become as they
279
-
reach the edge of the carousel, and must always be added to denote the start and
280
-
ends of the keyline strategy. Smaller anchor keyline sizes will result in items
281
-
looking 'squeezed' to the edges of the carousel, whereas bigger anchor sizes
282
-
will make it look like there's no size change as it goes off-screen.
283
-
284
-
Keylines must always follow rule patterns:
285
-
286
-
- Anchor keylines must always be smaller than any other keyline (it wouldn't
287
-
make sense for an item to get bigger as it goes off-screen)
288
-
- Anchor keylines must always be off-screen; typically, the left anchor's
289
-
center is located at `-(anchorSize)/2` and the right anchor's center is
290
-
located at `availableSpace + anchorSize/2`
291
-
- The biggest keyline must be focal keylines; these are items that are fully
292
-
unmasked
293
-
- Focal keylines must be grouped together, and there must be at least one
294
-
focal keyline
295
-
- Non-anchor keylines must be in ascending size order up to the focal
296
-
keyline(s), and in descending order after the focal keyline(s)
297
-
- All non-anchor keyline sizes must add up to the available space in the
298
-
carousel
299
-
300
-
For example, if you want the following strategy structure:
301
-
302
-

303
-
304
-
Then you can define it with a `KeylineState` like below in your custom Strategy.
305
-
The `KeylineState` returned by `onFirstChildMeasuredWithMargins` should always
306
-
be the 'default' `KeylineState`, meaning the `KeylineState` with no altered
307
-
behavior. The `CarouselLayoutManager` will take care of special behavior at the
308
-
front and ends of the RecyclerView by taking the default `KeylineState` given
309
-
and altering them.
310
-
311
-
```kt
312
-
overridefunonFirstChildMeasuredWithMargins(
313
-
carousel:Carousel,
314
-
child:View,
315
-
): KeylineState? {
316
-
val availableSpace =if (carousel.isHorizontal()) carousel.containerWidth else carousel.containerHeight
317
-
val focalItemSize =200f
318
-
val mediumItemSize =160f
319
-
val smallItemSize =80f
320
-
val anchorSize =50f// the anchor size determines how small items become as they reach the edge of the carousel
321
-
val childLayoutParams = child.layoutParams asLayoutParams
322
-
val childMargins = (childLayoutParams.leftMargin + childLayoutParams.rightMargin).toFloat() // this will be top and bottom margins for vertical strategies
323
-
val anchorMaskSize =CarouselStrategy.getChildMaskPercentage(
324
-
anchorSize,
325
-
focalItemSize,
326
-
childMargins
327
-
)
328
-
val mediumMaskSize =CarouselStrategy.getChildMaskPercentage(
329
-
mediumItemSize,
330
-
focalItemSize,
331
-
childMargins
332
-
)
333
-
val smallMaskSize =CarouselStrategy.getChildMaskPercentage(
334
-
smallItemSize,
335
-
focalItemSize,
336
-
childMargins
337
-
)
338
-
val leftAnchorCenterX =-anchorSize /2f// the anchor keyline
339
-
val rightAnchorCenterX = availableSpace + anchorSize /2f
0 commit comments