Java tutorial
/* * Copyright 2017 lizhaotailang * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.example.nq.journalism_master.innerbrowser; import android.content.Intent; import android.net.Uri; import android.os.Bundle; import android.support.annotation.Nullable; import android.support.v4.app.Fragment; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.Toolbar; import android.view.KeyEvent; import android.view.LayoutInflater; import android.view.Menu; import android.view.MenuInflater; import android.view.MenuItem; import android.view.View; import android.view.ViewGroup; import android.webkit.WebChromeClient; import android.webkit.WebResourceError; import android.webkit.WebResourceRequest; import android.webkit.WebSettings; import android.webkit.WebView; import android.webkit.WebViewClient; import android.widget.ImageView; import android.widget.ProgressBar; import android.widget.TextView; import com.example.nq.journalism_master.R; import com.example.nq.journalism_master.utils.NetworkState; /** * Created by ltnq on 2017/4/13. */ public class InnerBrowserFragment extends Fragment { private ProgressBar progressBar; private WebView webView; private Toolbar toolbar; private TextView textView; private ImageView imageView; private String url; public InnerBrowserFragment() { } public static InnerBrowserFragment getInstance() { return new InnerBrowserFragment(); } @Override public void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); url = getActivity().getIntent().getStringExtra("url"); } @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment_inner_browser, container, false); initViews(view); initWebViewSettings(webView); // if not set this, click the back arrow will call nothing setHasOptionsMenu(true); webView.setWebChromeClient(new WebChromeClient() { @Override public void onProgressChanged(WebView view, int newProgress) { changeToolbarAndProgressBar(newProgress); } }); webView.setWebViewClient(new WebViewClient() { @Override public void onReceivedError(WebView view1, WebResourceRequest request, WebResourceError error) { webView.setVisibility(View.GONE); imageView.setVisibility(View.VISIBLE); textView.setVisibility(View.VISIBLE); imageView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { if (NetworkState.networkConnected(getContext())) { webView.loadUrl(url); webView.setVisibility(View.VISIBLE); imageView.setVisibility(View.GONE); textView.setVisibility(View.GONE); } } }); } @Override public void onPageFinished(WebView view, String url) { super.onPageFinished(view, url); progressBar.setVisibility(View.GONE); } }); // WebView?html? webView.setOnKeyListener(new View.OnKeyListener() { @Override public boolean onKey(View v, int keyCode, KeyEvent event) { if (event.getAction() == KeyEvent.ACTION_DOWN) { if (keyCode == KeyEvent.KEYCODE_BACK && webView.canGoBack()) { webView.goBack(); return true; } } return false; } }); webView.loadUrl(url); return view; } private void initViews(View view) { progressBar = (ProgressBar) view.findViewById(R.id.progress_bar); webView = (WebView) view.findViewById(R.id.web_view); toolbar = (Toolbar) view.findViewById(R.id.toolbar); AppCompatActivity activity = (AppCompatActivity) getActivity(); activity.setSupportActionBar(toolbar); activity.getSupportActionBar().setDisplayHomeAsUpEnabled(true); textView = (TextView) view.findViewById(R.id.text_view); imageView = (ImageView) view.findViewById(R.id.image_view); } private void initWebViewSettings(WebView webView) { //js webView.getSettings().setJavaScriptEnabled(true); //,???? webView.getSettings().setBuiltInZoomControls(false); // webView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE); //?DOM storage API webView.getSettings().setDomStorageEnabled(true); //?application Cache webView.getSettings().setAppCacheEnabled(false); } @Override public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { inflater.inflate(R.menu.menu_browser, menu); } @Override public boolean onOptionsItemSelected(MenuItem item) { int id = item.getItemId(); if (id == android.R.id.home) { getActivity().onBackPressed(); } else if (id == R.id.action_open_in_browser) { startActivity(new Intent(Intent.ACTION_VIEW).setData(Uri.parse(webView.getUrl()))); } else if (id == R.id.action_refresh) { webView.reload(); } return super.onOptionsItemSelected(item); } private void changeToolbarAndProgressBar(int newProgress) { progressBar.setVisibility(View.VISIBLE); progressBar.setMax(100); progressBar.setProgress(newProgress); toolbar.setTitle(webView.getTitle()); } }