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()}
/>
}
/>
複製代碼