如何在Fragment中使用phonegap的CordovaWebView

就为这个问题翻遍国内外各大论坛  功夫不负有心人 终于在github上找到答案

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

废话不多说直接上代码

希望能帮助到需要的朋友

public class FragmentHtml extends Fragment implements CordovaInterface {

	private CordovaWebView webView = null;

	public void setSlidingMenu(SlidingMenu slidingMenu) {
		this.slidingMenu = slidingMenu;
	}

	public static FragmentHtml newInstance() {
		FragmentHtml fragment = new FragmentHtml();
		return fragment;
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		context = inflater.getContext();
		LayoutInflater localInflater = inflater.cloneInContext(new CordovaContext(getActivity(), this));
		View rootView = localInflater.inflate(R.layout.fragment_html, container, false);
		webView = (CordovaWebView) rootView.findViewById(R.id.web_report);
		Config.init(getActivity());
		//webView.loadUrl(Config.getStartUrl());
                webView.loadUrl("file:///android_asset/www/index.htm");
                return rootView;

	}

	// Plugin to call when activity result is received
	protected CordovaPlugin activityResultCallback = null;
	protected boolean activityResultKeepRunning;

	// Keep app running when pause is received. (default = true)
	// If true, then the JavaScript and native code continue to run in the
	// background
	// when another application (activity) is started.
	protected boolean keepRunning = true;

	private final ExecutorService threadPool = Executors.newCachedThreadPool();

	public Object onMessage(String id, Object data) {
		return null;
	}

	public void onDestroy() {
		super.onDestroy();
		if (webView.pluginManager != null) {
			webView.pluginManager.onDestroy();
		}
	}

	@Override
	public ExecutorService getThreadPool() {
		return threadPool;
	}

	@Override
	public void setActivityResultCallback(CordovaPlugin plugin) {
		this.activityResultCallback = plugin;
	}

	public void startActivityForResult(CordovaPlugin command, Intent intent, int requestCode) {
		this.activityResultCallback = command;
		this.activityResultKeepRunning = this.keepRunning;
		// If multitasking turned on, then disable it for activities that return
		// results
		if (command != null) {
			this.keepRunning = false;
		}
		// Start activity
		super.startActivityForResult(intent, requestCode);
	}

	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent intent) {
		super.onActivityResult(requestCode, resultCode, intent);
		CordovaPlugin callback = this.activityResultCallback;
		if (callback != null) {
			callback.onActivityResult(requestCode, resultCode, intent);
		}
	}

	private class CordovaContext extends ContextWrapper implements CordovaInterface {
		CordovaInterface cordova;

		public CordovaContext(Context base, CordovaInterface cordova) {
			super(base);
			this.cordova = cordova;
		}

		public void startActivityForResult(CordovaPlugin command, Intent intent, int requestCode) {
			cordova.startActivityForResult(command, intent, requestCode);
		}

		public void setActivityResultCallback(CordovaPlugin plugin) {
			cordova.setActivityResultCallback(plugin);
		}

		public Activity getActivity() {
			return cordova.getActivity();
		}

		public Object onMessage(String id, Object data) {
			return cordova.onMessage(id, data);
		}

		public ExecutorService getThreadPool() {
			return cordova.getThreadPool();
		}
	}

}
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="0dp">

    <org.apache.cordova.CordovaWebView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:id = "@+id/rl_title"/>

</LinearLayout>



  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 10
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 10
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值