From 7270728f58805dc54a268ff728dd1184df085252 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=82=B9=E9=93=AD=E6=9D=B0?= Date: Wed, 3 Apr 2024 10:46:53 +0800 Subject: [PATCH] feat:Basic Pull to Refresh sample --- .../sample/PullToRefreshWebViewSample.kt | 50 +++++++++++-------- 1 file changed, 29 insertions(+), 21 deletions(-) diff --git a/app/src/main/java/com/kevinnzou/webview/sample/PullToRefreshWebViewSample.kt b/app/src/main/java/com/kevinnzou/webview/sample/PullToRefreshWebViewSample.kt index 355697a5..48971124 100644 --- a/app/src/main/java/com/kevinnzou/webview/sample/PullToRefreshWebViewSample.kt +++ b/app/src/main/java/com/kevinnzou/webview/sample/PullToRefreshWebViewSample.kt @@ -7,7 +7,10 @@ import android.util.Log import android.webkit.WebView import androidx.activity.ComponentActivity import androidx.activity.compose.setContent +import androidx.compose.foundation.layout.Column import androidx.compose.foundation.layout.fillMaxSize +import androidx.compose.foundation.rememberScrollState +import androidx.compose.foundation.verticalScroll import androidx.compose.material3.ExperimentalMaterial3Api import androidx.compose.runtime.LaunchedEffect import androidx.compose.runtime.getValue @@ -42,6 +45,7 @@ class PullToRefreshWebViewSample : ComponentActivity() { var refreshing by remember { mutableStateOf(false) } LaunchedEffect(refreshing) { if (refreshing) { + navigator.reload() delay(1200) refreshing = false } @@ -51,30 +55,34 @@ class PullToRefreshWebViewSample : ComponentActivity() { state = rememberPullToRefreshState(isRefreshing = refreshing), onRefresh = { refreshing = true } ) { - // A custom WebViewClient and WebChromeClient can be provided via subclassing - val webClient = remember { - object : AccompanistWebViewClient() { - override fun onPageStarted( - view: WebView, - url: String?, - favicon: Bitmap? - ) { - super.onPageStarted(view, url, favicon) - Log.d("Accompanist WebView", "Page started loading for $url") + Column( + modifier = Modifier.verticalScroll(state = rememberScrollState()) + ) { + // A custom WebViewClient and WebChromeClient can be provided via subclassing + val webClient = remember { + object : AccompanistWebViewClient() { + override fun onPageStarted( + view: WebView, + url: String?, + favicon: Bitmap? + ) { + super.onPageStarted(view, url, favicon) + Log.d("Accompanist WebView", "Page started loading for $url") + } } } - } - WebView( - state = state, - modifier = Modifier - .fillMaxSize(), - navigator = navigator, - onCreated = { webView -> - webView.settings.javaScriptEnabled = true - }, - client = webClient - ) + WebView( + state = state, + modifier = Modifier + .fillMaxSize(), + navigator = navigator, + onCreated = { webView -> + webView.settings.javaScriptEnabled = true + }, + client = webClient + ) + } } } }