WebView空白屏幕

时间:2018-09-13 04:21:58

标签: android android-webview

我在DialogFragment内有一个Webview,该WebView使用 http://drive.google.com/viewerng/viewer?embedded=true&url 显示文档和PDF,但尝试很少,但是如果用户尝试打开和关闭Dialog,则webview经常显示白屏

我已经尝试了与此问题相关的所有线程link1 link2,没有人能解决问题

我的情况是,当我连续尝试多次时

  public class FileViewDialog extends DialogFragment implements 
   View.OnClickListener {
    private static final String TAG = "FileViewDialog";
    private static final String PARAM_1  = "param_one";
    private static final String PARAM_2  = "param_two";
    private static final String PARAM_3  = "param_three";

    private ArrayList<ImageListItem> imagesList = new ArrayList<>();
    private CustomViewPager viewPager;
    private View rlDelete, rlReplace ,rlDownload;
    private int currentItem = 0;
    private LinearLayout llIndicators ;
    private FileViewDialog.FileViewDialogContract dialogContract;
    private boolean isAttachment;
    private AdaptiveTableLayout adaptiveTableLayout;
    private View csvView;
    private WebView webView;
    private TextView tvInfo;
    private long downloadRef;
    String fileUrl;
    private ProgressBar progressBarWeb;
    private ProgressBar progressBar;

    public static FileViewDialog getInstance(ArrayList<ImageListItem> imagesList, int currentItem, boolean isAttachment) {
        FileViewDialog videoPlayerDialog = new FileViewDialog();
        Bundle argument = new Bundle();
        argument.putParcelableArrayList(PARAM_1, imagesList);
        argument.putInt(PARAM_2, currentItem);
        argument.putBoolean(PARAM_3, isAttachment);
        videoPlayerDialog.setArguments(argument);
        return videoPlayerDialog;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            imagesList = getArguments().getParcelableArrayList(PARAM_1);
            currentItem = getArguments().getInt(PARAM_2);
            isAttachment = getArguments().getBoolean(PARAM_3);
        }
        fileUrl = imagesList.get(currentItem).getImageUrl();
        setStyle(DialogFragment.STYLE_NORMAL, android.R.style.Theme_Black_NoTitleBar);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        getDialog().getWindow().getAttributes().windowAnimations = R.style.CustomDialog;
    }

    @Override
    public void onStart() {
        super.onStart();
        Dialog dialog = getDialog();
        if (dialog != null)
        {
            int width = ViewGroup.LayoutParams.MATCH_PARENT;
            int height = ViewGroup.LayoutParams.MATCH_PARENT;
            dialog.getWindow().setLayout(width, height);
        }
    }


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_file_view, container);
    }
    private boolean isCSVFile(final String imageURL) {
        return imageURL != null && imageURL.contains(".csv");
    }




    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE);
        View rlBack = view.findViewById(R.id.rlBack);
        rlBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
        progressBar = view.findViewById(R.id.csv_loading);
        webView = view.findViewById(R.id.webviewFile);
        tvInfo = view.findViewById(R.id.tvFileInfo);
        progressBarWeb = view.findViewById(R.id.loader);
        webView.setVisibility(isCSVFile(fileUrl) ? View.GONE : View.VISIBLE);
        webView.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
        Utils.setOnClickListener(this, rlDelete, rlReplace, rlDownload);
        llIndicators = view.findViewById(R.id.llIndicators);
        inflateIndicators(currentItem, llIndicators);
        if (fileUrl == null) {
            webView.setVisibility(View.GONE);
            tvInfo.setVisibility(View.VISIBLE);
            tvInfo.setText(R.string.msg_file_error);
        }
        removeAllCookies(getContext());
        progressBarWeb.setVisibility(View.VISIBLE);
        webView.setWebViewClient(new MyBrowser());

        // To be used for Telling that we are Loading
        webView.getSettings().setJavaScriptEnabled(true);
        try {
            fileUrl = URLEncoder.encode(fileUrl, "UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        webView.loadUrl("http://drive.google.com/viewerng/viewer?embedded=true&url=" + fileUrl);

    }

    private void inflateIndicators(int position, LinearLayout llIndicators) {

        llIndicators.removeAllViews();

        int pixels = Utils.convertDpToPixels(getActivity(), 8);
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(pixels, pixels);

        for (int index = 0; index < imagesList.size(); index++) {
            View view = new View(getActivity());
            view.setLayoutParams(params);
            view.setBackgroundResource(index == position ? R.drawable.switcher_filled : R.drawable.switcher);
            llIndicators.addView(view);
        }
    }




    @TargetApi(Build.VERSION_CODES.N)
    private class MyBrowser extends WebViewClient {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest url) {
            view.loadUrl(url.getUrl().toString());
            return true;
        }

        @Override
        public void onPageFinished(WebView view, String url) {
            webView.setVisibility(View.VISIBLE);
            progressBarWeb.setVisibility(View.GONE);
            super.onPageFinished(view, url);
        }


    }
    private static void removeAllCookies(final Context context) {
      //On Lollipop and beyond, the CookieManager singleton works fine by itself.
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
            CookieSyncManager.createInstance(context);
        }
        final CookieManager cookieManager = CookieManager.getInstance();
        cookieManager.removeAllCookie();
    }

}

0 个答案:

没有答案