在 Web 视图中的页面导航之间显示加载指示器
Show loading indicator between navigation of pages in a Webview
我在 Webview 中有一个 url,如 https://www.nytimes.com/ 当前代码在初始页面加载时有效,但如果我在 link 中键入任何内容,页面需要一段时间才能加载网站上没有加载指示器。有没有什么方法可以在我们点击任何 link 或页面加载时在 React Native 中放置一个页面加载指示器,如果它像下一个 js 一样在服务器端呈现的话?
这是我的 ReactNative 代码。
import * as React from 'react';
import {
View,
Text,
Image,
SafeAreaView,
ScrollView,
TextInput,
TouchableOpacity,
} from 'react-native';
import styles from './styles';
import { WebView } from 'react-native-webview';
// import WelcomeSwiper from '../../components/WelcomeScreen/WelcomeSwiper';
import LoadingIcon from '../../components/Loading/index.js';
const WebView = ({ navigation }) => {
return (
<SafeAreaView style={styles.container}>
<WebView
javaScriptEnabled={true}
domStorageEnabled={true}
renderLoading={LoadingIcon}
source={{ uri: 'https://www.nytimes.com/ ' }}
startInLoadingState={true}
/>
</SafeAreaView>
);
};
export default WebView;
这是我的加载组件
import React from 'react';
import { StyleSheet, Platform, ActivityIndicator } from 'react-native';
const LoadingIcon = () => {
return (
<ActivityIndicator
color='#009688'
size='large'
style={styles.ActivityIndicatorStyle}
/>
);
}
export default LoadingIcon;
const styles = StyleSheet.create(
{
WebViewStyle:
{
justifyContent: 'center',
alignItems: 'center',
flex: 1,
marginTop: (Platform.OS) === 'ios' ? 20 : 0
},
ActivityIndicatorStyle: {
position: 'absolute',
left: 0,
right: 0,
top: 0,
bottom: 0,
alignItems: 'center',
justifyContent: 'center'
}
});
我们可以使用这两种方法来得到结果:
您可以使用 onLoadProgress
方法检查 WebView 是否正在加载某些内容。此方法为您提供一个介于 0 和 1 之间的数字。如果页面已完全加载,它将 return 数字 1,更新您的状态并根据它显示 ActivityIndicator:
您可以使用onLoadStart
和onLoadEnd
来更新您的状态并根据它显示ActivityIndicator!
你也可以使用你的 ActivityIndicator
由 WebView 包裹,*不要忘记这个方法在 ios 中有效 android放在WebView外面
这是适合您的工作代码示例:
import React, {useState} from 'react';
import {View, Text, SafeAreaView, ActivityIndicator} from 'react-native';
import {WebView} from 'react-native-webview';
function WebViewTest() {
const [isLoading, setLoading] = useState(false);
return (
<SafeAreaView style={{flex: 1}}>
<WebView
source={{uri: 'https://www.nytimes.com/'}}
onLoadStart={(syntheticEvent) => {
setLoading(true);
}}
onLoadEnd={(syntheticEvent) => {
setLoading(false);
}} />
{isLoading && (
<View style={{flex: 10, backgroundColor: 'white'}}>
<ActivityIndicator
color="#009688"
size="large"
// style={{position: 'absolute', left: 200, top: 300}}
/>
</View>
)}
</SafeAreaView>
);
}
export default WebViewTest;
希望对你有帮助
我用onLoadProgress
来解决这个问题。
renderLoading
函数只是在 webview 组件的初始加载状态被调用。使用 renderLoading 无助于在点击页面链接或在 webview 页面之间导航时显示 activity 指示器。
检查 onLoadStart
和 onLoadEnd
在 android 中很有用,但在 iOS 中 onLoadEnd
不会在后退导航手势中调用,这会导致无限旋转activity 指标。
onLoadProgress
returns webview处于加载状态时0-1之间的数字。您检查其进度状态并更新您的 activity 指示器状态。
有关onLoadProgress
的更多信息:onLoadProgress
这是一个适合您的工作代码示例:
import React, {useState} from 'react';
import {View, Text, SafeAreaView, ActivityIndicator} from 'react-native';
import {WebView} from 'react-native-webview';
function WebViewTest() {
const [isLoading, setLoading] = useState(false);
return (
<SafeAreaView style={{flex: 1}}>
<WebView
source={{uri: 'https://www.nytimes.com/'}}
onLoadProgress={({nativeEvent}) => {
if (nativeEvent.progress != 1 && isLoading == false ) {
setLoading(true)
} else if (nativeEvent.progress == 1 ) {
setLoading(false)
}
}}
/>
{isLoading && (
<View style={{flex: 10, backgroundColor: 'white'}}>
<ActivityIndicator
color="#009688"
size="large"
// style={{position: 'absolute', left: 200, top: 300}}
/>
</View>
)}
</SafeAreaView>
);
}
export default WebViewTest;
我在 Webview 中有一个 url,如 https://www.nytimes.com/ 当前代码在初始页面加载时有效,但如果我在 link 中键入任何内容,页面需要一段时间才能加载网站上没有加载指示器。有没有什么方法可以在我们点击任何 link 或页面加载时在 React Native 中放置一个页面加载指示器,如果它像下一个 js 一样在服务器端呈现的话?
这是我的 ReactNative 代码。
import * as React from 'react';
import {
View,
Text,
Image,
SafeAreaView,
ScrollView,
TextInput,
TouchableOpacity,
} from 'react-native';
import styles from './styles';
import { WebView } from 'react-native-webview';
// import WelcomeSwiper from '../../components/WelcomeScreen/WelcomeSwiper';
import LoadingIcon from '../../components/Loading/index.js';
const WebView = ({ navigation }) => {
return (
<SafeAreaView style={styles.container}>
<WebView
javaScriptEnabled={true}
domStorageEnabled={true}
renderLoading={LoadingIcon}
source={{ uri: 'https://www.nytimes.com/ ' }}
startInLoadingState={true}
/>
</SafeAreaView>
);
};
export default WebView;
这是我的加载组件
import React from 'react';
import { StyleSheet, Platform, ActivityIndicator } from 'react-native';
const LoadingIcon = () => {
return (
<ActivityIndicator
color='#009688'
size='large'
style={styles.ActivityIndicatorStyle}
/>
);
}
export default LoadingIcon;
const styles = StyleSheet.create(
{
WebViewStyle:
{
justifyContent: 'center',
alignItems: 'center',
flex: 1,
marginTop: (Platform.OS) === 'ios' ? 20 : 0
},
ActivityIndicatorStyle: {
position: 'absolute',
left: 0,
right: 0,
top: 0,
bottom: 0,
alignItems: 'center',
justifyContent: 'center'
}
});
我们可以使用这两种方法来得到结果:
您可以使用
onLoadProgress
方法检查 WebView 是否正在加载某些内容。此方法为您提供一个介于 0 和 1 之间的数字。如果页面已完全加载,它将 return 数字 1,更新您的状态并根据它显示 ActivityIndicator:您可以使用
onLoadStart
和onLoadEnd
来更新您的状态并根据它显示ActivityIndicator!
你也可以使用你的 ActivityIndicator
由 WebView 包裹,*不要忘记这个方法在 ios 中有效 android放在WebView外面
这是适合您的工作代码示例:
import React, {useState} from 'react';
import {View, Text, SafeAreaView, ActivityIndicator} from 'react-native';
import {WebView} from 'react-native-webview';
function WebViewTest() {
const [isLoading, setLoading] = useState(false);
return (
<SafeAreaView style={{flex: 1}}>
<WebView
source={{uri: 'https://www.nytimes.com/'}}
onLoadStart={(syntheticEvent) => {
setLoading(true);
}}
onLoadEnd={(syntheticEvent) => {
setLoading(false);
}} />
{isLoading && (
<View style={{flex: 10, backgroundColor: 'white'}}>
<ActivityIndicator
color="#009688"
size="large"
// style={{position: 'absolute', left: 200, top: 300}}
/>
</View>
)}
</SafeAreaView>
);
}
export default WebViewTest;
希望对你有帮助
我用onLoadProgress
来解决这个问题。
renderLoading
函数只是在 webview 组件的初始加载状态被调用。使用 renderLoading 无助于在点击页面链接或在 webview 页面之间导航时显示 activity 指示器。
检查 onLoadStart
和 onLoadEnd
在 android 中很有用,但在 iOS 中 onLoadEnd
不会在后退导航手势中调用,这会导致无限旋转activity 指标。
onLoadProgress
returns webview处于加载状态时0-1之间的数字。您检查其进度状态并更新您的 activity 指示器状态。
有关onLoadProgress
的更多信息:onLoadProgress
这是一个适合您的工作代码示例:
import React, {useState} from 'react';
import {View, Text, SafeAreaView, ActivityIndicator} from 'react-native';
import {WebView} from 'react-native-webview';
function WebViewTest() {
const [isLoading, setLoading] = useState(false);
return (
<SafeAreaView style={{flex: 1}}>
<WebView
source={{uri: 'https://www.nytimes.com/'}}
onLoadProgress={({nativeEvent}) => {
if (nativeEvent.progress != 1 && isLoading == false ) {
setLoading(true)
} else if (nativeEvent.progress == 1 ) {
setLoading(false)
}
}}
/>
{isLoading && (
<View style={{flex: 10, backgroundColor: 'white'}}>
<ActivityIndicator
color="#009688"
size="large"
// style={{position: 'absolute', left: 200, top: 300}}
/>
</View>
)}
</SafeAreaView>
);
}
export default WebViewTest;