RoboBinding探究一:TextView及EditText基础实现

RoboBinding是一个实现了数据绑定 Presentation Model(MVVM) 模式的Android开源框架。从简单的角度看,他移除了如addXXListener(),findViewById()这些不必要的代码,连如BufferKnife那样的InjectView都不需要,因为你的代码一般不需要依赖于这些界面组件信息。

项目的中文文档地址是:http://robobinding.github.io/RoboBinding/index.zh.html
项目github 地址https://github.com/RoboBinding


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:bind="http://robobinding.org/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:ignore="MissingPrefix" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        bind:text="{hello}" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Name:" />

        <EditText
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            bind:text="${name}" />
    </LinearLayout>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Say Hello"
        bind:onClick="sayHello" />

</LinearLayout>



public class TestActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		PresentationModel presentationModel = new PresentationModel();
		ViewBinder viewBinder = createViewBinder();
		View rootView = viewBinder.inflateAndBind(R.layout.activity_test,
				presentationModel);
		setContentView(rootView);
	}

	private ViewBinder createViewBinder() {
		BinderFactory reusableBinderFactory = new BinderFactoryBuilder()
				.build();
		return reusableBinderFactory.createViewBinder(this);
	}

}



@org.robobinding.annotation.PresentationModel
public class PresentationModel implements HasPresentationModelChangeSupport {
	private PresentationModelChangeSupport changeSupport;
	private String name;

	public PresentationModel() {
		changeSupport = new PresentationModelChangeSupport(this);
	}

	public String getHello() {
		return name + ": hello Android MVVM(Presentation Model)!";
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public void sayHello() {
		changeSupport.firePropertyChange("hello");
	}

	@Override
	public PresentationModelChangeSupport getPresentationModelChangeSupport() {
		return changeSupport;
	}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值