之前可以直接使用CordovaWebView,之后的版本CordovaWebView作为一个 interface,很尴尬。。
我用的cordova的版本是 7.1.0,可以模仿CordovaActivity的实现。
参考实现:
https://github.com/Adobe-Marketing-Cloud-Apps/app-sample-android-phonegap/blob/master/platforms/android/src/info/geometrixx/webviewapp/HomeFragment.java
package com.sia.don;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.RelativeLayout;
import org.apache.cordova.ConfigXmlParser;
import org.apache.cordova.CordovaInterfaceImpl;
import org.apache.cordova.CordovaPreferences;
import org.apache.cordova.CordovaWebView;
import org.apache.cordova.CordovaWebViewImpl;
import org.apache.cordova.PluginEntry;
import java.util.ArrayList;
/**
* Created by SiaDon 2018/3/9.
*/
public class CordovaFragment extends Fragment {
protected CordovaWebView webView;
protected CordovaPreferences preferences;
protected String launchUrl;
protected ArrayList<PluginEntry> pluginEntries;
protected CordovaInterfaceImpl cordovaInterface;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
// rootView will contain cordova web view
View rootView = inflater.inflate(R.layout.activity_main_fragment_cordova, container, false);
cordovaInterface = new CordovaInterfaceImpl(getActivity());
if (savedInstanceState != null)
cordovaInterface.restoreInstanceState(savedInstanceState);
loadConfig();
// new web view
webView = new CordovaWebViewImpl(CordovaWebViewImpl.createEngine(getActivity(), preferences));
RelativeLayout.LayoutParams wvlp = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.MATCH_PARENT);
webView.getView().setLayoutParams(wvlp);
// init
if (!webView.isInitialized()) {
webView.init(cordovaInterface, pluginEntries, preferences);
}
cordovaInterface.onCordovaInit(webView.getPluginManager());
// your Url
webView.loadUrl(launchUrl);
((FrameLayout) rootView).addView(webView.getView());
return rootView;
}
protected void loadConfig() {
ConfigXmlParser parser = new ConfigXmlParser();
parser.parse(getActivity());
preferences = parser.getPreferences();
preferences.setPreferencesBundle(getActivity().getIntent().getExtras());
// set config.xml
launchUrl = parser.getLaunchUrl();
pluginEntries = parser.getPluginEntries();
}
}
activity_main_fragment_cordova.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
网友评论