import { FlatList } from 'react-native';
复制代码
用来定义头部组件react
ListHeaderComponent={()=> this._createListHeader()}
_createListHeader(){
return (
<View style={styles.headView}>
<Text style={{color: 'white'}}>
头部布局
</Text>
</View>
)
}
复制代码
用来定义尾部android
ListFooterComponent = {()=> this._createListFooter()}
_createListFooter(){
return(
<View style="style.indicatorContainer">
{ this.state.showFoot === 1
&&
<ActivityIndicator
style={styles.indicator}
size = 'small'
animating = {true}
color = 'red'
/>
}
<Text>{this.state.showFoot === 1?'正在加载更多数据...':'没有更多数据'}</Text>
</View>
)
}
复制代码
用来定义分割线ios
ItemSeparatorComponent = {()=>this._createSeparatorComponent()}
_createSeparatorComponent(){
return(
<View style={{height: 5, backgroundColor: '#eeeeee'}}/>
)
}
复制代码
为FlatList设置一个没有数据时候的展现的视图git
必需要为item设置key属性,不然会有一个黄色的警告弹出。并且咱们须要注意的是这里每个item的key是惟一的!github
keyExtractor={this._keyExtractor}
_keyExtractor(item, index){
return `index${index}`
}
复制代码
属性是一个可选的优化,用于避免动态测量内容尺寸的开销。若是咱们知道item的高度,就能够为FlatList指定这一个属性,来使FlatList更加高效的运行!react-native
//200为item的高度,5为分割线的高度
getItemLayout={(data, index) => ({
length:200, offset: (200 + 5) * index, index
})}
复制代码
FlatList中有两个属性,能够用来设置下拉刷新。bash
//不须要自定义样式的时候
/*refreshing = { this.state.is_Loading }
onRefresh = {()=>{
this.loadData();
}}*/
//修改loading样式
refreshControl = {
<RefreshControl
title = {'loading'}
colors = {['red']}//android
tintColor = {'red'}//ios
refreshing = { this.state.is_Loading }
onRefresh = {()=>{
this._onRefresh();
}}
/>
}
_onRefresh(){
...
//下拉刷新的方法
}
复制代码
上拉加载,FlatList封装两个属性来实现:函数
onEndReached ={()=>{
this._onLoadMore();
}}
onEndReachedThreshold={0.1}
复制代码
horizontal={true}
复制代码
showsVerticalScrollIndicator={false}
复制代码
showsHorizontalScrollIndicator={false}
复制代码
1. 须要为FlatList设置ref属性绑定flatList
<FlatList ref={(flatList) => this._flatList = flatList}/>
2. 调用
_toEnd(){
this._flatList.scrollToEnd();
}
复制代码
1. 须要为FlatList设置ref属性绑定flatList
<FlatList ref={(flatList) => this._flatList = flatList}/>
2. 调用
_toItem(){
//viewPosition参数:0表示顶部,0.5表示中部,1表示底部
this._flatList.scrollToIndex({viewPosition: 0, index: this.state.index});
}
复制代码
支持FlatList的全部的属性和方法,另外新增3个自有属性.布局
默认是true,表示第一次是否先滑一下FlatList的Item优化
必需要赋值,表示向左滑动的最大距离
必需要赋值,表示滑动显示的内容。
<SwipeableFlatList
renderQuickActions={()=>this.genQuickAction()}
maxSwipeDistance={50}
bounceFirstRowOnMount={false}/>
//渲染
genQuickAction(){
return <View style={styles.quickContiner}>
<TouchableHighlight>
<View>
<Text
style={styles.delText}
onPress={()=>{
alert("您肯定要删除吗?")
}}>删除</Text>
</View>
</TouchableHighlight>
</View>
}
复制代码
<SectionList
sections={this.state.CITY_NAMES}//须要绑定的数据
renderItem={(data) => this._renderItem(data)}
ItemSeparatorComponent = {()=>this._createSeparatorComponent()}
keyExtractor={this._keyExtractor}
renderSectionHeader={(data)=>this._renderSectionHeader(data)}//分组的头部
refreshControl = {
<RefreshControl
title={'loading'}
colors={['red']}
refreshing= { this.state.is_Loading }
onRefresh={() => this._onRefresh()}
/>
}
/>
复制代码