旋转木马
一个用于显示可滚动项目集合的 Jetpack Compose 轮播组件。
Expo UI 轮播组件与官方 Jetpack Compose 轮播 API 相匹配,并显示一个可水平滚动的项目集合,具有可配置的大小和快速滑动行为。
🌐 Expo UI Carousel matches the official Jetpack Compose Carousel API and displays a horizontally scrollable collection of items with configurable sizing and fling behavior.
安装
🌐 Installation
- npx expo install @expo/uiIf you are installing this in an existing React Native app, make sure to install expo in your project.
用法
🌐 Usage
基础轮播
🌐 Basic carousel
import { Host, Carousel, Box, Text } from '@expo/ui/jetpack-compose'; import { size, background } from '@expo/ui/jetpack-compose/modifiers'; export default function BasicCarouselExample() { const items = [ { label: 'Item 1', color: '#6200EE' }, { label: 'Item 2', color: '#03DAC5' }, { label: 'Item 3', color: '#FF5722' }, { label: 'Item 4', color: '#4CAF50' }, { label: 'Item 5', color: '#2196F3' }, ]; return ( <Host style={{ height: 200 }}> <Carousel> {items.map(item => ( <Box key={item.label} contentAlignment="center" modifiers={[size(200, 180), background(item.color)]}> <Text color="#FFFFFF" style={{ fontSize: 18 }}> {item.label} </Text> </Box> ))} </Carousel> </Host> ); }
多浏览器版本
🌐 MultiBrowse variant
multiBrowse 变体显示一个大项目以及较小的预览项目,让用户可以看到接下来的内容。
🌐 The multiBrowse variant shows a large item alongside smaller peek items, letting users see what comes next.
import { Host, Carousel, Box, Text } from '@expo/ui/jetpack-compose'; import { size, background } from '@expo/ui/jetpack-compose/modifiers'; export default function MultiBrowseCarouselExample() { const colors = ['#6200EE', '#03DAC5', '#FF5722', '#4CAF50', '#2196F3']; return ( <Host style={{ height: 200 }}> <Carousel variant="multiBrowse" preferredItemWidth={280} minSmallItemWidth={40} maxSmallItemWidth={80} itemSpacing={8} flingBehavior="singleAdvance"> {colors.map((color, index) => ( <Box key={index} contentAlignment="center" modifiers={[size(200, 180), background(color)]}> <Text color="#FFFFFF" style={{ fontSize: 18 }}> Card {index + 1} </Text> </Box> ))} </Carousel> </Host> ); }
未受约束的变体
🌐 Unconstrained variant
unconstrained 变体为每个项目提供固定宽度而不具有吸附行为,允许自由滚动。
🌐 The unconstrained variant gives each item a fixed width without snapping behavior, allowing free-form scrolling.
import { Host, Carousel, Box, Text } from '@expo/ui/jetpack-compose'; import { size, background } from '@expo/ui/jetpack-compose/modifiers'; export default function UnconstrainedCarouselExample() { const items = ['Photo 1', 'Photo 2', 'Photo 3', 'Photo 4', 'Photo 5']; return ( <Host style={{ height: 200 }}> <Carousel variant="unconstrained" itemWidth={160} itemSpacing={12} contentPadding={{ start: 16, top: 0, end: 16, bottom: 0 }} flingBehavior="noSnap"> {items.map(item => ( <Box key={item} contentAlignment="center" modifiers={[size(160, 180), background('#3F51B5')]}> <Text color="#FFFFFF" style={{ fontSize: 16 }}> {item} </Text> </Box> ))} </Carousel> </Host> ); }
应用接口
🌐 API
import { Carousel } from '@expo/ui/jetpack-compose';
Components
Type: React.Element<CarouselProps>
unionPadding for carousel content (dp or object)
Acceptable values are: number | PaddingValuesRecord
Type: React.Element<CarouselProps>