DataBinding-xml中使用include

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,这是其中一篇开发记录,更多开发记录文章请点击该项目查看。

  • 8
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
你可以在你的pom文件添加以下依赖配置: ``` <dependencies> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-core</artifactId> <version>3.4.4</version> </dependency> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-bindings-soap</artifactId> <version>3.4.4</version> </dependency> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-bindings-xml</artifactId> <version>3.4.4</version> </dependency> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-databinding-jaxb</artifactId> <version>3.4.4</version> </dependency> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-frontend-simple</artifactId> <version>3.4.4</version> </dependency> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-ws-addr</artifactId> <version>3.4.4</version> </dependency> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-ws-policy</artifactId> <version>3.4.4</version> </dependency> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-wsdl</artifactId> <version>3.4.4</version> </dependency> <dependency> <groupId>wsdl4j</groupId> <artifactId>wsdl4j</artifactId> <version>1.6.3</version> </dependency> <dependency> <groupId>xml-resolver</groupId> <artifactId>xml-resolver</artifactId> <version>1.2</version> </dependency> <dependency> <groupId>org.apache.ws.xmlschema</groupId> <artifactId>xmlschema-core</artifactId> <version>2.2.5</version> </dependency> </dependencies> ``` 请注意,这些依赖的版本可能会随着时间的推移而更新。建议在使用这些依赖时,查阅官方文档以获取最新版本信息。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值