本文基于React Native 0.52

Demo上传到Git了,有需要可以看看,写了新内容会上传的。Git地址 https://github.com/gingerJY/React-Native-Demo

一、总览

  这个效果也是APP里很常见的,之前把这个想的太复杂了,后来才知道原来用FlatList就可以轻松实现,效果图如下(专题精选):

二、代码实现

  1、加几条数据

topic: [
{
title: '岁末清扫有它们,体验大不同',
describe: '更轻松、更美好的大扫除攻略',
price: '9.9元起',
},
{
title: '新年一点红,幸运一整年',
describe: '那些让你“红”运当头的好物',
price: '9.9元起',
},
]

  2、写列表的一个item

renderTopicItem = ({ item }) => {
return (
<TouchableOpacity style={styles.topicItem}>
<Image source={require('../../img/topic.jpg')} style={styles.topicImg} />
<View style={styles.topicContainer}>
<View style={styles.topicText}>
<Text style={styles.topicTitle}>{item.title}</Text>
<Text style={styles.topicDesc}>{item.describe}</Text>
</View>
<Text style={styles.topicPrice}>{item.price}</Text>
</View>
</TouchableOpacity>
)
}

  3、用FlatList渲染出列表

renderTopic() {
return (
<View style={styles.topic}>
<Text style={styles.topicHead}>专题精选</Text>
<FlatList
data={this.state.topic}
keyExtractor={(item, index) => index}
renderItem={this.renderTopicItem}
horizontal={true}
showsHorizontalScrollIndicator={false}
/>
</View>
)
}
    • data —— 数据(目前只支持普通数组)
    • renderItem —— 根据行数据data渲染每一行的组件
    • keyExtractor —— 用于为给定的item生成一个不重复的key(Key的作用是使React能够区分同类元素的不同个体,以便在刷新时能够确定其变化的位置,减少重新渲染的开销)
    • horizontal —— 设为true时是水平布局
    • showsHorizontalScrollIndicator —— 设为false,则不显示水平滚动条

4、样式

    topic: {
width: width,
alignItems:'center',
backgroundColor: '#fff',
paddingBottom:10,
marginBottom:10,
},
topicHead:{
fontSize:16,
color:'#666',
padding:15,
},
topicItem: {
width: width*0.7,
marginLeft:15,
},
topicImg: {
width: width*0.7,
height: width*0.4,
borderWidth:0.5,
borderColor:'#cdcdcd',
borderRadius:2,
},
topicContainer:{
flexDirection: 'row',
justifyContent:'space-between',
marginTop:10,
},
topicTitle:{
fontSize:16,
color:'#666',
},
topicDesc:{
fontSize:13,
color:'#999',
marginTop:3,
},
topicPrice:{
fontSize:14,
color:'#b4282d',
},

  recommend.js完整代码 https://github.com/gingerJY/example/blob/master/RN_flatList/recommend.js

三、其他

    

  这种也是用 FlatList 做的,写法都差不多,具体看https://github.com/gingerJY/React-Native-Demo

END-----------------------------------------------------------------

React Native学习(七)—— FlatList实现横向滑动列表效果的更多相关文章

  1. 实现移动端touch事件的横向滑动列表效果

    要实现手机端横向滑动效果并不难,了解实现的原理及业务逻辑就很容易实现.原理:touchstart(手指按下瞬间获取相对于页面的位置)——>touchmove(手指移动多少,元素相应移动多少). ...

  2. react native 左边固定,右边横向滑动左右自适应高度

    要实现的效果 https://zuobaiquan.github.io/blogImg/201903/01.gif

  3. React Native 学习笔记--进阶(二)--动画

    React Native 进阶(二)–动画 动画 流畅.有意义的动画对于移动应用用户体验来说是非常必要的.我们可以联合使用两个互补的系统:用于全局的布局动画LayoutAnimation,和用于创建更 ...

  4. React Native 学习-01

    React Native 学习 (学习版本 0.39) 一.环境配置 二.IDE选择 webstorm 1.webstorm配置 ①.首先是可以选择使用汉化包汉化.eu68 ②.安装插件和外部库. 由 ...

  5. react native 学习一(环境搭配和常见错误的解决)

    react native 学习一(环境搭配) 首页,按照http://reactnative.cn/docs/0.30/getting-started.html#content上的介绍,下载安装pyt ...

  6. React Native 学习资料

    React Native 学习资料 学习资料 网址 React Native中文网 https://reactnative.cn/

  7. React Native 学习(三)之 FlexBox 布局

    React Native 学习(三)之 FlexBox 布局

  8. React Native学习(八)—— 对接七鱼客服

    本文基于React Native 0.52 Demo上传到Git了,有需要可以看看,写了新内容会上传的.Git地址 https://github.com/gingerJY/React-Native-D ...

  9. iOS、swift、React Native学习常用的社区、论坛

    <!----iOS> <!----Swift>*IOS开发常用社区:http://code4app.com/ *IOS开发常用社区:http://www.cocoachina. ...

随机推荐

  1. String 转化成java.sql.Date和java.sql.Time

    String类型转换成java.sql.Date类型不能直接进行转换,首先要将String转换成java.util.Date,在转化成java.sql.Date 请点击--->   java架构 ...

  2. 如何减轻ajax定时触发对服务器造成的压力和带宽的压力?ajax-长轮训

    AJAX长轮询的方法来解决频繁对后台的请求,进一步减小压力 在实现过程发现AJAX的多次请求会出现多线程并发的问题又使用线程同步来解决该问题 个人对ajax长轮询的一点愚见 ajax请示后台时,后台程 ...

  3. Io 异常: The Network Adapter could not establish the connection

    新接触一个项目,导入源码,在本地启动的时候后台报了一个错误: Could not discover the dialect to use. java.sql.SQLException: Io 异常: ...

  4. 高仿二次元网易GACHA

    高仿二次元网易GACHA,所有接口均通过Charles抓取而来,图片资源通过 https://github.com/yuedong56/Assets.carTool 工具提取. 详情见github地址 ...

  5. 抽象方法为什么不能被private与static修饰

    private private访问修饰符修饰的方法只能在本类当中使用.所以,必然不能用private去修饰抽象方法.抽象方法一定是要被子类去重写的. static Java中用static修饰符修饰的 ...

  6. 基于web的网上书城系统开发-----登录注册

    注册功能实现 signup.jsp //时间实现 function showLocale(objD) { var str,colorhead,colorfoot; var yy = objD.getY ...

  7. 深入理解cookie和session

    cookie和session在java web开发中扮演了十分重要的作用,本篇文章对其中的重要知识点做一些探究和总结. 1.cookie存在于浏览器 随意打开一个网址,用火狐的调试工具,随意选取一个链 ...

  8. sqlserver资源

    1.数据库“高可用性”和“灾难恢复”技术 参考: niyi0318的专栏

  9. Mac新手使用指南:brew安装的nginx常用命令

    安装:brew install nginx/sudo brew install nginx 启动:brew services start nginx/sudo brew services start ...

  10. C#在与java对接时候的UrlEncode的坑

    最近与建行接口做对接和与一家短信运营商做对接时候遇到了这个坑 在java中对UrlEncode 时候哪些url非安全字符被转为%数字和大写字幕组合,比如:zhangsan/d 会被转为 zhangsa ...