【问题标题】:How can i display the following output in react native snap carousel我如何在反应原生快照轮播中显示以下输出
【发布时间】:2020-12-02 19:17:40
【问题描述】:

如何在 react native snap carousel 中显示以下输出

  <Carousel
                autoplayInterval={4000}
                // slideStyle={{ flex: 1,
                // marginRight:10}}
                ref={ (c) => { this._carousel = c; } }
                data={this.state.featuredBusinessData}
                scrollInterpolator={stackScrollInterpolator}
                slideInterpolatedStyle={stackAnimatedStyles}
                useScrollView={true}
                renderItem={this._renderItemFeaturedBusiness.bind(this)}
                onSnapToItem={this.handleSnapToItem.bind(this)}
                sliderWidth={deviceWidth}
                // itemWidth={deviceWidth/2.5 }
                itemWidth={deviceWidth/3 }
                layout={'default'}
                // loopClonesPerSide={this.state.videos.length-1}
                firstItem={2}
                autoplay={true}
                loop={true}
                useScrollView={true}
                enableMomentum={true}
                lockScrollWhileSnapping={false}
                
                />

由于我是 RN 新手,请任何人告诉我如何在 react native snap carousel 中显示以下输出

【问题讨论】:

标签: reactjs react-native react-native-android carousel react-native-snap-carousel


【解决方案1】:

输出:

@Waheed Akhtar 为您提供了完美的建议,但如果您在实施中仍有一些困难,这里是完整的工作示例:


import * as React from 'react';
import { Text, View, StyleSheet, FlatList, Image } from 'react-native';
import Constants from 'expo-constants';

// You can import from local files
import AssetExample from './components/AssetExample';

// or any pure javascript modules available in npm
import { Card } from 'react-native-paper';

export default function App() {
  const videos = [
    {
      id: 'WpIAc9by5iU',
      thumbnail: 'https://source.unsplash.com/random',
      title: 'Led Zeppelin - Stairway To Heaven',
    },
    {
      id: 'sNPnbI1arSE',
      thumbnail: 'https://img.youtube.com/vi/sNPnbI1arSE/hqdefault.jpg',
      title: 'Eminem - My Name Is',
    },
    {
      id: 'VOgFZfRVaww',
      thumbnail: 'https://img.youtube.com/vi/VOgFZfRVaww/hqdefault.jpg',
      title: 'some title',
    },{
      id: 'WpIAc9by5iU',
      thumbnail: 'https://source.unsplash.com/random',
      title: 'Led Zeppelin - Stairway To Heaven',
    },
    {
      id: 'sNPnbI1arSE',
      thumbnail: 'https://source.unsplash.com/random',
      title: 'Eminem - My Name Is',
    },
    {
      id: 'VOgFZfRVaww',
      thumbnail: 'https://img.youtube.com/vi/VOgFZfRVaww/hqdefault.jpg',
      title: 'some title',
    },
  ];
  return (
    <View style={styles.container}>
      <FlatList
        horizontal
        data={videos}
        renderItem={({ item }) => (
          <View style={styles.card_template}>
        <Image
          style={styles.card_image}
          source={{
            uri:
              item.thumbnail,
          }}
        />
        <View style={styles.text_container}>
          <Text style={styles.card_title}>{item.title.toUpperCase().substr(0, 10)+"..."}</Text>
          <Text style={styles.card_subtitle}>{item.id}</Text>
        </View>
        
      </View>
        )}
      />
    </View>
  );
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: 'center',
    paddingTop: Constants.statusBarHeight,
    backgroundColor: '#ecf0f1',
    padding: 8,
  },
  card_template: {
    width: 150,
    height: 150,
    marginLeft: 10,
    marginTop: 20,
    boxShadow: '20px 20px 17px -12px rgba(0,0,0,0.75)',
    borderRadius: 10,
  },
  card_image: {
    width: 150,
    height: 150,
    borderRadius: 10,
  },
  text_container: {
    position: 'absolute',
    width: 150,
    height: 50,
    bottom: 0,
    paddingVertical: 2,
    backgroundColor: 'rgba(255,255,255, 1)',
    borderBottomLeftRadius: 10,
    borderBottomRightRadius: 10,
  },
  card_title: {
    color: 'black',
    fontSize: 14,
    marginLeft: 10,
  },
  card_subtitle:{
    color: 'grey',
    fontSize: 14,
    marginLeft: 10,
  }
});

您可以在此处使用工作代码:Expo Snack

【讨论】:

  • 感谢@Ketan 的评论,但我需要自动播放,我想我只能通过轮播(反应式快照轮播)来实现。你能告诉我它怎么能自动播放。
  • 自动播放是指自动滚动?
  • 是的,我想让它像一个轮播,第二个项目在第一个项目之后自动播放
  • @KetanRamteke 您能否使用视频而不是图像创建上述示例。我尝试了你的世博小吃链接,但由于我是 RN 的新手,所以无法实现。你能指导我正确的方向吗?
【解决方案2】:

使用ScrollviewFlatlisthorizontal={true} props 渲染上面的ListItems

https://reactnative.dev/docs/scrollview#horizontal

【讨论】:

    猜你喜欢
    • 2018-02-15
    • 2019-09-25
    • 1970-01-01
    • 2022-07-06
    • 1970-01-01
    • 2017-10-26
    • 1970-01-01
    • 2021-09-07
    • 1970-01-01
    相关资源
    最近更新 更多