cordova in fragment

2018-06-27  本文已影响0人  坑爹马

新版的cordova plugin,将CordovaWebview改成了interface,所以无法作为视图在xml使用,这对于需要在fragment中使用Cordova是一个麻烦,官网也没有更新readme.md。所以这里记录下自己的实现.这里有些区别 由于项目需要我把cordova内核换成了腾讯x5,所以这里的webview是X5WebViewwebView。

      public class TestFragmentextends Fragment {

      private X5WebViewwebView;

      private CordovaWebViewwebInterface;

      private CordovaInterfaceImplcordovaInterface;

      private ConfigXmlParserparser;

      @Override

      public void onAttach(Context context) {

      super.onAttach(context);

      cordovaInterface =new TestCordovaInterfaceImpl(this.getActivity(),this);

      parser =new ConfigXmlParser();

      parser.parse(this.getActivity());

      }

      @Override

   public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

  View view = inflater.inflate(R.layout.test_fragment_webview, container,false);

//这里在xml中引用X5WebView替换webview

  webView = view.findViewById(R.id.webview);

webInterface =new CordovaWebViewImpl(new X5WebViewEngine(webView));

  webInterface.init(cordovaInterface,parser.getPluginEntries(),parser.getPreferences());

  this.loadUrl(this.getWebviewUrl());

  return view;

}

@Override

public void onActivityResult(int requestCode,int resultCode, Intent intent) {

super.onActivityResult(requestCode, resultCode, intent);

cordovaInterface.onActivityResult(requestCode, resultCode, intent);

}

@Override

public void onRequestPermissionsResult(int requestCode, String permissions[],int[] grantResults) {

try {

cordovaInterface.onRequestPermissionResult(requestCode, permissions, grantResults);

}catch (JSONException e) {

e.printStackTrace();

}

}

@Override

public void onStart() {

super.onStart();

if (this.webInterface ==null) {

return;

}

this.webInterface.handleStart();

}

@Override

public void onDestroy() {

    super.onDestroy();

    if (this.webInterface ==null) {

    return;

    }

    webInterface.handleDestroy();

}

@Override

public void onResume() {

  super.onResume();

  if (this.webInterface ==null) {

  return;

  }

  this.webInterface.handleResume(true);

 }

@Override

public void onPause() {

  super.onPause();

  if (this.webInterface ==null) {

  return;

  }

this.webInterface.handlePause(true);

}

public void loadUrl(String url) {

  if (webView !=null) {

  webView.loadUrl(url);

  }

}

private String getWebviewUrl() {

  return "http://www.baidu.com";

 }

}



public class TestCordovaInterfaceImpl extends CordovaInterfaceImpl {

  private Fragment mFragment;

  public TestCordovaInterfaceImpl(Activity activity, Fragment fragment) {

  super(activity);

  mFragment = fragment;

}

public TestCordovaInterfaceImpl(Activity activity, ExecutorService threadPool, Fragment    fragment) {

  super(activity, threadPool);

  mFragment = fragment;

}

@Override

public void startActivityForResult(CordovaPlugin command, Intent intent,int requestCode) {

  this.setActivityResultCallback(command);

  try {

  this.mFragment.startActivityForResult(intent, requestCode);

  }catch (RuntimeException e) {

    this.activityResultCallback =null;

  throw e;

}

}

}

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

    <com.xxx.x5.X5WebView
        android:id="@+id/webview"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
   </com.xxx.x5.X5WebView>

</LinearLayout>
上一篇下一篇

猜你喜欢

热点阅读