簡體   English   中英

有沒有辦法在 webview 片段完成加載之前顯示啟動畫面?

[英]is there a way to show splash screen until the webview fragment finish loading?

我有一個首先安裝 SplashScreen installSplashScreen()的 MainActivity,然后它將加載WebView ,webviewFragment 在其他文件中,我正在嘗試使用splashScreen.setKeepOnScreenCondition{ }來保持啟動畫面顯示,直到 webviewFragment 到達onPageFinished() state,我該怎么做?

有幾種方法可以傳達這兩個組件:

  1. 你可以在你的片段中使用一個監聽器來通知活動,
  2. 您可以從片段上下文中轉換活動以通知它,
  3. 您可以使用活動 ViewModel 和觀察者模式來通知更改
  4. 單身人士雖然不推薦
  5. 事件總線實現
  6. 在啟動畫面或 MainActivity 中使用 BroadCastListener 發送和接收廣播。

您可以使用這樣的布局

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <WebView
        android:id="@+id/webView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:visibility="gone" />

    <androidx.constraintlayout.widget.ConstraintLayout
        android:id="@+id/cl_container_splash"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@mipmap/ic_launcher"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

    </androidx.constraintlayout.widget.ConstraintLayout>

</FrameLayout>
  • 框架布局(父級)
    • WebView(可見性 = 消失)
    • 啟動畫面(可見)

現在加載您的 url,然后在您的onPageFinished()調用中。

binding.webView.visibility = View.Visible
binding.root.removeView(binding.clContainerSplash)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM