學習路徑
- 1, 入門篇
- 2, 專題篇
- 3, 原始碼閱讀
簡單demo
- 1, 假設我要定義關於學生的資料模型, 需要實現Serializable, Parcelable. 假如學生有。年齡,名稱, id屬性。 那麼簡單的資料定義為:
@Fields({
@Field(propName = "age" , type = int.class),
@Field(propName = "name" , type = String.class),
@Field(propName = "id" , type = long.class),
})
public interface Student extends Serializable, Parcelable{
}
複製程式碼
- 2, 使用idea外掛生成程式碼, 快捷鍵比如 alt + insert. (安裝見這裡Data-mediator入門系列1) 模型生成後類似這樣
@Fields({
@Field(propName = "age" , type = int.class),
@Field(propName = "name" , type = String.class),
@Field(propName = "id" , type = long.class),
})
public interface Student extends Serializable, Parcelable, DataPools.Poolable {
Property PROP_age = SharedProperties.get(int.class.getName(), "age", 0);
Property PROP_name = SharedProperties.get(String.class.getName(), "name", 0);
Property PROP_id = SharedProperties.get(long.class.getName(), "id", 0);
Student setAge(int age1);
int getAge();
Student setName(String name1);
String getName();
Student setId(long id1);
long getId();
}
複製程式碼
- 3, 呼叫示例。
//屬性改變demo. 下面用butterknife快速寫demo
public class TestPropertyChangeActivity extends BaseActivity {
@BindView(R.id.tv_desc)
TextView mTv_desc;
@BindView(R.id.bt_set_text_on_TextView)
Button mBt_changeProperty;
@BindView(R.id.bt_set_text_on_mediator)
Button mBt_temp;
DataMediator<Student> mMediator;
@Override
protected int getLayoutId() {
return R.layout.ac_test_double_bind;
}
@Override
protected void onInit(Context context, Bundle savedInstanceState) {
mBt_changeProperty.setText("click this to change property");
mBt_temp.setVisibility(View.GONE);
//為資料模型建立 中介者。
mMediator = DataMediatorFactory.createDataMediator(Student.class);
//新增屬性callback
mMediator.addDataMediatorCallback(new DataMediatorCallback<Student>() {
@Override
public void onPropertyValueChanged(Student data, Property prop, Object oldValue, Object newValue) {
Logger.w("TestPropertyChangeActivity","onPropertyValueChanged","prop = "
+ prop.getName() + " ,oldValue = " + oldValue + " ,newValue = " + newValue);
mTv_desc.setText(String.valueOf(newValue));
}
});
mMediator.getDataProxy().setName("heaven7");
}
@OnClick(R.id.bt_set_text_on_TextView)
public void onClickSetTextOnTextView(View v){
mMediator.getDataProxy().setName("time: " + System.currentTimeMillis());
}
}
複製程式碼
該demo 做了3件事情。建立資料中介者,新增屬性回撥。然後在點選事件中改變了資料的屬性。 結果使得日誌被列印出來。而且textView也被更新。
對模型實現的程式碼感興趣?
模型實現程式碼大致是這樣的。
public class Student_$Impl implements Student, Serializable, Parcelable, DataPools.Poolable {
private static final long serialVersionUID = 1L;
public static final Parcelable.Creator<Student_$Impl> CREATOR = new Parcelable.Creator<Student_$Impl>() {
@Override
public Student_$Impl createFromParcel(Parcel in) {
return new Student_$Impl(in);
}
@Override
public Student_$Impl[] newArray(int size) {
return new Student_$Impl[size];
}
};
private int age;
private String name;
private long id;
protected Student_$Impl(Parcel in) {
this.age = in.readInt();
this.name = in.readString();
this.id = in.readLong();
}
public Student_$Impl() {
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeInt(this.age);
dest.writeString(this.name);
dest.writeLong(this.id);
}
@Override
public void recycle() {
DataPools.recycle(this);
}
@Override
public void clearProperties() {
this.id = 0;
this.age = 0;
this.name = null;
}
@Override
public String toString() {
Objects.ToStringHelper helper = Objects.toStringHelper(this)
.add("age", String.valueOf(this.age))
.add("name", String.valueOf(this.name))
.add("id", String.valueOf(this.id));
return helper.toString();
}
public int getAge() {
return age;
}
public Student setAge(int age1) {
this.age = age1;
return this;
}
public String getName() {
return name;
}
public Student setName(String name1) {
this.name = name1;
return this;
}
public long getId() {
return id;
}
public Student setId(long id1) {
this.id = id1;
return this;
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (!(o instanceof Student_$Impl)) {
return false;
}
Student_$Impl that = (Student_$Impl) o;
if (getAge() != that.getAge()) {
return false;
}
if (getName() != null ? !getName().equals(that.getName()) : that.getName() != null) {
return false;
}
if (getId() != that.getId()) {
return false;
}
return true;
}
}
複製程式碼
下一章
想要體驗最新的特性 ?
請到github/data-mediator體驗。 如果覺得不錯,請star支援下專案哈。