您的位置:首页 > 移动开发 > Android开发

Android的Databinding-需要使用控件id,listener以及布局有include的场景

2017-08-18 11:55 567 查看
主的布局xml文件:

<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:bind="http://schemas.android.com/apk/res-auto">

<data>

<import type="com.liangfeizc.databinding.model.User" />
<variable
name="user"
type="User" />
<variable
name="listener"
type="com.liangfeizc.databinding.listener.OkListener" />
<variable
name="okText"
type="String" />
</data>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<include
android:id="@+id/layout_input"
layout="@layout/layout_input" />

<include
layout="@layout/user"
bind:user="@{user}" />

<include
layout="@layout/layout_btn_ok"
bind:okText="@{okText}"
bind:listener="@{listener}"/>
</LinearLayout>
</layout>


本布局一共包含3个include布局,第一个是一个普通的布局,里面没有<data></data>, 考虑的就是如何通过id获取控件的引用。binding.layoutInput.etName binding是绑定的对象,layoutInput和etName就是Id,至于为啥没有了下划线_ , 因为这个id也是自动生成的。

第二个布局,需要传递User这个pojo类,所以要在<data>中引入,而同时也需要定义一个类别(看上面代码第二行)把user对象进行传递。

第三个布局较为复杂,需要传递listener和text,同样也需要一个类别进行传递。

本节参考代码。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐