GuideWebviewFragment.java 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. package com.epson.mobilephone.common.guide;
  2. import android.content.Context;
  3. import android.os.Bundle;
  4. import android.support.p000v4.app.Fragment;
  5. import android.view.LayoutInflater;
  6. import android.view.View;
  7. import android.view.ViewGroup;
  8. import android.webkit.WebView;
  9. import android.widget.FrameLayout;
  10. import androidx.fragment.app.Fragment;
  11. import com.epson.guidelib.R;
  12. public class GuideWebviewFragment extends Fragment {
  13. private static final String ARG_PARAM1 = "html_file_path";
  14. MyClickListener mClickListerner;
  15. private String mHtmlPath;
  16. private WebView mWebView;
  17. private FrameLayout mWebViewFrame;
  18. public interface MyClickListener {
  19. void onClickClose();
  20. }
  21. public static GuideWebviewFragment newInstance(String str) {
  22. GuideWebviewFragment guideWebviewFragment = new GuideWebviewFragment();
  23. Bundle bundle = new Bundle();
  24. bundle.putString(ARG_PARAM1, str);
  25. guideWebviewFragment.setArguments(bundle);
  26. return guideWebviewFragment;
  27. }
  28. public void onCreate(Bundle bundle) {
  29. super.onCreate(bundle);
  30. if (getArguments() != null) {
  31. mHtmlPath = getArguments().getString(ARG_PARAM1);
  32. }
  33. }
  34. public View onCreateView(LayoutInflater layoutInflater, ViewGroup viewGroup, Bundle bundle) {
  35. View inflate = layoutInflater.inflate(R.layout.fragment_guide_webview, viewGroup, false);
  36. mWebView = (WebView) inflate.findViewById(R.id.guideWebview);
  37. mWebViewFrame = (FrameLayout) inflate.findViewById(R.id.guideWebViewFrame);
  38. ViewGroup.MarginLayoutParams marginLayoutParams = (ViewGroup.MarginLayoutParams) mWebView.getLayoutParams();
  39. ViewGroup.MarginLayoutParams marginLayoutParams2 = (ViewGroup.MarginLayoutParams) mWebViewFrame.getLayoutParams();
  40. mWebView.getSettings().setBuiltInZoomControls(false);
  41. mWebView.getSettings().setLoadWithOverviewMode(true);
  42. mWebView.getSettings().setUseWideViewPort(true);
  43. mWebView.setInitialScale(1);
  44. mWebView.loadUrl(mHtmlPath);
  45. int webViewDP = Utils.getWebViewDP(getActivity(), getContext());
  46. marginLayoutParams.height = Utils.dptopx(webViewDP, getContext());
  47. marginLayoutParams.width = Utils.dptopx(webViewDP, getContext());
  48. marginLayoutParams2.height = Utils.dptopx(Utils.LAYOUT_MEARGIN + webViewDP, getContext());
  49. marginLayoutParams2.width = Utils.dptopx(webViewDP + Utils.LAYOUT_MEARGIN, getContext());
  50. mWebView.setLayoutParams(marginLayoutParams);
  51. mWebViewFrame.setLayoutParams(marginLayoutParams2);
  52. inflate.findViewById(R.id.closeImage).setOnClickListener(new View.OnClickListener() {
  53. public void onClick(View view) {
  54. if (GuideWebviewFragment.mClickListerner != null) {
  55. GuideWebviewFragment.mClickListerner.onClickClose();
  56. }
  57. }
  58. });
  59. return inflate;
  60. }
  61. public void onPause() {
  62. super.onPause();
  63. mWebView.onPause();
  64. }
  65. public void onResume() {
  66. mWebView.onResume();
  67. super.onResume();
  68. }
  69. public void onDestroy() {
  70. WebView webView = mWebView;
  71. if (webView != null) {
  72. webView.destroy();
  73. }
  74. mWebView = null;
  75. super.onDestroy();
  76. }
  77. public void onAttach(Context context) {
  78. super.onAttach(context);
  79. try {
  80. mClickListerner = (MyClickListener) context;
  81. } catch (ClassCastException unused) {
  82. throw new ClassCastException(getActivity().toString() + "must implement OnArticleSelectedListener.");
  83. }
  84. }
  85. public void onDetach() {
  86. super.onDetach();
  87. mClickListerner = null;
  88. }
  89. }