《ReactNative系列讲义》进阶篇---06.FlatList(三)

| 版权声明:本文为博主原创文章,未经博主允许不得转载。

一、简介

截止到上篇文章,关于FlatList无论是简单的还是高级的属性用法都已经介绍完毕,今天我们一起来看看FlatList更高级的玩法,相关方法的调用。

二、基础知识
  • 获取FlatList的引用
  • scrollToEnd:直接跳转到内容的底部,建议设置getItemLayout属性,不然会出现卡顿
  • scrollToIndex:跳转到指定索引的行。属性:viewPosition: 指定选定行显示的位置,0代表top,0.5代表middle,1代表bottom;index: 输入的索引值;如果不设置getItemLayout属性,无法到达不可见区域
  • 以上两个方法在使用时,需设置getItemLayout属性
三、应用
  • 新增功能
// 修改构造方法
constructor(props) {
   super(props);

   this.state = {
       sourceData : []
       ,selected: (new Map(): Map<String, boolean>)
       ,refreshing: false
       // 添加存储用户输入索引的状态
       ,indexText: ''
   }
}

// 跳转到指定位置
_doActionToItem = () => {
   this.flatList.scrollToIndex({ viewPosition: 0, index: this.state.indexText });
};

// 跳转到内容最底端
_doActionToBottom = () => {
   this.flatList.scrollToEnd();
};

// Header布局
_renderHeader = () => (
   <View style={
   { flexDirection:'row' }}>
       <TextInput
           style={
   { height:50, flex:1 }}
           placeholder='请输入行号'
           onChangeText={
      (text)=> {this.setState({ indexText: text })} }
       />
       <TouchableOpacity
           onPress={
      this._doActionToItem }
           style={
   { height:50, width:90, backgroundColor:'green', justifyContent:'center', alignItems:'center' }}
       >
           <Text style={
   { color:'#fff' }}>跳转到指定行</Text>
       </TouchableOpacity>
       <TouchableOpacity
           onPress={
      this._doActionToBottom }
           
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值