weexteam / article

This repos is a third party collection, and is not developed nor maintained by Apache Weex.
1.22k stars 141 forks source link

Weex 扩展(Extend to Android) #27

Open xkli opened 8 years ago

xkli commented 8 years ago

本文档已迁移至 https://weex-project.io/cn/references/advanced/extend-to-android.html , 此处不再维护,谢谢。

Weex 扩展(Extend to Android)

Weex 提供了扩展机制,可以根据自己的业务进行定制自己的功能。
主要分为两类扩展:

示例如下:

public class MyModule extends WXModule {

  @WXModuleAnno(runOnUIThread = true)
  public void printLog(String msg) {
    Toast.makeText(mWXSDKInstance.getContext(),msg,Toast.LENGTH_SHORT).show();
  }
}

JS 调用如下:

<template>
  <div>
    <text onclick="click">点击我测试</text>
  </div>
</template>

<script>
  module.exports = {
    methods: {
      click: function() {
        require('@weex-module/myModule').printLog("我是一个测试!");
      }
    }
  }
</script>

Component 扩展

  1. Component 扩展类必须集成WXComponent.
  2. Component 对应的设置属性的方法必须添加注解@WXComponentProp(name=value(value is attr or style of dsl))
  3. Weex sdk 通过反射调用对应的方法,所以Component对应的属性方法必须是public,并且不能被混淆。请在混淆文件中添加代码 -keep public class * extends com.taobao.weex.ui.component.WXComponent{*;}
  4. Component 扩展的方法可以使用int, double, float, String, Map, List 类型的参数
  5. 完成Component后一定要在初始化时注册 WXSDKEngine.registerComponent("richtext",RichText.class);

示例如下:

public class RichText extends WXComponent {

  public RichText(WXSDKInstance instance, WXDomObject dom, WXVContainer parent, boolean isLazy) {
    super(instance, dom, parent, isLazy);
  }

  @Override
  protected void initView() {
    mHost=new TextView(mContext);
    ((TextView)mHost).setMovementMethod(LinkMovementMethod.getInstance());
  }

  @WXComponentProp(name = "tel")
  public void setTelLink(String tel){
    SpannableString spannable=new SpannableString(tel);
    spannable.setSpan(new URLSpan("tel:"+tel),0,tel.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    ((TextView)mHost).setText(spannable);
  }
}

JS 调用如下:

<template>
  <div>
    <richText tel="12305" style="width:200;height:100">12305</text>
  </div>
</template>

Adapter扩展

图片下载: 需要时集成接口IWXImgLoaderAdapter,实现setImage 方法。 示例如下:

public class ImageAdapter implements IWXImgLoaderAdapter {

  public ImageAdapter() {
  }

  @Override
  public void setImage(final String url, final ImageView view,
                       WXImageQuality quality, WXImageStrategy strategy) {

    WXSDKManager.getInstance().postOnUiThread(new Runnable() {

      @Override
      public void run() {
        if(view==null||view.getLayoutParams()==null){
          return;
        }
        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)
            .into(view);
      }
    },0);
  }
}

注:工程要添加依赖 compile 'com.squareup.picasso:picasso:2.5.2'

lvscar commented 8 years ago

another chinese translation Android扩展 by @wangrunxiang

liuguangli commented 7 years ago

可以自己扩展实现网络请求么。

Jinjiang commented 7 years ago

@liuguangli 可以

cnx2016 commented 7 years ago

这不是三类扩展么?

jiantao88 commented 7 years ago

这个例子RichText运行时 android端无法显示啊,按照demo里操作的,Weex那还需要引用么?

DoranYun commented 7 years ago

@liuguangli 可以,不过 Weex stream module 支持网络请求 http://alibaba.github.io/weex/cn/doc/modules/modal.html

yulinho commented 7 years ago

Module 扩展 可以用 callback.invoke 回调,那 Component 扩展如何实现回调 WEEX 里面的方法?

yulinho commented 7 years ago

可以了,参考 web 组件源码,用 fireevent 实现。

DoranYun commented 7 years ago

本文档已迁移至 https://weex-project.io/cn/references/advanced/extend-to-android.html , 此处不再维护,谢谢。

AllenVork commented 7 years ago

新地址404。我想问 调用 RichText 的 .we 代码是应该转换成 .js 代码然后调用的吧,但是这个转换异常啊