Hello. I'm trying to add swipe-to-refresh to my We...
# compose
r
Hello. I'm trying to add swipe-to-refresh to my WebView, using Accompanist's
SwipeRefresh
. Simple example:
Copy code
var isRefreshing by remember { mutableStateOf(false) }
var isLoaded by remember { mutableStateOf(false) }

SwipeRefresh(state = rememberSwipeRefreshState(isRefreshing),
    onRefresh = { isRefreshing = true }) {
    AndroidView(
        factory = {
            WebView(it).apply {
                webViewClient = WebViewClient()
            }
        }
    ) { webView ->
        if (!isLoaded) {
            webView.loadUrl("<https://www.android.com/>")
            isLoaded = true
        }

        if (isRefreshing) {
            webView.reload()
            isRefreshing = false
        }
    }
}
This does not work because
SwipeRefresh
needs scrollable child. By adding
verticalScroll
modifier to AndroidView,
SwipeRefresh
works, but WebView gets infinite height and content does not rendered as expected. Is there any way to notify legacy View's scroll events to parent Composable?
d
I believe there is a releated issue - https://issuetracker.google.com/issues/174348612
a
The problem is more than that. AFAIK even in view system you can’t (easily) implement swipe-to-refresh with WebView as WebView doesn’t support nested scroll out of the box.
r
Thank you, that issue seems to focus on View -> ComposeView case(not AnroidView -> Composable case), but I got some hint about
NestedScrollDispatcher
. I'll try to dispatch view's scroll event to NestedScrollDispatcher ...
In view system, simply using
SwipeRefreshLayout
over WebView seems to work fine, as far as I tried.
634 Views