首先说明一下为什么要使用 CordovaWebView 而不直接使用 WebView 呢?由于 Android4.4 版本之后对 WebView 做了很大的改变,具体参考

这篇文章:理解WebKit和Chromium: Android 4.4 上的Chromium WebView,这样的改变会出现这样的问题,也是我遇到的问题:webview4.4以上版本使用loadurl加载过长js文件失效问题

网上也有相关的解决方法,但是感觉很麻烦,而且解决不了我的问题,最后想了想,我们在使用 PhoneGap 的时候有一个继承 WebView 的 CordovaWebView 组件

已经对于这样的问题做了很好的处理,所以为何不直接用咧,所以就有了这篇文章的: Fragment里使用 PhoneGap 的 CordovaWebView

实现一个类继承 PhoneGap 的接口:CordovaInterface

<span style="font-size:14px;">import android.app.Activity;
import android.content.ContextWrapper;
import android.content.Intent; import org.apache.cordova.CordovaInterface;
import org.apache.cordova.CordovaPlugin; import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors; /**
* Created by long on 2016/5/30.
*/
public class CordovaContext extends ContextWrapper implements CordovaInterface {
Activity activity;
protected final ExecutorService threadPool = Executors.newCachedThreadPool(); public CordovaContext(Activity activity) {
super(activity.getBaseContext());
this.activity = activity;
} @Override
public void startActivityForResult(CordovaPlugin cordovaPlugin, Intent intent, int i) {
} @Override
public void setActivityResultCallback(CordovaPlugin cordovaPlugin) {
} @Override
public Activity getActivity() {
return activity;
} @Override
public Object onMessage(String s, Object o) {
return null;
} @Override
public ExecutorService getThreadPool() {
return threadPool;
}
} </span>
<span style="font-size:14px;">import android.app.Fragment;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView; import org.apache.cordova.Config;
import org.apache.cordova.CordovaWebView; public class HomeFragment extends Fragment {
private View view = null; @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) { LayoutInflater localInflater = inflater.cloneInContext(new CordovaContext(this.getActivity()));
view = localInflater.inflate(R.layout.fragment_cordova, container, false); CordovaWebView webView = (CordovaWebView) view.findViewById(R.id.id_cordovaWebView);
Config.init(getActivity());
webView.loadUrl("file:///android_asset/www/index_home.html"); } return view;
} } </span>

相关文章分享:

http://www.lai18.com/content/1978961.html

https://github.com/Adobe-Marketing-Cloud-Apps/app-sample-android-phonegap/wiki/Embed-Webview-in-Android-Fragment

Fragment里使用 PhoneGap 的 CordovaWebView的更多相关文章

  1. sessionId在fragment里无法保存的问题

    fragment页面需要验证用户是否登录.若没登陆调用登录页面,后返回fragment. 这个问题解决 sessionId = SharePreferenceUtils.getSessionId(); ...

  2. ViewPager和Fragment的结合使用fragment里包含着listview的常见问题

    在我们开发的过程中可能会遇到类似需求,我们需要做一个左右滑动的tab导航,这个时候有些朋友可能会想到viewpager和fragment的结合来实现这个功能:当然实现的方法不单单着一种.我们这个随笔讨 ...

  3. Android笔记:如何在Fragment里使用findViewById()方法?

    在Activity中,可以直接用findViewById(int id),通过xml的id来找到对应的View. 查找官方的api,具体如下: https://developer.android.go ...

  4. Fragment里面的ViewPager嵌套subFragment,主Fragment切换的时候subFragment出现空白Fragment的Bug

    Fragment第二次进入不显示,主要是第二次加载的时候重复调用了onCreateView()这个方法,重新new了一个pageadapter导致子fragment不显示,问题的解决方法就是在onCr ...

  5. 如何获取fragment里的控件

    不能在onCreate函数中获取控件,以为fragment还没有start,你可以在onStart函数中获取: @Overrideprotected void onStart() { super.on ...

  6. Fragment里面的ListView的item点击没反应

    近期做一个消息列表,发现fragment的listview载入消息出来后,每条消息都点击不了.经过询问和查下已解决 开发中非经常见的一个问题,项目中的listview不不过简单的文字,经常须要自定义l ...

  7. android 在Fragment里添加Theme主题

    @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanc ...

  8. Android Fragment使用(三) Activity, Fragment, WebView的状态保存和恢复

    Android中的状态保存和恢复 Android中的状态保存和恢复, 包括Activity和Fragment以及其中View的状态处理. Activity的状态除了其中的View和Fragment的状 ...

  9. Android Fragment使用(二) 嵌套Fragments (Nested Fragments) 的使用及常见错误

    嵌套Fragment的使用及常见错误 嵌套Fragments (Nested Fragments), 是在Fragment内部又添加Fragment. 使用时, 主要要依靠宿主Fragment的 ge ...

随机推荐

  1. 【转】谈谈 iOS 中图片的解压缩

    转自:http://blog.leichunfeng.com/blog/2017/02/20/talking-about-the-decompression-of-the-image-in-ios/ ...

  2. Develop Android Game Using Cocos2d-x

    0. Environment Windows 7 x64Visual Studio 2013adt-bundle-windows-x86 (http://developer.android.com/s ...

  3. 【APUE】Chapter3 File I/O

    这章主要讲了几类unbuffered I/O函数的用法和设计思路. 3.2 File Descriptors fd本质上是非负整数,当我们执行open或create的时候,kernel向进程返回一个f ...

  4. Ubuntu下使用Git_2

    接着上一篇的写,这里练习一下git clone 指令 指令格式 $ git clone <repository> <directory> <respository> ...

  5. QSS 的选择器

    本文连接地址:http://www.qtdebug.com/QSS-Selector.html 选择器决定了 style sheet 作用于哪些 Widget,QSS 支持 CSS2 定义的所有选择器 ...

  6. linux下 su 与 su - 的区别和使用

    Linux下su与su -命令的区别   在启动服务器ntpd服务时遇到一个问题 使用 su root 切换到root用户后,不可以使用service命令: 使用 su - 后,就可以使用servic ...

  7. Struts2—整合Spring

    Struts2—整合Spring Spring框架是一个非常优秀的轻量级java EE容器,大部分javaEE应用,都会考虑使用Spring容器来管理应用中的组件. Struts2是一个MVC框架,是 ...

  8. 官方文档 恢复备份指南四 Starting and Interacting with the RMAN Client

    本章讲: Starting and Exiting RMAN Specifying the Location of RMAN Output                                ...

  9. Android stateMachine分析

    StateMachine与State模式的详细介绍可以参考文章:Android学习 StateMachine与State模式 下面是我对于StateMachine的理解: 先了解下消息处理.看下Sta ...

  10. 【iOS开发】UIView之userInteractionEnabled属性介绍

    http://my.oschina.net/hmj/blog/108002 属性作用 该属性值为布尔类型,如属性本身的名称所释,该属性决定UIView是否接受并响应用户的交互. 当值设置为NO后,UI ...