• [转]Embed WebView in Fragment


    This exercise embed WebView in Fragment.

    package com.example.androidwebviewfragment;
    
    import android.os.Bundle;
    import android.app.Activity;
    import android.app.Fragment;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.webkit.WebView;
    import android.webkit.WebViewClient;
    
    public class MainActivity extends Activity {
    
     static public class MyWebViewFragment extends Fragment {
      
      WebView myWebView;
      final static String myBlogAddr = "http://android-er.blogspot.com";
      String myUrl;
      
    
      @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
       View view = inflater.inflate(R.layout.layout_webfragment, container, false);
       myWebView = (WebView)view.findViewById(R.id.mywebview);
       
       myWebView.getSettings().setJavaScriptEnabled(true);                
       myWebView.setWebViewClient(new MyWebViewClient());
       
       if(myUrl == null){
        myUrl = myBlogAddr;
       }
       myWebView.loadUrl(myUrl);
           
             return view;
    
      }
      
      private class MyWebViewClient extends WebViewClient {
             @Override
             public boolean shouldOverrideUrlLoading(WebView view, String url) {
              myUrl = url;
                 view.loadUrl(url);
                 return true;
             }
         }
    
      @Override
      public void onActivityCreated(Bundle savedInstanceState) {
       super.onActivityCreated(savedInstanceState);
       setRetainInstance(true);
      }
    
     }
    
     @Override
     protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
     }
    
     @Override
     public void onBackPressed() {
      MyWebViewFragment fragment = 
        (MyWebViewFragment)getFragmentManager().findFragmentById(R.id.myweb_fragment);
      WebView webView = fragment.myWebView;
      
      if(webView.canGoBack()){
       webView.goBack();
      }else{
       super.onBackPressed();
      }
     }
    
    }

    Layout, activity_main.xml.

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        tools:context=".MainActivity" >
    
        <fragment
            android:name="com.example.androidwebviewfragment.MainActivity$MyWebViewFragment"
            android:id="@+id/myweb_fragment"
            android:layout_height="match_parent"
            android:layout_width="match_parent" />
    
    </RelativeLayout>

    Layout of our fragment, layout_webfragment.xml.

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >
    
        <WebView
            android:id="@+id/mywebview"
            android:layout_height="match_parent"
            android:layout_width="match_parent" />
    
    </LinearLayout>

    Permission of "android.permission.INTERNET" is need. 

    - I can't implement using WebViewFragment, so this exercise extends Fragment, not WebViewFragment. 
    - In this implement, the WebView can load the last loaded address after orientation changed, but cannot keep the navigation history. 

  • 相关阅读:
    在ASP.NET Core中使用TagHelpers
    ASP.NET Core使用Redis
    ASP.NET Core MVC中视图
    HTTP状态码
    ASP.NET Core中静态文件
    ASP.NET Core中使用依赖注入
    在ASP.NET Core中使用多个环境
    ASP.NET Core读取配置文件
    HTML+CSS解决高度塌陷和垂直重叠
    Element-UI的表格合计行的列添加操作按钮
  • 原文地址:https://www.cnblogs.com/jackson-leung/p/3608467.html
Copyright © 2020-2023  润新知