sword
Introduction: 一个基于注解的 Android 数据绑定框架,支持双向和单向绑定,并且可以无感知做到数据更新,只需要简单开启实验功能。
Tags:
databinding-android-an android data binding framework 中文说明文档
how to use
Step.1
make an model which implement interface
BindImpl
class Data implements BInterface {
public String end_title = "title";
public String end_desc = "desc";
public int endTitleColor=getResources().getColor(R.color.colorAccent);
public View.OnClickListener end_titleL = new View.OnClickListener() {
@Override
public void onClick(View v) {
//todo sth
}
};
}
the layout is like that.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<RelativeLayout
android:layout_gravity="center_horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:id="@+id/end_title"
android:text="end-title"
android:textSize="23sp"
android:textStyle="bold"
android:layout_toRightOf="@id/end_img"
android:layout_marginLeft="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/end_desc"
android:layout_below="@id/end_title"
android:textSize="15sp"
android:textColor="#000000"
android:layout_toRightOf="@id/end_img"
android:layout_marginLeft="11dp"
android:text="desc"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
</LinearLayout>
Step.2
if you want to bind the field of data to the property of view, you should make the data model like that:
class Data implements BInterface {
@Bind(viewId = R.id.end_title,setMethod = B.setText)// bind text
public String end_title = "title";
@Bind(viewId = R.id.end_desc,setMethod = B.setText)// bind text
public String end_desc = "desc";
@Bind(viewId = R.id.end_title,setMethod = B.setTextColor) // bind textColor
public int endTitleColor=getResources().getColor(R.color.colorAccent);
@Bind(viewId = R.id.end_title, setMethod = B.setOnClickListener) // bind listener
public View.OnClickListener end_titleL = new View.OnClickListener() {
@Override
public void onClick(View v) {
//todo sth
}
};
}
Step.3
Let's get it all in one.
Data data = new Data();
SwordBind swordBind = new SwordBind();
swordBind.bindOneWay(activity, data);//swordBind.bindOneWay(view, data);
change data
data.end_title="change-data";
swordBind.dataChange();///notify to update view
Step.4
proguard
-keep interface com.taobao.pandora.sword.BInterface { *; }
-keep class * implements com.taobao.pandora.sword.BInterface {*;}
-keep class **.R$id {*;}
how to import
dependencies {
compile 'com.shanks.hongfa:sword:2.1.0'
}
