【问题标题】:react-native-snap-carousel full screen + passing contentreact-native-snap-carousel 全屏 + 传递内容
【发布时间】:2021-08-20 18:39:16
【问题描述】:

我有一个两部分的问题。我目前正在使用react-native-snap-carousel,在这里找到:https://github.com/meliorence/react-native-snap-carousel

我的代码如下所示:

    carouselItems: [
        {
            image: require('../assets/images/placeholder-slide.png'),
        },
        {
            image: require('../assets/images/slide2.png'),
        },
        {
            //I want to pass <View> here with content and a button
        },
    ]

_renderItem({ item, index }) {
    <View>
        <Image source={item.image} />
    </View>
}
    
    <SafeAreaView style={styles.body}>
        <Carousel
            layout={"default"}
            ref={ref => this.carousel = ref}
            data={this.state.carouselItems}
            sliderWidth={phoneWidth}
            itemWidth={phoneWidth}
            renderItem={this._renderItem}
            inactiveSlideOpacity={1}
            inactiveSlideScale={1}
            slideStyle={{ width: phoneWidth }}
            onSnapToItem={index => this.setState({ activeIndex: index })} />   
    
    
        //This is the block I want to pass in the 3rd slide of the carousel 
        <View>
            <Text>Some Text Here</Text>
            <button>Button here</button>
        </View>
    </SafeAreaView>

body: {
    flex: 1,
    position: 'relative',
},

所以轮播中的前两项是全角图像,最后一张幻灯片将包含内容和 2 个按钮。如何将&lt;View&gt; 中的项目传递到carouselItems

我的第二个问题是如何使轮播全屏显示。这是我的模拟器的截图:https://ibb.co/ZNZKRqm

如您所见,旋转木马上方和下方都有灰色区域。我不相信这与轮播有关,我想也许默认情况下这是全宽的?但我也看到应用程序也针对这个灰色空间。

【问题讨论】:

  • 你的_renderItem看起来怎么样?
  • @Bojke 谢谢你的收获,我更新了

标签: reactjs react-native


【解决方案1】:
  1. 部分
carouselItems: [
        {
            image: require('../assets/images/placeholder-slide.png'),
        },
        {
            image: require('../assets/images/slide2.png'),
        },
        {
            content:
<View>
            <Text>Some Text Here</Text>
            <button>Button here</button>
        </View>
        },
    ]

_renderItem({ item, index }) {
//return item.content if exists
   return item?.content || <View> 
        <Image source={item.image} />
    </View>
}
    
    <SafeAreaView style={styles.body}>
        <Carousel
            layout={"default"}
            ref={ref => this.carousel = ref}
            data={this.state.carouselItems}
            sliderWidth={phoneWidth}
            itemWidth={phoneWidth}
            renderItem={this._renderItem}
            inactiveSlideOpacity={1}
            inactiveSlideScale={1}
            slideStyle={{ width: phoneWidth }}
            onSnapToItem={index => this.setState({ activeIndex: index })} />   
    </SafeAreaView>
  1. 部分 灰色空间是因为SafeAreaView。这只会出现在带有缺口的设备上。您可以使用 style 属性为其设置自定义背景颜色。

【讨论】:

  • 谢谢,我会在大约半小时后尝试第 1 部分。如果一切顺利,我会竖起大拇指!至于2。有没有办法让轮播物品泄漏到那个区域。所以它不是该区域的背景颜色,而是轮播图像?
  • 我认为如果您取出 SafeAreView,它的行为会像这样。并且可以用它包裹3.screen,否则文本组件将不可见。
猜你喜欢
  • 2020-03-18
  • 2019-01-27
  • 2020-01-07
  • 2020-02-05
  • 2021-01-22
  • 2018-03-06
  • 2020-10-20
  • 1970-01-01
  • 2021-08-25
相关资源
最近更新 更多