Android dataBinding中使用include
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><variablename="user"type="com.yc.bean.User" /><variablename="onImgClick "type="android.view.View.OnClickListener" /></data><LinearLayoutandroid:layout_width="match_parent"android:layout_height="match_parent"android:gravity="center"android:orientation="horizontal"><ImageViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:onClick ="@{onImgClick }"android:src="@mipmap/ic_home_position"/><TextViewandroid: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"/><variablename="viewModel"type="HomeFragmentViewModel" /><variablename="user"type="com.yc.bean.User"/></data><FrameLayoutandroid: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() {@Overridepublic void onClick(View v) {ToastUtils.showShort("点击了头像");}};
双向绑定有待更新 敬请期待吧!!!!
Android dataBinding中使用include相关推荐
- android布局中使用include及需注意点
在android布局中,使用include,将另一个xml文件引入,可作为布局的一部分,但在使用include时,需注意以下问题:一.使用include引入如现有标题栏布局block_header.x ...
- android——databinding中字符串的拼接处理、TextView显示的值随activity的属性值改变同时改变--LiveData、双向绑定过滤器、监听某个值的改变
简介 使用的技术是观察者与被观察者的模式,在google推荐的案例中也有使用到,现在我把它封装成一个扩展函数,使得使用更加简单明了 注意 1.在build.gradle添加databinding,在a ...
- 在android布局中使用include和merge标签
在我们开发Android布局时,经常会有很多的布局是相同的,这个时候我们可以通过<include/>和<merge/>标签实现将复杂的布局包含在需要的布局中,减少重复代码的编写 ...
- Android——layout中使用include复用模块
早在ITOO中我们觉得页面代码太多而各种抽页面,像公共的按钮.搜索框.表格等,在主页面中只需要写几句代码就可以了,其实语言都是一样的,在安卓的世界中也是可以抽出部分视图的,只不过在安卓的开发环境中,是 ...
- android include 控件详解,Android开发中include控件用法分析
本文实例讲述了Android开发中include控件用法.分享给大家供大家参考,具体如下: 我们知道,基于Android系统的应用程序的开发,界面设计是非常重要的,它关系着用户体验的好坏.一个好的界面 ...
- Android Makefile中inherit-product函数和include的区别
在 Android Makefile中inherit-product函数和include都是执行某个文件. include 使用: include device/mediatek/mt2712/dev ...
- Android Makefile中inherit-product函数简介,以及与include的区别
在 Android Makefile 中时不时会看见 inherit-product 函数的使用,类似下方这样: $(call inherit-product, vendor/do ...
- Android DataBinding 从入门到进阶
DataBinding 是谷歌官方发布的一个框架,顾名思义即为数据绑定,是 MVVM 模式在 Android 上的一种实现,用于降低布局和逻辑的耦合性,使代码逻辑更加清晰.MVVM 相对于 MVP,其 ...
- Android开发中应避免的重大错误
by Varun Barad 由Varun Barad Android开发中应避免的重大错误 (Critical mistakes to avoid in Android development) A ...
最新文章
- 【Python】Pandas基础:结构化数据处理
- 白话Elasticsearch01- 结构化搜索之使用term query来搜索数据
- python 作用域嵌套_python嵌套作用域问题
- Servlet的重定向和转发特点
- [Objective-C语言教程]动态绑定(32)
- (五)springcloud微服务分布式云架构 - 云架构代码结构构建
- 云原生与AI时代的存储该是什么样?新华三发布全NVMe智能闪存与智慧中枢数据平台
- 安装pgsql解压版操作步骤
- Hadoop系列-分布式集群
- NWT失败反省:做视频的多了,汝眼里怎么只有腾讯
- 苹果手机怎么关闭爱奇艺自动续费_爱奇艺回应涨价为用户创造优质服务,小编建议用户先关闭自动续费...
- jQuery淡入浅出
- 特种浓缩分离:全自动型拉板式压滤机
- 3dmax:3dmax三维VR渲染设置(VR发光贴图、VR灯光缓存、V-Ray焦散,渲染图中出现黑斑点的原因、插值类型)之详细攻略
- 【君思智慧园区】智慧园区的发展类型
- 报错UnicodeEncodeError: ‘ascii‘ codec can‘t encode character ‘\u7b2c‘ in position 64: ordinal not in r
- pygame播放音乐带歌词
- linux 和windows 内存管理区别
- Oracle Eloqua:精准化营销为企业吸引到理想客户
- java采用Jacob将Excel转PDF