首页 > Web开发 > 详细

[转]Embed WebView in Fragment

时间:2014-03-19 07:16:49      阅读:378      评论:0      收藏:0      [点我收藏+]

This exercise embed WebView in Fragment.

bubuko.com,布布扣

 

bubuko.com,布布扣
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();
  }
 }

}
bubuko.com,布布扣

Layout, activity_main.xml.

bubuko.com,布布扣
<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>
bubuko.com,布布扣

Layout of our fragment, layout_webfragment.xml.

bubuko.com,布布扣
<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>
bubuko.com,布布扣

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. 

[转]Embed WebView in Fragment,布布扣,bubuko.com

[转]Embed WebView in Fragment

原文:http://www.cnblogs.com/jackson-leung/p/3608467.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!