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);
}