GuideWebviewFragment.java 3.7 KB

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