FlatList实现横向滑动列表效果

本文基于React Native 0.52

http://www.cnblogs.com/MaiJiangDou/p/8351288.html

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

一、总览

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

 

二、代码实现

  1、加几条数据




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




  2、写列表的一个item




1
2
3
4
5
6
7
8
9
10
11
12
13
14
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渲染出列表




1
2
3
4
5
6
7
8
9
10
11
12
13
14
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、样式




1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
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

实现移动端touch事件的横向滑动列表效果可以使用原生的JavaScript和CSS3来实现。 首先,我们需要在HTML中创建一个容器元素,用来包含列表项。容器元素需要设置overflow-x属性为scroll,使得内容超出容器范围时可以滚动。 ```html <div class="container"> <ul class="list"> <li>项1</li> <li>项2</li> <li>项3</li> <li>项4</li> <li>项5</li> </ul> </div> ``` 然后,在CSS中,我们需要设置容器元素和列表项的样式,以及使用CSS3的transition属性来实现平滑的过渡效果。 ```css .container { width: 100%; overflow-x: scroll; -webkit-overflow-scrolling: touch; /* 添加iOS滚动效果 */ } .list { display: flex; flex-wrap: nowrap; /* 设置列表项不换行 */ transition: transform 0.3s ease; /* 添加平滑的过渡效果 */ } .list li { width: 100px; height: 100px; margin-right: 10px; background-color: #ccc; } ``` 最后,在JavaScript中,我们需要监听容器元素的touchstart、touchmove和touchend事件,计算滑动距离并通过改变列表项的transform属性来实现横向滑动效果。 ```javascript const container = document.querySelector('.container'); const list = document.querySelector('.list'); let isDragging = false; let startPosition = 0; let currentTranslate = 0; let prevTranslate = 0; let animationId = 0; container.addEventListener('touchstart', touchStart); container.addEventListener('touchmove', touchMove); container.addEventListener('touchend', touchEnd); container.addEventListener('touchcancel', touchEnd); function touchStart(event) { if (event.target.classList.contains('list')) { isDragging = true; startPosition = event.touches[0].clientX; animationId = requestAnimationFrame(updateAnimation); container.classList.add('grabbing'); } } function touchMove(event) { if (isDragging) { const currentPosition = event.touches[0].clientX; currentTranslate = prevTranslate + currentPosition - startPosition; } } function touchEnd() { isDragging = false; cancelAnimationFrame(animationId); prevTranslate = currentTranslate; container.classList.remove('grabbing'); } function updateAnimation() { list.style.transform = `translateX(${currentTranslate}px)`; animationId = requestAnimationFrame(updateAnimation); } ``` 通过以上代码,我们就成功地实现了移动端touch事件的横向滑动列表效果
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值