android -------- Data Binding的使用 ( 五) include
Data Binding的中 include 标签的使用
inclune使用和原来一样,但要如何使数据也在 include中使用呢?
先看看我的布局文件
include的布局文件,也要使用 <layout>标签
<?xml version="1.0" encoding="utf-8"?> <layout xmlns:android="http://schemas.android.com/apk/res/android"> <data> <variable name="userInfo" type="com.zhangqie.databinding.demo10.UserInfo" /> </data> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <TextView android:id="@+id/tv_tow_mls" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@{userInfo.dataName}" /> <Button android:id="@+id/btn_demo10_include" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="include 下的点击" /> </LinearLayout> </layout>
来看看主布局
<?xml version="1.0" encoding="utf-8"?> <layout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto"> <data> <variable name="userInfo" type="com.zhangqie.databinding.demo10.UserInfo" /> </data> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <TextView android:id="@+id/tv_title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@{userInfo.dataName}" /> <Button android:id="@+id/btn_demo10" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="测试" /> <include android:id="@+id/demo10_ainclude" layout="@layout/include_test" app:userInfo="@{userInfo}" /> </LinearLayout> </layout>
include 中需要把 userinfo对象传给他;
也要给include 添加一个id值
来看看java代码:
public class InCludeActivity extends AppCompatActivity { Demo10Binding demo10Binding; @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); demo10Binding = DataBindingUtil.setContentView(this,R.layout.demo10); initView(); } private void initView(){ UserInfo userInfo = new UserInfo("zhangqie"); demo10Binding.setUserInfo(userInfo); demo10Binding.demo10Ainclude.tvTowMls.setText("访问include 中的id"); demo10Binding.demo10Ainclude.btnDemo10Include.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Toast.makeText(InCludeActivity.this,"123",Toast.LENGTH_LONG).show(); } }); } }
通过已上介绍应该可以简单了解databinding中 include的使用了