0
1

JetpackComposeのWebViewで上部に読み込みインジケータを表示する

Posted at

はじめに

今回はJetpackComposeのWebViewで上部に読み込みインジケータを表示する方法を紹介していきます

本文

Composebale関数内で下記の処理を記述します
newProgressは1から100で帰ってきますがProgressIndicatorは0から1で表示するので
渡す場合は100で割らないとずっと読み込み完了のままインジケータが進みません

val isLoading = remember { mutableStateOf(true) } 
val currentProgress = remember { mutableStateOf(0f) }

val webChromeClient = object : AccompanistWebChromeClient() {
    override fun onProgressChanged(view: WebView?, newProgress: Int) {
        super.onProgressChanged(view, newProgress)
        currentProgress.value = newProgress.toFloat() / 100f
        isLoading.value = newProgress < 100
    }
}

if (isLoading.value) {
    LinearProgressIndicator(
        progress =  currentProgress.value,
        modifier = Modifier.fillMaxWidth(),
    )
}

最後に

昔はAndroidViewで書いていたWebViewも公式から出てくるようになり色々実装できるようになったので備忘録として今回の記事を書きました
どなたかのお役に立てれば幸いです

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1