当前位置:网站首页>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
边栏推荐
- 汇编语言与逆向工程 栈溢出漏洞逆向分析实验报告
- 洋桃电子STM32物联网入门30步笔记三、CubeMX图形化编程、设置开发板上的IO口
- LINQ学习系列-----1.4 匿名对象
- [explanation] get ora-12838: cannot read / modify an object after modifying it in parallel
- LeetCode-199-二叉树的右视图
- '恶霸' Oracle 又放大招,各大企业连夜删除 JDK。。。
- Stm32f103zet6 [development of standard library functions] - Introduction to library functions
- SYS_ CONNECT_ BY_ Path (column, 'char') combined with start with connect by prior
- Let the earth have less "carbon" and rest on the road
- QT reads all files under the path or files of the specified type (including recursion, judging whether it is empty and creating the path)
猜你喜欢
Yangtao electronic STM32 Internet of things introduction 30 steps notes 1. The difference between Hal library and standard library
Noyer électronique stm32 Introduction à l'Internet des objets 30 étapes notes I. différences entre la Bibliothèque Hal et la Bibliothèque standard
Yangtao electronic STM32 Internet of things entry 30 step notes II. Cube ide download, installation, sinicization and setting
信息收集相关知识点及题解
《深度学习》学习笔记(八)
作文以记之 ~ 二叉树的前序遍历
Goland 调试go使用-大白记录
Flash project cross domain interception and DBM database learning [Baotou cultural and creative website development]
洋桃電子STM32物聯網入門30步筆記一、HAL庫和標准庫的區別
K210学习笔记(二) K210与STM32进行串口通信
随机推荐
二维01背包
QT reading and writing XML files
Add random attributes to the Li class array objects and sort them
[C语言] 文件操作《一》
Protobuf简介
Stm32f103zet6 [development of standard library functions] - Introduction to library functions
Shell script advanced
Go语言自学系列 | golang方法
Transformer XL: attention language modelsbbeyond a fixed length context paper summary
【58】最后一个单词的长度【LeetCode】
Get the absolute path of the class according to the bytecode
synchronized 锁的基本用法
洋桃电子STM32物联网入门30步笔记一、HAL库和标准库的区别
DOM学习笔记---遍历页面所有元素节点
耳穴减肥自身感受细节描述0422
《深度学习》学习笔记(八)
使用flask和h5搭建网站/应用的简要步骤
STM32F103ZET6【标准库函数开发】----库函数介绍
记录:js删除数组中某一项或几项的几种方法
DOM 学习之—添加+-按钮