Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/react-native/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Listview ScrollView子元素度量值始终返回frameOffsetY=0_Listview_React Native - Fatal编程技术网

Listview ScrollView子元素度量值始终返回frameOffsetY=0

Listview ScrollView子元素度量值始终返回frameOffsetY=0,listview,react-native,Listview,React Native,我想使用ScrollView的scrollTo方法滚动到按下的元素 ScrollView中的每个子级都有一个ref,测量值由onLayout道具触发,状态为offsetY足以正确触发滚动 但是,frameOffsetY的this.componentRef.measure返回的值始终为0,因此它总是滚动到ScrollView的开头 下面是子组件的代码 measureOffset() { this.componentRef.measure( (frameOffsetX, fr

我想使用
ScrollView
scrollTo
方法滚动到按下的元素

ScrollView
中的每个子级都有一个
ref
,测量值由
onLayout
道具触发,状态为
offsetY
足以正确触发滚动

但是,
frameOffsetY
this.componentRef.measure
返回的值始终为0,因此它总是滚动到ScrollView的开头

下面是子组件的代码

  measureOffset() {
    this.componentRef.measure(
      (frameOffsetX, frameOffsetY, width, height, pageOffsetX, pageOffsetY) => {
        console.log(frameOffsetX, frameOffsetY, width, height, pageOffsetX, pageOffsetY);
        this.setState({ offsetY: frameOffsetY });
      }
    );
  }

  render() {
    return (
      <View
        ref={ref => this.componentRef = ref}
        style={{ opacity: 1, flex: 1 }}
        onLayout={() => this.measureOffset()}
      >
      ...
      </View>
    );
 }
measureOffset(){
此.componentRef.measure(
(frameOffsetX,frameOffsetY,宽度,高度,pageOffsetX,pageOffsetY)=>{
日志(frameOffsetX,frameOffsetY,宽度,高度,pageOffsetX,pageOffsetY);
this.setState({offsetY:frameOffsetY});
}
);
}
render(){
返回(
this.componentRef=ref}
style={{opacity:1,flex:1}}
onLayout={()=>this.measureOffset()}
>
...
);
}
父组件是一个滚动视图

  renderBuses() {
    return this.props.buses.map(bus =>
      <BusDetail 
        key={bus.reg_code} 
        bus={bus} 
        scrollTo={y => ScrollViewRef.scrollTo({ y })}
      />
    );
  }

  render() {
    return (
      <View style={{ flex: 1, backgroundColor: '#e6e6e6' }}>
        <ScrollView 
          style={{ flex: 1 }}
          ref={ref => ScrollViewRef = ref}
        >
          {this.renderBuses()}
        </ScrollView>
      </View>
    );
  }
renderBuses(){
返回此.props.bus.map(bus=>
ScrollViewRef.scrollTo({y}}
/>
);
}
render(){
返回(
ScrollViewRef=ref}
>
{this.renderBus()}
);
}
我不确定我是否遗漏了什么,或者这是一个错误的反应


提前感谢您的帮助。

请注意,这些测量值在本机渲染完成后才可用。如果您需要尽快测量,请考虑使用ONPLAYPROP。 看

初始化时将调用onLayout,此处调用measure将不会获得实际测量值

有一个onLayout函数的参数,如下所示

onLayout={(e)=>{
  let obj = e.nativeEvent.layout;
  console.log(obj.width);
  console.log(obj.height);
}