DataBinding-xml中使用include
2017-08-31 14:47
411 查看
DataBinding-xml中使用include
需要include的xml文件
该文件同其他普通的xml文件相同,用layout标签包裹其他内容,需要的变量定义在data中,代码如下:<?xml version="1.0" encoding="utf-8"?> <layout> <data> <import type="android.view.View" /> <variable name="showEmptyView" type="boolean" /> </data> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <RelativeLayout android:layout_width="match_parent" android:layout_height="match_parent" android:visibility="@{showEmptyView ? View.VISIBLE : View.GONE}"> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:src="@mipmap/ic_placeholder" /> </RelativeLayout> <android.support.v7.widget.RecyclerView android:id="@+id/recycler_view" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_marginTop="12dp" /> </FrameLayout> </layout>
使用include
上面代码简单在data中定义了一个boolean型的变量showEmptyView。接下来我们在其他布局中引入该布局的时候就需要用到这个变量,并赋值给他,如下代码所示:<?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" xmlns:bind="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools"> <data> <variable name="viewModel" type="xxx.xxx.xxx.ViewModel" /> </data> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:background="@color/color_bg" android:orientation="vertical"> <include android:id="@+id/include" layout="@layout/include_recycler" android:layout_width="match_parent" android:layout_height="match_parent" bind:showEmptyView="@{viewModel.showEmptyView}" /> </LinearLayout> </layout>
重点有两处:
bind: bind:showEmptyView=“”,这里需要使用bind来绑定我们include布局中的showEmptyView变量,然后还需要定义bind的命名空间 xmlns:bind=”http://schemas.android.com/apk/res-auto” ;include标签需要给一个id:否则的话想用include的布局中的控件ID是无法使用的,给定include标签一个id如include,那么在binding中就可以使用binding.include.recyclerView就可以来操作RecyclerView了。
谢谢观看
该项目Sunshine已放在GitHub,这是其中一篇开发记录,更多开发记录文章请点击该项目查看。相关文章推荐
- Android Studio Databinding在xml中使用中文报错
- databinding使用include
- 数据绑定(六)使用XML数据作为Binding的Source
- 使用js加载图像和setDataXML()加载数据
- ajax上传文件,提交表单,使用FormData和XMLHttpRequest提交表单
- Windows Presentation Foundation(WPF)中的数据绑定(使用XmlDataProvider作控件绑定)
- How to create custom navigation menu in SharePoint with XML data source 使用XML数据源在SharePoint创建自定义导航菜单
- DataBinding基本功能使用笔记
- WPF:ComboBox使用XmlDataProvider做级联
- WPF:在XmlDataProvider上使用主-从绑定(Master-Detail Binding)
- DataBinding使用进阶
- Android dataBinding 使用记录
- How to create custom navigation menu in SharePoint with XML data source 使用XML数据源在SharePoint创建自定义导航菜单
- Android DataBinding使用详解(二)
- Android databinding xml中处理数据与控件绑定
- Databinding中的BindingConversion的使用
- DataBinding中如何使用Listener
- 使用ADF Data Binding 之一:使用JavaBean Data Control
- XML and Java technologies: Data binding, Part 2: Performance
- XmlDataDocument---- 加载和使用XmlDataDocument