Commit 72b3dc78 authored by wanglei's avatar wanglei

...

parent 012a9eb7
...@@ -16,16 +16,19 @@ import android.webkit.WebSettings ...@@ -16,16 +16,19 @@ import android.webkit.WebSettings
import android.webkit.WebStorage import android.webkit.WebStorage
import android.webkit.WebView import android.webkit.WebView
import android.webkit.WebViewClient import android.webkit.WebViewClient
import androidx.lifecycle.lifecycleScope
import com.base.browserwhite.databinding.FragmentWebViewBinding import com.base.browserwhite.databinding.FragmentWebViewBinding
import com.base.browserwhite.utils.LogEx import com.base.browserwhite.utils.LogEx
import kotlinx.coroutines.Job import kotlinx.coroutines.Job
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import kotlin.random.Random
class WebViewFragment(val url: String) : BaseFragment<FragmentWebViewBinding>() { class WebViewFragment(val url: String) : BaseFragment<FragmentWebViewBinding>() {
private val TAG = "WebViewFragment" private val TAG = "WebViewFragment"
private var job: Job? = null private var job: Job? = null
private var searchAction: (context: String) -> String = HomeFragment.Companion::getBaiduSearch
override val binding: FragmentWebViewBinding by lazy { override val binding: FragmentWebViewBinding by lazy {
FragmentWebViewBinding.inflate(layoutInflater) FragmentWebViewBinding.inflate(layoutInflater)
...@@ -44,6 +47,14 @@ class WebViewFragment(val url: String) : BaseFragment<FragmentWebViewBinding>() ...@@ -44,6 +47,14 @@ class WebViewFragment(val url: String) : BaseFragment<FragmentWebViewBinding>()
binding.tvReload.setOnClickListener { binding.tvReload.setOnClickListener {
reloadWebView() reloadWebView()
} }
binding.refreshLayout.setOnRefreshListener {
lifecycleScope.launch {
binding.refreshLayout.isRefreshing = true
delay(Random.nextLong(1500, 2500))
binding.refreshLayout.isRefreshing = false
}
reloadWebView()
}
} }
private fun reloadWebView() { private fun reloadWebView() {
...@@ -161,29 +172,30 @@ class WebViewFragment(val url: String) : BaseFragment<FragmentWebViewBinding>() ...@@ -161,29 +172,30 @@ class WebViewFragment(val url: String) : BaseFragment<FragmentWebViewBinding>()
override fun onReceivedError(view: WebView?, request: WebResourceRequest?, error: WebResourceError?) { override fun onReceivedError(view: WebView?, request: WebResourceRequest?, error: WebResourceError?) {
LogEx.logDebug(TAG, "onReceivedError ${error?.errorCode} ${error?.description}") LogEx.logDebug(TAG, "onReceivedError ${error?.errorCode} ${error?.description}")
super.onReceivedError(view, request, error) super.onReceivedError(view, request, error)
if (error?.errorCode == -1) { return
//-1 net::ERR_BLOCKED_BY_ORB 跨域读取不影响显示 // if (error?.errorCode == -1) {
return // //-1 net::ERR_BLOCKED_BY_ORB 跨域读取不影响显示
} // return
if (error?.errorCode == -6) { // }
//-6 net::ERR_BLOCKED_BY_ORB 服务器拒绝连接,是服务的锅 // if (error?.errorCode == -6) {
return // //-6 net::ERR_BLOCKED_BY_ORB 服务器拒绝连接,是服务的锅
} // return
job?.cancel() // }
binding.webView.visibility = View.GONE // job?.cancel()
binding.llError.visibility = View.VISIBLE // binding.webView.visibility = View.GONE
if (error != null) { // binding.llError.visibility = View.VISIBLE
// 获取错误码 // if (error != null) {
val errorCode = error.errorCode // // 获取错误码
// 获取错误描述 // val errorCode = error.errorCode
val description = error.description // // 获取错误描述
// val description = error.description
// 创建错误信息文本 //
val errorText = "Error ${errorCode}: $description" // // 创建错误信息文本
// val errorText = "Error ${errorCode}: $description"
// 打印错误信息,或者根据需要进行其他处理 //
binding.tvErrorReason.text = errorText // // 打印错误信息,或者根据需要进行其他处理
} // binding.tvErrorReason.text = errorText
// }
} }
} }
......
...@@ -16,8 +16,9 @@ ...@@ -16,8 +16,9 @@
android:progressDrawable="@drawable/progress_bar_web" android:progressDrawable="@drawable/progress_bar_web"
tools:progress="50" /> tools:progress="50" />
<FrameLayout <androidx.swiperefreshlayout.widget.SwipeRefreshLayout
android:layout_width="match_parent" android:layout_width="match_parent"
android:id="@+id/refresh_layout"
android:layout_height="match_parent"> android:layout_height="match_parent">
<WebView <WebView
...@@ -77,7 +78,7 @@ ...@@ -77,7 +78,7 @@
</LinearLayout> </LinearLayout>
</FrameLayout> </androidx.swiperefreshlayout.widget.SwipeRefreshLayout>
</LinearLayout> </LinearLayout>
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment