码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • Android开发JetPack-Databinding组件


    DataBinding介绍

    • Android开发JetPack-Databinding组件
        • 1. 什么是databinding
          • (1)简介
          • (2)意义
        • 2. databinding基本使用
          • (1)启用databinding
          • (2)定义一个布局
          • (3)创建一个User类
          • (4)改变MainActivity.kt的代码
        • 3. 通过DatabindingUtil获取到Binding
        • 4.给BaseViewFragment加载View
        • 5.编写BaseVmFragment
        • 6.布局和绑定表达式
          • (1)其他控件引用,TextView引用EditText。
          • (2)表达式中也可以引用资源内容。
          • (3)某些资源需要用到特定的类型。
        • 7.事件处理
          • 事件处理,一种为方法引用,一种为监听绑定。
          • 实例
        • 8.双向绑定
          • 双向绑定要达到的效果便是除了数据影响界面,界面变化也要使得数据发生变化。比如EditText输入内容时,绑定的数据bean要跟着变化。
        • 9.数据更新-->UI更新
          • Databinding通过使用实现Observable的数据,当数据更新的时候,自动更新UI。
          • 监听对象变化更新
        • 10.BindingAdapter
          • DataBinding支持在普通方法上添加@注解来添加自定义控件属性。
        • 11.小结

    Android开发JetPack-Databinding组件

    1. 什么是databinding

    (1)简介

    Databinding是谷歌的一个官方支持库,它允许您使用声明性格式而不是通过编程方式将布局中的UI组件绑定到应用程序中的数据源。通常在活动中使用调用UI框架方法的代码来定义布局。例如,调用findViewById()以查找TextView窗口小部件并将其绑定到变量。因为它通过在布局文件中绑定组件,您可以删除活动中的许多UI框架调用,从而使它们更易于维护。这也可以提高应用程序的性能,并有助于防止内存泄漏和空指针异常。

    (2)意义

    1、布局文件通常只负责UI控件的布局工作,页面中通过代码对控件需要进行各种操作,承担了绝大部分的工作量

    2、DataBinding让布局文件承担了部分原本属于页面的工作,也使得布局文件和页面的耦合度进一步降低

    3、使得UI控件能够直接合数据模型中的字段绑定,甚至能响应用户的交互。方便实现MVVM

    2. databinding基本使用

    (1)启用databinding

    在模块下的build.gradle文件中,启动dataBinding。

    android {
       
      ...
      dataBinding {
       
          enabled = true
      }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    (2)定义一个布局

    显示用户的姓名,年龄和性别,将普通布局文件转换为DataBinding布局文件。

    转换后代码如下:

    
    <layout xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:app="http://schemas.android.com/apk/res-auto"
      xmlns:tools="http://schemas.android.com/tools">
    
      <data>
    
          <variable
              name="user"
              type="com.example.myapplication.domain.User" />
      data>
    
      <androidx.constraintlayout.widget.ConstraintLayout
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          tools:context=".MainActivity">
    
          <androidx.constraintlayout.widget.Guideline
              android:id="@+id/guideline"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:orientation="horizontal"
              app:layout_constraintGuide_percent="0.10259918" />
    
          <androidx.constraintlayout.widget.Guideline
              android:id="@+id/guideline2"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:orientation="horizontal"
              app:layout_constraintGuide_percent="0.2" />
    
          <androidx.constraintlayout.widget.Guideline
              android:id="@+id/guideline3"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:orientation="horizontal"
              app:layout_constraintGuide_percent="0.3" />
    
          <androidx.constraintlayout.widget.Guideline
              android:id="@+id/guideline4"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:orientation="vertical"
              app:layout_constraintGuide_percent="0.3" />
    
          <TextView
              android:id="@+id/textView"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:text="@string/text_name"
              app:layout_constraintBottom_toTopOf="@+id/guideline"
              app:layout_constraintEnd_toStartOf="@+id/guideline4"
              app:layout_constraintStart_toStartOf="parent"
              app:layout_constraintTop_toTopOf="parent" />
    
          <TextView
              android:id="@+id/textView2"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:text="@string/text_age"
              app:layout_constraintBottom_toTopOf="@+id/guideline2"
              app:layout_constraintEnd_toStartOf="@+id/guideline4"
              app:layout_constraintStart_toStartOf="parent"
              app:layout_constraintTop_toTopOf="@+id/guideline" />
    
          <TextView
              android:id="@+id/textView3"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:text="@string/text_gender"
              app:layout_constraintBottom_toTopOf="@+id/guideline3"
              app:layout_constraintEnd_toStartOf="@+id/guideline4"
              app:layout_constraintStart_toStartOf="parent"
              app:layout_constraintTop_toTopOf="@+id/guideline2" />
    
          <TextView
              android:id="@+id/textView4"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
  • 相关阅读:
    轻量封装WebGPU渲染系统示例<33>- 单精度浮点纹理(源码)
    Nlp项目实战自定义模板框架
    Git操作复习笔记
    CentOS7无法连接网络 右上角网络图标消失
    Learning Invariant Representation for Unsupervised Image Restoration
    resources(static与templates)
    Qt C++春晚刘谦魔术约瑟夫环问题的模拟程序
    JVM虚拟机:JVM中垃圾回收器的总结
    4.4 switch语句
    几个Web自动化测试框架的比较:Cypress、Selenium和Playwright
  • 原文地址:https://blog.csdn.net/fjnu_se/article/details/128151309
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号