当前位置:网站首页>dataBinding中使用include
dataBinding中使用include
2022-04-23 08:35:00 【锐湃】
DataBinding下 使用include包括对象的传递,事件的监听。
include的布局如下:
custom_home.xml
<layout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:binding="http://schemas.android.com/apk/res-auto">
<data>
<variable
name="user"
type="com.yc.bean.User" />
<variable
name="onImgClick "
type="android.view.View.OnClickListener" />
</data>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="horizontal">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick ="@{onImgClick }"
android:src="@mipmap/ic_home_position"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@{user.name}"
android:textColor="@color/colorBarText"
android:textSize="12sp" />
</LinearLayout>
</Layout>
这里要使用对象和监听的话,就要先声明相应的variable
main.xml 中代码:
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:binding="http://schemas.android.com/apk/res-auto">
<data>
<import type="com.yc.ui.home.HomeFragmentViewModel"/>
<variable
name="viewModel"
type="HomeFragmentViewModel" />
<variable
name="user"
type="com.yc.bean.User"/>
</data>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
.......
<include android:id="@+id/include"
layout="@layout/custom_home"
binding:onImgClick ="@{viewModel.onImgClick}"
binding:user="@{user}"/>
</FrameLayout>
</layout>
binding:user=@{user}
这里 binding:include中变量名=@{ 传进去的监听或者实体对象}
名字必须对应不然找不到 类名也必须相同。
viewModel中方法:
public View.OnClickListener onImgClick = new View.OnClickListener() {
@Override
public void onClick(View v) {
ToastUtils.showShort("点击了头像");
}
};
————————————————
版权声明:本文为CSDN博主「云梦星河」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/yunmengxinghe/article/details/107792376
版权声明
本文为[锐湃]所创,转载请带上原文链接,感谢
https://blog.csdn.net/chuyouyinghe/article/details/124354463
边栏推荐
猜你喜欢
Flash project cross domain interception and DBM database learning [Baotou cultural and creative website development]
【深度好文】Flink SQL流批⼀体化技术详解(一)
程序,进程,线程;内存结构图;线程的创建和启动;Thread的常用方法
使用flask和h5搭建网站/应用的简要步骤
pgsql想实现mysql一样样的列子查询操作
Use of Arthas in JVM tools
作文以记之 ~ 二叉树的前序遍历
Notes on 30 steps of introduction to Internet of things of yangtao electronics STM32 III. Explanation of new cubeide project and setting
虚拟线上展会-线上vr展馆实现24h沉浸式看展
HAL库的RCC简介
随机推荐
ajax防止缓存方法
让地球少些“碳”息 度能在路上
K210学习笔记(二) K210与STM32进行串口通信
Redis Desktop Manager for Mac(Redis可视化工具)
根据字节码获取类的绝对路径
Type anonyme (Principes fondamentaux du Guide c)
php基于哈希算法出现的强弱比较漏洞
xctf刷题小记
How browser works
uni-app和微信小程序中的getCurrentPages()
关于cin,scanf和getline,getchar,cin.getline的混合使用
一个必看的微信小程序开发指南1-基础知识了解
《深度学习》学习笔记(八)
虚拟线上展会-线上vr展馆实现24h沉浸式看展
00后最关注的职业:公务员排第二,第一是?
Go语言自学系列 | golang结构体指针
【IndexOf】【lastIndexOf】【split】【substring】用法详解
On time atom joins hands with oneos live broadcast, and the oneos system tutorial is fully launched
Description of the abnormity that the key frame is getting closer and closer in the operation of orb slam
QT reads all files under the path or files of the specified type (including recursion, judging whether it is empty and creating the path)