添加依赖
将weex集成到现有Android项目中,首先需要添加如下依赖
compile 'com.android.support:recyclerview-v7:23.1.1'
compile 'com.android.support:support-v4:23.1.1'
compile 'com.android.support:appcompat-v7:23.1.1'
compile 'com.alibaba:fastjson:1.1.46.android'
compile 'com.taobao.android:weex_sdk:0.5.1@aar'
(版本可更高但不可低)
添加ImageAdapter
ImageAdapter需继承IWXImgLoaderAdapter接口,该接口要求实现setImage方法,我们需在此方法中实现自己的图片加载,此处以Glide加载图片为例:
public class ImageAdapter implements IWXImgLoaderAdapter {
@Override
public void setImage(String url, ImageView view, WXImageQuality quality, WXImageStrategy strategy) {
Glide.with(WXApplication.app).
load(url).
into(view);
}
}
初始化
在Application中
InitConfig config=new InitConfig.Builder().setImgAdapter(new ImageAdapter()).build();
WXSDKEngine.initialize(this,config);
此处的setimagadapter不可省略
在activity中集成
WXSDKInstance mWXSDKInstance;
mWXSDKInstance = new WXSDKInstance(this);
mWXSDKInstance.registerRenderListener(this);
mWXSDKInstance.render("WXSample", WXFileUtils.loadFileContent("hello.js", this), null, null, -1, -1, WXRenderStrategy.APPEND_ASYNC);
注册reisterrenderlistener,实现IWXRenderListener接口,包含如下方法供不同状态回调调用
void onViewCreated(WXSDKInstance instance, View view);
void onRenderSuccess(WXSDKInstance instance, int width, int height);
void onRefreshSuccess(WXSDKInstance instance, int width, int height);
void onException(WXSDKInstance instance, String errCode, String msg);
我们在onViewCreated中执行如下方法,在js加载完毕时setcontentview
@Override
public void onViewCreated(WXSDKInstance instance, View view) {
setContentView(view);
}
最后我们通过render方法加载本地的js文件
public void render(String pageName, String template, Map<String, Object> options, String jsonInitData, int width, int height, WXRenderStrategy flag)
run~~