WebViewFragment.java
2.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package com.polysoft.nafmii.fragment;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.ValueCallback;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import com.polysoft.nafmii.R;
import com.polysoft.nafmii.webview.JavaToJsinterface;
import com.polysoft.nafmii.webview.JsToJavaInterface;
import com.polysoft.nafmii.webview.MyWebViewClient;
import com.polysoft.nafmii.webview.WebviewLoadListener;
import com.polysoft.nafmii.webview.X5WebviewUtils;
import com.tencent.smtt.sdk.WebView;
public class WebViewFragment extends Fragment implements WebviewLoadListener {
private View rootView;
private ViewGroup layout;
private WebView webView;
public static WebViewFragment newInstances(String url) {
WebViewFragment fragment = new WebViewFragment();
Bundle bundle = new Bundle();
bundle.putString("url", url);
fragment.setArguments(bundle);
return fragment;
}
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater,
@Nullable ViewGroup container,
@Nullable Bundle savedInstanceState) {
if (null == this.rootView) {
this.rootView = inflater.inflate(R.layout.fragment_webview, container, false);
}
return this.rootView;
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
this.layout = view.findViewById(R.id.webview_layout);
this.initWebview();
}
private void initWebview() {
WebView webView = X5WebviewUtils.create(getActivity());
webView.setWebViewClient(new MyWebViewClient(this));
JsToJavaInterface nativeApi = new JsToJavaInterface(getActivity(), new JavaToJsinterface(webView));
webView.addJavascriptInterface(nativeApi, "$NativeApi");
this.layout.removeAllViews();
int match = ViewGroup.LayoutParams.MATCH_PARENT;
this.layout.addView(webView, new ViewGroup.LayoutParams(match, match));
this.webView = webView;
Bundle bundle = getArguments();
String url = bundle.getString("url", "");
this.webView.loadUrl(url);
}
@Override
public void onDestroy() {
super.onDestroy();
X5WebviewUtils.destroy(this.webView);
}
@Override
public void onWebviewLoad(int type, Object param) {
if (type == WebviewLoadListener.TYPE_FINISH) {
} else if (type == WebviewLoadListener.TYPE_ERROR) {
}
}
@Override
public void onShowFileChooser(ValueCallback<Uri[]> filePathCallback, String[] acceptTypes, boolean isCapture) {
}
}