注意: Carousel 是一个可水平滚动的组件,因此父级 Host 必须在滚动轴上提供一个有限宽度。请结合 style={{ width: '100%' }} 使用 matchContents={{ vertical: true }}(或任何有限宽度)。有关详细信息,请参见 Host 参考中的 Match contents。
import { Host, HorizontalCenteredHeroCarousel, Box, Text } from '@expo/ui/jetpack-compose';
import { size, background } from '@expo/ui/jetpack-compose/modifiers';
export default function CenteredHeroExample() {
const colors = ['#6200EE', '#03DAC5', '#FF5722', '#4CAF50', '#2196F3'];
return (
<Host matchContents={{ vertical: true }} style={{ width: '100%' }}>
<HorizontalCenteredHeroCarousel itemSpacing={8}>
{colors.map((color, index) => (
<Box
key={index}
contentAlignment="center"
modifiers={[size(300, 200), background(color)]}>
<Text color="#FFFFFF">幻灯片 {index + 1}</Text>
</Box>
))}
</HorizontalCenteredHeroCarousel>
</Host>
);
}
import { Host, HorizontalMultiBrowseCarousel, Box, Text } from '@expo/ui/jetpack-compose';
import { size, background } from '@expo/ui/jetpack-compose/modifiers';
export default function MultiBrowseExample() {
const colors = ['#6200EE', '#03DAC5', '#FF5722', '#4CAF50', '#2196F3'];
return (
<Host matchContents={{ vertical: true }} style={{ width: '100%' }}>
<HorizontalMultiBrowseCarousel
preferredItemWidth={200}
itemSpacing={8}
flingBehavior="singleAdvance">
{colors.map((color, index) => (
<Box
key={index}
contentAlignment="center"
modifiers={[size(200, 180), background(color)]}>
<Text color="#FFFFFF">卡片 {index + 1}</Text>
</Box>
))}
</HorizontalMultiBrowseCarousel>
</Host>
);
}
import { Host, HorizontalUncontainedCarousel, Box, Text } from '@expo/ui/jetpack-compose';
import { size, background } from '@expo/ui/jetpack-compose/modifiers';
export default function UncontainedExample() {
const items = ['照片 1', '照片 2', '照片 3', '照片 4', '照片 5'];
return (
<Host matchContents={{ vertical: true }} style={{ width: '100%' }}>
<HorizontalUncontainedCarousel
itemWidth={160}
itemSpacing={12}
contentPadding={{ start: 16, top: 0, end: 16, bottom: 0 }}>
{items.map(item => (
<Box
key={item}
contentAlignment="center"
modifiers={[size(160, 180), background('#3F51B5')]}>
<Text color="#FFFFFF">{item}</Text>
</Box>
))}
</HorizontalUncontainedCarousel>
</Host>
);
}
import {
HorizontalCenteredHeroCarousel,
HorizontalMultiBrowseCarousel,
HorizontalUncontainedCarousel,
} from '@expo/ui/jetpack-compose';
HorizontalCenteredHeroCarousel
Type: React.Element<ComponentType<HorizontalCenteredHeroCarouselProps>>
A hero carousel that centers one large item between two small peek items,
matching Compose's HorizontalCenteredHeroCarousel.
HorizontalCenteredHeroCarouselProps
Optional • Type: number
Maximum width of the hero item in dp.
When unspecified, the hero item will be as wide as possible.
Optional • Type: number • Default: CarouselDefaults.MaxSmallItemSize
Maximum width of small peek items in dp.
Optional • Type: number • Default: CarouselDefaults.MinSmallItemSize
Minimum width of small peek items in dp.
HorizontalMultiBrowseCarousel
Type: React.Element<ComponentType<HorizontalMultiBrowseCarouselProps>>
A carousel that shows a large item alongside smaller peek items,
matching Compose's HorizontalMultiBrowseCarousel.
HorizontalMultiBrowseCarouselProps
Optional • Type: number • Default: CarouselDefaults.MaxSmallItemSize
Maximum width of small peek items in dp.
Optional • Type: number • Default: CarouselDefaults.MinSmallItemSize
Minimum width of small peek items in dp.
Type: number
The preferred width of the large item in dp.
HorizontalUncontainedCarousel
Type: React.Element<ComponentType<HorizontalUncontainedCarouselProps>>
A carousel where each item has a fixed width with free-form scrolling,
matching Compose's HorizontalUncontainedCarousel.
HorizontalUncontainedCarouselProps
Type: number
The width of each item in dp.
Shared props across all carousel components.
Literal type: string
Fling behavior type for controlling carousel snapping.
Acceptable values are: 'singleAdvance' | 'noSnap'
Per-side padding values in dp for the content.