数据中介者--数据绑定2(data-binding)

379 阅读4分钟

关于数据中介者Data-mediator

  • 数据中介者设计之初主要是为了减少数据层的变化,以及适应其他的业务需求。 后面才有了更加强大的功能.
    这就不做其他介绍了。 有兴趣的可以看看我之前的文章或者直接看github/Data-Mediator, 里面有中文文档

阐述:DataMediator之data-binding.

  • 关于android数据绑定,相信最开始大家是从google Data-binding库开始了解的。 当然有部分童鞋是从javaweb转的,javaweb也有对应的数据绑定思想。
  • 数据绑定的好处: 一次绑定,以后所有的对对应数据的操作可以直接或者说间接的反馈到视图view上。这样就会减轻技术人员
    对view操作的负担。
  • DataMediator的优势: 由于butterknife的工作是负责id等资源的绑定,DataMediator之Data-binding负责对数据的绑定。
    两者工作完全不冲突,而且可以无缝衔接在一起。 所以加起来就是2个字--强大. 最主要的就是俩者的部分工作原理都是类似的.
    都是利用编译时注解去生成代码然后绑定到目标上。实际上dagger/dagger2, google的数据库组件也是类似的。
    另外的此框架还针对adapter的数据绑定做了支持。
    (关于不懂ButterKnife的童鞋,需要自行去github/Butterknife了解并熟悉运用。)

适用范围

  • 数据中介者可以用于常用的控件,view/TextView/ImageView等。甚至连自定义控件任意方法都支持.

数据绑定相关注解介绍

  • 属性对象改变: 属性对象指的是某个属性属于数据模型的,对应的这个属性的数据对象。
  • 1, View的注解
    • @BindBackground
      用于绑定View的背景。并且属性对象是Drawable类型的. 示例:
      @BindBackground("background") TextView title;
    • @BindBackgroundColor
      用于绑定View的背景颜色。并且属性对象是color类型的. 示例:
      @BindBackgroundColor("backgroundColor") TextView title;
    • @BindBackgroundRes
      用于绑定View的背景资源。并且属性对象是资源类型的. 示例:
      @BindBackgroundRes("backgroundRes") TextView title;
    • @BindVisibility
      用于绑定view的可见性. 并且属性对象是int或者boolean类型(取决于forceAsBoolean的返回值)的.
      @BindVisibility("visible") TextView title;
    • @BindEnable
      用于绑定控件是否启用. 并且属性对象是boolean类型的.
      java @BindEnable("enable") TextView tv;
  • 2, TextView注解
    • @BindTextColor 绑定文本颜色.
      @BindTextColor("textColor") TextView title;
    • @BindTextColorRes 绑定文本颜色资源
      @BindTextColorRes("textColorRes") TextView title;
    • @BindTextSize 绑定文本大小(默认dp为单位)
      @BindTextSize("textSize") TextView title;
    • @BindTextSizePx 绑定文本大小(以pix为单位)
      @BindTextSizePx("textSizePx") TextView title;
    • @BindTextSizeRes 绑定文本大小资源
      @BindTextSizeRes("textSizeRes") TextView title;
    • @BindTextGravity 绑定文本重心
      @BindTextGravity("textGravity") TextView title;
    • @BindText 绑定文本
      @BindText("text") TextView title;
    • @BindTextRes 绑定文本资源
      @BindTextRes("textRes") TextView title;
    • 其他的TextView注解.比如@BindHintText, @BindHintTextColor, @BindHintTextColorRes, @BindHintTextRes 都是针对hint的。
  • 3, ImageView注解介绍。

    • @BindImageUrl 绑定图片url地址
      @BindImageUrl("url") ImageView iv;
    • @BindImageRes 绑定图片资源
      @BindImageRes("imageRes") ImageView iv;
    • @BindImageBitmap 绑定图片之bitmap
      @BindImageBitmap("imageBitmap") ImageView iv;
    • @BindImageDrawable 绑定图片之drawable
      @BindImageDrawable("imageDrawable") ImageView iv;
  • 4,绑定一组属性或任意属性

    • @BindsView 绑定一组View属性, 比如 'backgroundRes'. 'visibility'. 'enable'。
      @BindsView( {"backgroundRes", "visibility", "enable"})
    • @BindsTextView 绑定一组TextView属性。比如'textColorRes'. 'textSizeRes'. 'textRes'
      @BindsTextView( {"textColorRes", "textSizeRes", "textRes"})
      TextView mTv;
    • @BindsAny 用于绑定任意的一组属性。(value数组是属性名称,methods数组是Binder的方法名称,由于Binder可以自定义,所以可以绑定任意的控件到任意的方法 )
      @BindsAny(value = {"prop1", "prop2"}, methods = {"bindAddText1", "bindAddText2"})
      TextView mTv_supplier;
    • @BindAny 用于绑定任意的一个属性,(value是属性名称,method是Binder的方法名称,由于Binder可以自定义,所以可以绑定任意的控件到任意的方法 )
      @BindAny(value = "text", method = "bindAppendText")
      TextView mTv_supplier;

综合示例

/**
 * 第二种方式: 绑定一组属性到view
 * Created by heaven7 on 2017/11/13 0013.
 */
@BindMethodSupplierClass(BindMethodSupplier.DefaultBindMethodSupplier2.class)
public class TestBindArrayPropertyToOneView2 extends BaseActivity {

    //bind array properties to a TextView.
    //relative to @BindsTextView , @BindsAny has greater freedom(翻译: BindsAny注解有更大的自由度).
    // but it must use with @BindMethodSupplierClass.(翻译: 但是他必须搭配注解BindMethodSupplierClass.)
    @BindView(R.id.tv)
    @BindsAny(value = {"textSizeRes", "textRes"},    //any count you want
            methods = {"bindTextSizeRes", "bindTextRes"})
    @BindAny(value = "textColorRes", method = "bindTextColorRes") //only one property
    TextView mTv;

    private ResHelper mHelper = new ResHelper();
    private Binder<TextViewBind> mBinder;

    @Override
    protected int getLayoutId() {
        return R.layout.ac_bind_array_prop_to_view;
    }

    @Override
    protected void onInit(Context context, Bundle savedInstanceState) {
        mHelper.init(context);

        TextViewBind data = DataMediatorFactory.createData(TextViewBind.class);
        mBinder = DataMediatorFactory.createDataBinding(this)
                .bind(data, 0, PropertyInterceptor.NULL);
    }

    @OnClick(R.id.bt_text_color)
    public void onClickChangeTextColorRes(View v) {
        mBinder.getDataProxy().setTextColorRes(mHelper.toggleTextColorRes());
    }

    @OnClick(R.id.bt_text_size)
    public void onClickChangeTextSizeRes(View v) {
        mBinder.getDataProxy().setTextSizeRes(mHelper.toggleTextSizeRes());
    }

    @OnClick(R.id.bt_text)
    public void onClickChangeTextRes(View v) {
        mBinder.getDataProxy().setTextRes(mHelper.toggleTextRes());
    }

}

更多的demo请到github/Data-Mediator下载
或者看我之前的数据中介者:DataMediator, (ButterKnife最佳拍档)

FAQ

如果你有任何的问题可以在github上提issue或者加入qq群(389960698)咨询。
同时欢迎star/fock/contribute。

感谢你的阅读 !

技术源于分享!!!