https://kotlinlang.org logo
#compose
Title
# compose
r

Ryosuke Yamada

03/27/2022, 10:43 AM
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

Dmitry Strekha

03/27/2022, 11:52 AM
I believe there is a releated issue - https://issuetracker.google.com/issues/174348612
a

Albert Chang

03/27/2022, 1:01 PM
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

Ryosuke Yamada

03/27/2022, 1:59 PM
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.
456 Views