React Native入门(七)之列表组件的使用(2)关于FlatList的一切
前言在上一篇博客中了解了列表组件的相关内容,主要是静态的展示了一个列表数据,了解了ScrollVIew,FlatList和SectionList的基本用法,本篇文章就深入的了解一个常用的一个列表组件FlatList的用法! 属性添加头部组件
//ES6之前写法
_header = function () {
return (
<Text style={{fontWeight: 'bold',fontSize: 20}}>热门电影</Text>
);
}
<FlatList
ListHeaderComponent={this._header}//header头部组件
/>
添加尾部组件
//ES6的写法 _footer = () => ( <Text style={{fontSize: 14,alignSelf: 'center'}}>到底啦,没有啦!</Text> ) <FlatList ListFooterComponent={this._footer} //添加尾部组件 />
添加头部和尾部组件比较简单,需要注意的就是上边两者ES5和6写法的区别! 添加分割线
class ItemDivideComponent extends Component {
render() {
return (
<View style={{height: 1,backgroundColor: 'skyblue'}}/>
);
}
};
<FlatList
ItemSeparatorComponent={ItemDivideComponent}//分割线组件
/>
这里我们自定义了一个组件来设置分割线,当然我们像添加头部和尾部一样,在内部声明之后使用 设置空数据视图
createEmptyView() { return ( <Text style={{fontSize: 40,alignSelf: 'center'}}>还没有数据哦!</Text> ); } <FlatList ListEmptyComponent={this.createEmptyView()} />
设置item的key在前一篇博客中,我们的设置 data={[
{key: '大护法'},{key: '绣春刀II:修罗战场'},...
]}
类似 那么一般地,我们可以这样使用: _keyExtractor = (item,index) => index;
<FlatList
keyExtractor={this._keyExtractor}
/>
这样就把 你明白了吗? 设置itemLayout
getItemLayout={(data,index) => ({
length: 44,offset: (44 + 1) * index,index
})}
我们在上边使用的时候指定了item的高度为44,所以 getItemLayout={(data,index) => ({ length: 你的item的height,offset: (你的item的height + ItemSeparator的height) * index,index
})}
设置这一属性,在调用FlatList的跳转函数的时候非常有用,否则可能会很卡顿!如 下拉刷新FlatList中有两个属性,可以用来设置下拉刷新。
这里的 如何使用,举个例子: refreshing={this.state.refreshing}
onRefresh={() => {
this.setState({refreshing: true})//开始刷新
//这里模拟请求网络,拿到数据,3s后停止刷新
setTimeout(() => {
alert('没有可刷新的内容!');
this.setState({refreshing: false});//停止刷新
},3000);
}}
上拉加载关于上拉加载,FlatList也封装有两个属性来实现:
具体使用,举个例子: onEndReachedThreshold={0.1}
onEndReached={({distanceFromEnd}) => (
setTimeout(() => {
this.setState((state) => ({
data: state.data.concat(this._newData),}));
},3000)
)}
这里我们设置的距离为列表可见长度的1/10,而触发了onEndReached函数时,我们设置了一个定时器,3s后,将 函数介绍FlatList下有两个比较常用的函数:
如官方所言,使用这两个函数的时候,最好指定设置 使用因为这两个是FlatList组件的函数,所以在使用这两个函数之前,首先我们要得到FlatList组件的引用。 具体的使用: ref={(flatList) => this._flatList = flatList}
this._flatList.scrollToEnd();
//viewPosition参数:0表示顶部,0.5表示中部,1表示底部
this._flatList.scrollToIndex({viewPosition: 0,index: this.state.text});
至此,关于FlatList进阶的相关基础内容就说完了! 完整的Demo下面看一下完整的demo: 源码: class FlatListTest extends Component {
constructor(props) {
super(props);
this.state = {
data: this._sourceData,refreshing: false,//初始化不刷新
text: ''//跳转的行
};
}
_header = function () {
return (
<Text style={{fontWeight: 'bold',fontSize: 20}}>热门电影</Text>
);
}
_footer = () => (
<Text style={{fontSize: 14,alignSelf: 'center'}}>到底啦,没有啦!</Text>
)
createEmptyView() {
return (
<Text style={{fontSize: 40,alignSelf: 'center'}}>还没有数据哦!</Text>
);
}
//此函数用于为给定的item生成一个不重复的key
//若不指定此函数,则默认抽取item.key作为key值。若item.key也不存在,则使用数组下标index。
_keyExtractor = (item,index) => index;
itemClick(item,index) {
alert('点击了第' + index + '项,电影名称为:' + item.name);
}
_renderItem = ({item,index}) => {
return (
<TouchableOpacity
activeOpacity={0.5}
onPress={this.itemClick.bind(this,item,index)}>
<Text style={flatListStyles.item}>{item.name}</Text>
</TouchableOpacity>
);
}
//点击按钮跳转
onButtonPress() {
//viewPosition参数:0表示顶部,0.5表示中部,1表示底部
this._flatList.scrollToIndex({viewPosition: 0,index: this.state.text});
//this._flatList.scrollToOffset({ animated: true,offset: 2000 });
};
onBtnPress2Botton() {
this._flatList.scrollToEnd();
}
_sourceData = [
{name: '大护法'},{name: '绣春刀II:修罗战场'},{name: '神偷奶爸3'},{name: '神奇女侠'},{name: '摔跤吧,爸爸'},{name: '悟空传'},{name: '闪光少女'},{name: '攻壳机动队'},{name: '速度与激情8'},{name: '蝙蝠侠大战超人'},{name: '蝙蝠侠大战超人'}
]
_newData = [{name: '我是新添加的数据1'},{name: '我是新添加的数据2'},{name: '我是新添加的数据3'}]
render() {
return (
<View style={flatListStyles.container}>
<View style={{flexDirection: 'row',justifyContent: 'center',alignItems: 'center'}}>
<TextInput
style={{flex: 1}}
placeholder="请输入要跳转的行号"
onChangeText={(text) => this.setState({text})}
/>
<Button title="跳转到行" onPress={this.onButtonPress.bind(this)} color={'skyblue'}/>
<Button title="跳转到底部" onPress={this.onBtnPress2Botton.bind(this)} color={'green'}/>
</View>
<FlatList
data={this.state.data}
//使用 ref 可以获取到相应的组件
ref={(flatList) => this._flatList = flatList}
ListHeaderComponent={this._header}//header头部组件
ListFooterComponent={this._footer}//footer尾部组件
ItemSeparatorComponent={ItemDivideComponent}//分割线组件
//空数据视图,可以是React Component,也可以是一个render函数,或者渲染好的element。
ListEmptyComponent={this.createEmptyView()}
keyExtractor={this._keyExtractor}
//是一个可选的优化,用于避免动态测量内容尺寸的开销,不过前提是你可以提前知道内容的高度。
//如果你的行高是固定的,getItemLayout用起来就既高效又简单.
//注意如果你指定了SeparatorComponent,请把分隔线的尺寸也考虑到offset的计算之中
getItemLayout={(data,index) => ( {length: 44,index} )}
//决定当距离内容最底部还有多远时触发onEndReached回调。
//注意此参数是一个比值而非像素单位。比如,0.5表示距离内容最底部的距离为当前列表可见长度的一半时触发。
onEndReachedThreshold={0.1}
//当列表被滚动到距离内容最底部不足onEndReachedThreshold的距离时调用
onEndReached={({distanceFromEnd}) => (
setTimeout(() => {
this.setState((state) => ({
data: state.data.concat(this._newData),}));
},3000)
)}
refreshing={this.state.refreshing}
onRefresh={() => {
this.setState({refreshing: true})//开始刷新
//这里模拟请求网络,拿到数据,3s后停止刷新
setTimeout(() => {
alert('没有可刷新的内容!');
this.setState({refreshing: false});
},3000);
}}
renderItem={this._renderItem}
/>
</View>
);
}
}
;
class ItemDivideComponent extends Component {
render() {
return (
<View style={{height: 1,backgroundColor: 'skyblue'}}/>
);
}
}
;
const flatListStyles = StyleSheet.create({
container: {
flex: 1,paddingTop: 22
},item: {
padding: 10,fontSize: 18,height: 44,},})
AppRegistry.registerComponent('AwesomeProject',() => FlatListTest);
结语好了,关于FlatList基本的进阶内容都在这里了,我也是初学者,如果有什么错误或者疑问,还请不吝赐教! 我们下一篇再见! (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |