android React Native动态引用

k10s72fa  于 2023-01-19  发布在  Android
关注(0)|答案(1)|浏览(162)

我试图从特定的视图中获取一些度量值,以查看视图的位置。在此之后,我想实现一个scrollto函数。
我使用以下代码作为句柄:

_handleItemClicked = (index) => () => {
  this.sectionItem[index].measure( (fx, fy, width, height, px, py) => {
    console.log('Component width is: ' + width)
    console.log('Component height is: ' + height)
    console.log('X offset to frame: ' + fx)
    console.log('Y offset to frame: ' + fy)
    console.log('X offset to page: ' + px)
    console.log('Y offset to page: ' + py)
  })
}

我用这段代码来创建视图

renderItem = ({item, index, section}) => (
            <TouchableNativeFeedback ref={view => { this.sectionItem[index] = view; }} onPress={this._handleItemClicked(index)}>
               <View style={styles.itemrow} >
                 <View style={{width:80,marginLeft:10,height:30}}><Text style={{textAlign:'right',color:'#000',opacity:0.9,fontSize:18,marginTop:4}}>{item.title}</Text></View>
                 </View>
               </View>
             </TouchableNativeFeedback>
);

我尝试了一些其他的方法,但是到目前为止还没有成功。我总是以一些错误结束。这次它是undefined,不是一个对象。
期待答案。

p1tboqfb

p1tboqfb1#

试试这个:

ref = { view => ( this[`sectionItem${index}`] = view ) }

我猜它试图在范围内找到sectionItem,但无法找到,给你一个未定义的错误。
如果你像上面那样做,它会把一个带索引的ref放在this中。

相关问题