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

How to customize a native Component ? #57

Open pihunwang opened 8 years ago

pihunwang commented 8 years ago

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

概述

Weex包含了很多重要的控件,比如ScrollView,ListView,Text,Imageview等,但是这些控件也许不能满足你的需求,还有很多原生控件和自定义控件需要移植到Weex上,幸运的是,我们可以很容易地定义自己的components来满足需要。

详细步骤

1.自定义components一定要继承自WXComponent或者WXContainer

2.weex SDK只能识别@WXComponentProp(name=value(value is attr or style of dsl))这个注解

3.需要暴露的方法必须是public修饰的

4.component类不能是内部类

5.自定义components类不能被混淆

6.component中的方法会在UI线程中被调用,所以不要包含耗时逻辑

7.Weex的参数可以是int,double,float,String,Map,List,以及实现WXObject接口的类。

例子

package com.taobao.weex.ui.component;
……

public class  MyViewComponent extends WXComponent{

       public MyViewComponent(WXSDKInstance instance, WXDomObject node, 
                    WXVContainer parent,  String instanceId, boolean lazy) {                
           super(instance, node, parent, instanceId, lazy);
        }

       @Override
       protected void initView() {
          //TODO:your own code ……
       }

      @Override
      public WXFrameLayout getView() {
         //TODO:your own code ………        
      }
      @WXComponentProp(name=WXDomPropConstant.WX_ATTR_VALUE)
      public void setMyViewValue(String value) {
         ((TextView)mHost).setText(value);
      }

}

注册Component

WXSDKEngine.registerComponent("MyView", MyViewComponent.class);
wzj583585700 commented 7 years ago

iOS的有吗

yundongbot commented 7 years ago

@wzj583585700 Hi, 可以参考新文档 http://alibaba.github.io/weex/cn/doc/how-to/customize-a-native-component.html

yundongbot commented 7 years ago

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