Skip to content

Commit be9f16c

Browse files
hunterstichraajkumars
authored andcommitted
[Carousel] Add developer documentation
PiperOrigin-RevId: 502846546
1 parent 3b99749 commit be9f16c

File tree

2 files changed

+102
-0
lines changed

2 files changed

+102
-0
lines changed

docs/components/Carousel.md

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
<!--docs:
2+
title: "Carousel"
3+
layout: detail
4+
section: components
5+
excerpt: "Carousels contain a collection of items that can move into or out of view.
6+
iconId: carousel
7+
path: /catalog/carousel/
8+
-->
9+
10+
# Carousel
11+
12+
Carousels contain a collection of items that can move into or out of view
13+
14+
![A contained, multi-browse Carousel](assets/carousel/carousel-header.png)
15+
16+
**Contents**
17+
18+
* [Using carousel](#using-carousel)
19+
* [Multi-browse carousels](#multi-browse-carousels)
20+
* [Customizing carousel](#customizing-carousel)
21+
22+
## Using carousel
23+
24+
Before you can use Material carousels, you need to add a dependency on the
25+
Material Components for Android library. For more information, go to the
26+
[Getting started](https://github.com/material-components/material-components-android/tree/master/docs/getting-started.md)
27+
page.
28+
29+
Carousel is built on top of `RecyclerView`. To learn how to use `RecyclerView` to display a list of items, please see [Create dynamic lists with RecyclerView](https://developer.android.com/develop/ui/views/layout/recyclerview).
30+
31+
## Multi-browse carousels
32+
33+
A multi-browse carousel allows quick browsing of many small items, like a photo thumbnail gallery. A start-aligned, multi-browse carousel is the default carousel configuration.
34+
35+
To turn a horizontal list into a multi-browse carousel, first wrap your `RecyclerView`'s item layout in a `MaskableFrameLayout`. `MaskableFrameLayout` is a `FrameLayout` that is able to mask (clip) itself, and its children, to a percentage of its width. When a mask is set to 0%, the the entire view is visible in its original, "unmaksed" width. As a mask approaches 100%, the edges of the view begin to crop in towards the center, leaving a narrower and narrower sliver of the original view visible. Carousel masks and unmasks items as they are scrolled across the viewport to create a stylized look and feel.
36+
37+
```xml
38+
<com.google.android.material.carousel.MaskableFrameLayout
39+
xmlns:android="http://schemas.android.com/apk/res/android"
40+
xmlns:app="http://schemas.android.com/apk/res-auto"
41+
xmlns:tools="http://schemas.android.com/tools"
42+
android:id="@+id/carousel_item_container"
43+
android:layout_width="150dp"
44+
android:layout_height="match_parent"
45+
android:layout_marginStart="4dp"
46+
android:layout_marginEnd="4dp"
47+
android:foreground="?attr/selectableItemBackground"
48+
app:shapeAppearance="?attr/shapeAppearanceCornerExtraLarge">
49+
<ImageView
50+
android:id="@+id/carousel_image_view"
51+
android:layout_width="match_parent"
52+
android:layout_height="match_parent"
53+
android:scaleType="centerCrop"/>
54+
</com.google.android.material.carousel.MaskableFrameLayout>
55+
```
56+
57+
**Note:** Masking creates the best effect when `MaskableFrameLayout` contains a full-bleed image or other backgrounds that extend to or past the edges of its parent. If the shape or masking behavior of your item doesn't look correct, try removing any padding set on `MaskableFrameLayout` or margins set on children of `MaskableFrameLayout`.
58+
59+
Next, set your `RecyclerView`s layout manager to a new `CarouselLayoutManager`.
60+
61+
```xml
62+
<androidx.recyclerview.widget.RecyclerView
63+
android:id="@+id/carousel_recycler_view"
64+
android:layout_width="match_parent"
65+
android:layout_height="196dp"
66+
android:clipChildren="false"
67+
android:clipToPadding="false" />
68+
```
69+
70+
```kotlin
71+
carouselRecyclerView.setLayoutManager(CarouselLayoutManager())
72+
```
73+
74+
These are the basic steps to create a carousel with large items at the start of the list followed by medium and small items, depending on the size of the `RecyclerView` container.
75+
76+
## Customizing carousel
77+
78+
### Item size
79+
80+
The main means of changing the look of carousel is by setting the height of your `RecyclerView` and width of your item's `MaskableFrameLayout`. The width set in the item layout is used by `CarouselLayoutManager` to determine the size items should be when they are fully unmasked. This width needs to be set to a specific dp value and cannot be set to `wrap_content`. `CarouselLayoutManager` tries to then use a size as close to your item layout's specified width as possible but may increase or decrease this size depending on the `RecyclerView`'s available space. This is needed to create a pleasing arrangement of items which fit within the `RecyclerView`'s bounds. Additionally, `CarouselLayoutManager` will only read and use the width set on the first list item. All remaining items will be laid out using this first item's width.
81+
82+
### Item shape
83+
84+
`MaskableFrameLayout` takes an `app:shapeAppearance` attribute to determine its corner radius. It's recommended to use the `?attr/shapeAppearanceExtraLarge` shape attribute but this can be set to any `ShapeAppearance` theme attribute or style. See [Shape theming](https://github.com/material-components/material-components-android/tree/master/docs/theming/Shape.md) documentation for more details.
85+
86+
### Reacting to changes in item mask size
87+
88+
If your `RecyclerView`'s item layout contains text or other content that needs to react to changes in the item's mask, you can listen for changes in mask size by setting an `onMaskChangedListener` on your `MaskableFrameLayout` inside your `RecyclerView.ViewHolder`.
89+
90+
```kotlin
91+
(viewHolder.itemView as MaskableFrameLayout).addOnMaskChangedListener(
92+
maskRect ->
93+
// Any custom motion to run when mask size changes
94+
viewHolder.title.setTranslationX(maskRect.left);
95+
viewHolder.title.setAlpha(lerp(1F, 0F, 0F, 80F, maskRect.left));
96+
);
97+
}
98+
```
99+
100+
In the example above, a title is translated so it appears pinned to the left masking edge of the item. As the item masks and becomes too small for the title, it is faded out.
101+
102+
173 KB
Loading

0 commit comments

Comments
 (0)