react native 关于ScrollView嵌套WebView的交互问题

本文探讨了在React Native中ScrollView嵌套WebView时遇到的交互问题,包括WebView加载空白及Android上的滚动问题。作者提出了解决方案:注入JavaScript代码设置高度,并通过onMessage实现WebView与RN的数据交换,动态调整WebView高度,从而成功解决滚动问题。
摘要由CSDN通过智能技术生成

react native 关于ScrollView嵌套WebView的交互问题,这个问题很早前就解决过了,但是发现好多人有这方面问题,之前是因为写资讯的需求在网页下面还得显示评论等内容所以发现的这个问题,写个我的解决方案吧!

首先常规下,WebView正常的展示网页:

 <SafeAreaView style={styles.safeAreaView}>
   <WebView
       style={
  { flex: 1 }}
       source={
  { uri: 'https://bbs.reactnative.cn/category/3/blogs' }}
    />         
</SafeAreaView> 

但是用ScrollView嵌套时,就会加载空白:

<SafeAreaView style={styles.safeAreaView}>
   <ScrollView style={
  { flex: 1 }}>
       <WebView
         style={
  { flex: 1 }}
         source={
  { uri: 'https://bbs.reactnative.cn/category/3/blogs' }}
       />
   </ScrollView>
</SafeAreaView>

 此时我们需要设置webview的展示的宽高,就可以正常显示:

 <SafeAreaView style={styles.safeAre
  • 4
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
React Native中,当一个ScrollView嵌套在另一个ScrollView中时,子ScrollView默认是不可以滑动的。为了让子ScrollView可以滑动,需要在父ScrollView的onTouchStart、onTouchMove和onTouchEnd事件中对事件进行处理。 具体来说,可以在父ScrollView的onTouchStart事件中记录下当前手指的位置,然后在onTouchMove事件中判断当前手指的位置是否在子ScrollView的范围内,如果是,则将事件交给子ScrollView处理;如果不是,则将事件交给父ScrollView处理。在onTouchEnd事件中,可以清除掉已经记录的手指位置信息。 以下是一个示例代码: ``` import React, { Component } from 'react'; import { ScrollView, View } from 'react-native'; class ParentScrollView extends Component { constructor(props) { super(props); this.state = { isChildScrolling: false, childScrollTop: 0, }; } handleChildScroll = (event) => { const { contentOffset } = event.nativeEvent; this.setState({ childScrollTop: contentOffset.y, }); } handleTouchStart = (event) => { const { locationY } = event.nativeEvent; if (locationY > this.state.childScrollTop) { this.setState({ isChildScrolling: true, }); } } handleTouchMove = (event) => { const { locationY } = event.nativeEvent; if (this.state.isChildScrolling) { this.childScrollView.scrollTo({ y: locationY - this.state.childScrollTop }); } } handleTouchEnd = () => { this.setState({ isChildScrolling: false, childScrollTop: 0, }); } render() { return ( <ScrollView onTouchStart={this.handleTouchStart} onTouchMove={this.handleTouchMove} onTouchEnd={this.handleTouchEnd} > <View style={{ height: 200 }}> <ScrollView ref={(ref) => { this.childScrollView = ref; }} onScroll={this.handleChildScroll} > {/* 子ScrollView的内容 */} </ScrollView> </View> {/* 父ScrollView的其他内容 */} </ScrollView> ); } } export default ParentScrollView; ``` 在上面的代码中,首先在父ScrollView的state中记录了一个isChildScrolling和childScrollTop变量,分别表示子ScrollView是否正在滑动,以及子ScrollView的当前scrollTop值。然后在父ScrollView的onTouchStart事件中,根据手指位置是否在子ScrollView的范围内,来判断是否将事件交给子ScrollView处理。在onTouchMove事件中,如果子ScrollView正在滑动,则通过scrollTo方法来滑动子ScrollView。在onTouchEnd事件中,清除掉记录的变量信息。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值