weex sdk support Module extend, Weex SDK provides only rendering capabilities, rather than have other capabilities, such as network, picture, and URL redirection. If you want the these features, you need to implement it.
For example: If you want to implement an address jumping function, you can achieve a Module Follow the steps below.
Refer to the following example:
public class WXEventModule extends WXModule{ private static final String WEEX_CATEGORY="com.taobao.android.intent.category.WEEX"; @JSMethod public void openURL(String url){ //implement your module logic here } }
WXSDKEngine.registerModule("event", WXEventModule.class);
Now event
moudle is avaiable in weex, use the module like this:
var event = require('@weex-module/event'); event.openURL("http://www.github.com");
If the module need implement a callback to javascript, you just add JSCallback
argument to the method you want expose to javascript:
@JSMethod public void openURL(String url,JSCallback callback){ //implement your module logic here Map<String,Object> resp = new HashMap(); resp.put("result","ok"); callback.invoke(resp); }
At the javascript side, call the module with javascript function to receive callback data:
event.openURL("http://www.github.com",function(resp){ console.log(resp.result); });
There are label, image, div, scroll, ect. components in weex, you can also customize your own components.
Refer to the following example
public class MyViewComponent extends WXComponent{ public MyViewComponent(WXSDKInstance instance, WXDomObject dom, WXVContainer parent, String instanceId, boolean isLazy) { public MyViewComponent(WXSDKInstance instance, WXDomObject dom, WXVContainer parent, String instanceId, boolean isLazy) { super(instance, dom, parent, instanceId, isLazy); } @Override protected void initView() { mHost = new TextView(mContext); } @WXComponentProp(name=WXDomPropConstant.WX_ATTR_VALUE) public void setMyViewValue(String value) { ((TextView)mHost).setText(value); } }
WXSDKEngine.registerComponent("MyView", MyViewComponent.class);
Weex SDK has no image download capability, you need to implement IWXImgLoaderAdapter
. Refer to the following examples.
public class ImageAdapter implements IWXImgLoaderAdapter { private Activity mContext; public ImageAdapter(Activity activity) { mContext = activity; } @Override public void setImage(final String url, final ImageView view, WXImageQuality quality, WXImageStrategy strategy) { mContext.runOnUiThread(new Runnable() { @Override public void run() { if (TextUtils.isEmpty(url)) { view.setImageBitmap(null); return; } String temp = url; if (url.startsWith("//")){ temp = "http:" + url; } if (view.getLayoutParams().width<=0 || view.getLayoutParams().height<=0) { return; } Picasso.with(WXEnvironment.getApplication()) .load(temp) .resize(view.getLayoutParams().width, view.getLayoutParams().height).into(view); } }); } }
from WeexSDK 0.9.5
, you can define your component method
for example, define a method in component:
@JSMethod public void focus(){ //method implementation }
after your registration for your own custom component, now you can call it in your js file.
<template> <mycomponent id='mycomponent'></mycomponent> </template> <script> module.exports = { created: function() { this.$el('mycomponent').focus(); } } </script>