JetPack系列之ViewBinding

2023-05-16

  • viewBinding的作用
  • 启用视图绑定
  • Activity中使用视图绑定
  • Framgent中使用视图绑定
  • 与findViewById相比

viewBinding的作用

启用视图绑定之后,系统会为每个xml生成一个绑定类,我们在使用控件的时候就可以不用findviewbyid这种方式了,可以一定程度避免空指针。视图绑定主要用来替换findViewById。

启用视图绑定

视图绑定可以按模块启用,在需要绑定视图的模块的build.gradle中加入以下代码,启用viewbinding

viewBinding{
    enabled=true
}

同步之后会为每个布局文件生成一个binding文件,每个绑定均包含对根视图以及具有ID的所有视图的引用。

Activity中使用视图绑定

class MainActivity : AppCompatActivity() {

    private var mainBinding:ActivityMainBinding ? = null
    override fun onCreate(savedInstanceState: Bundle?) {
        //使用viewbinding替换原来的R.layout.activity_main
        mainBinding = ActivityMainBinding.inflate(LayoutInflater.from(this))
        // mainBinding!!.root获取根视图,对应Java的getRoot()方法
        setContentView(mainBinding!!.root)
        //直接使用mainBinding为控件赋值,无需使用findviewbyid
        mainBinding!!.tvTest.text = "一个textview"
    }
}

Fragment中使用视图绑定

class ViewBindingFragment : Fragment() {
    private var ktbinding : KtLayoutFragmentBinding? = null
    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        ktbinding = KtLayoutFragmentBinding.inflate(inflater)
        // ktbinding!!.root获取根视图,对应Java的getRoot()方法
        return ktbinding!!.root
    }

    override fun onDestroyView() {
        super.onDestroyView()
        ktbinding = null
    }
}

与findViewByid()相比

视图绑定与传统的findViewById()相比有以下有点

  1. 空安全,视图绑定会创建对视图的直接引用,不会出现以前由于ID不存在导致的空指针异常,如果视图仅出现在布局的某些配置中,则绑定类中包含其引用的字段会使用 @Nullable 标记。
  2. 类型安全 每个绑定类中的字段在视图中都存在一个与之匹配的类型,因此不会出现类型转换错误。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

JetPack系列之ViewBinding 的相关文章

  • B树、B+树及索引

    B树 xff1a 每个节点都存储key和data xff0c 所有节点组成这棵树 xff0c 并且叶子节点指针为null B 43 树 xff1a 只有叶子节点存储data xff0c 叶子节点包含了这棵树的所有键值 xff0c 叶子节点不
  • Arrays.sort和Collections.sort实现原理解析

    Collections sort方法底层就是调用的Arrays sort方法 写一个例子看源码 xff1a public static void main String args List lt String gt strings 61 A
  • Java代理模式之动态代理

    代理模式是设计模式中非常重要的一种类型 代理模式从类型上来说 xff0c 可以分为静态代理和动态代理两种类型 假设一个场景 xff0c 有一个蛋糕店 xff0c 卖的蛋糕都是用蛋糕机做的 xff0c 而且不同种类的蛋糕由不同的蛋糕机来做 x
  • 二叉树镜像

    求二叉树镜像 public class Solution public void Mirror TreeNode root if root 61 61 null return if root left 61 61 null amp amp
  • 设计模式之适配器模式

    适配器模式是作为两个不兼容的接口之间的桥梁 这种类型的设计模式属于结构型模式 xff0c 它结合了两个独立接口的功能 这种模式涉及到一个单一的类 xff0c 该类负责加入独立的或不兼容的接口功能 举个真实的例子 xff0c 读卡器是作为内存
  • 设计模式之单例模式

    1 懒汉式 xff0c 线程不安全 public class Demo1 private static Demo1 instance private Demo1 public static Demo1 getInstance if inst
  • Lambda表达式详解

    Java 8最值得学习的特性就是Lambda表达式 Lambda写的好可以极大减少代码冗余 xff0c 同时可读性也好过冗长的内部类 xff0c 匿名类 举例说明一下 xff1a xff08 1 xff09 创建线程传统写法 xff1a T
  • Android8.0以上实现APP(应用)开机自启动

    一 程序中实现APP开机自启动 可参考 xff1a https www cnblogs com jetereting p 4572302 html 二 设置APP开机自启动权限 小米手机设置开机启动应用权限 xff08 Android9 0
  • 跳台阶问题

    1 输出斐波那契数列的第n项 直接上代码 xff1a public class Fibonacci public static int fibonacci int n if n 61 61 0 return 0 if n 61 61 1 r
  • 字符串编辑距离

    字符串的编辑距离 xff0c 又称为Levenshtein距离 是利用字符操作 xff0c 把字符串A转换成字符串B所需要的最少操作数 其中 xff0c 字符操作包括 xff1a 删除一个字符插入一个字符修改一个字符 例如对于 34 hel
  • [leetcode]807.Max Increase to Keep City Skyline

    In a 2 dimensional array grid each value grid i j represents the height of a building located there We are allowed to in
  • [leetcode]1038. Binary Search Tree to Greater Sum Tree

    Given the root of a binary search tree with distinct values modify it so that every node has a new value equal to the su
  • [leetcode]344. Reverse String

    Write a function that reverses a string The input string is given as an array of characters char Do not allocate extra s
  • [leetcode]136. Single Number

    Given a non empty array of integers every element appears twice except for one Find that single one Note Your algorithm
  • [leetcode]412. Fizz Buzz

    Write a program that outputs the string representation of numbers from 1 to n But for multiples of three it should outpu
  • [leetcode]94. Binary Tree Inorder Traversal

    Given a binary tree return the inorder traversal of its nodes 39 values Example Input 1 null 2 3 1 2 3 Output 1 3 2 Solu
  • [leetcode]46. Permutations

    Given a collection of distinct integers return all possible permutations Example Input 1 2 3 Output 1 2 3 1 3 2 2 1 3 2
  • [ERROR][idea报错]Unmapped Spring configuration files found.

    idea启动Event Log提示 xff1a Spring Configuration Check Unmapped Spring configuration files found Please configure Spring fac
  • (备忘)android模拟器摄像头模拟

    Camera分Front Camera和Back Camera 通常我们模拟后摄像头就可以了 三个选项 none 表示没有摄像头 打开摄像应用会崩溃 emulated 系统模拟一个动态的画面 在黑白格背景上随机移动的矩形色块 Webcam
  • [leetcode]22. Generate Parentheses

    Given n pairs of parentheses write a function to generate all combinations of well formed parentheses For example given

随机推荐

  • [leetcode]238. Product of Array Except Self

    Given an array nums of n integers where n gt 1 return an array output such that output i is equal to the product of all
  • [ERROR]ubuntu18.04安装Postman报错

    解压完成以后 xff0c 进入解压目录 xff0c 执行 Postman xff0c 提示 xff1a Postman error while loading shared libraries libgconf 2 so 4 cannot
  • [leetcode]347. Top K Frequent Elements

    Given a non empty array of integers return the k most frequent elements Example 1 Input nums 61 1 1 1 2 2 3 k 61 2 Outpu
  • [leetcode]206. Reverse Linked List

    Reverse a singly linked list Example Input 1 gt 2 gt 3 gt 4 gt 5 gt NULL Output 5 gt 4 gt 3 gt 2 gt 1 gt NULL Follow up
  • [ERROR]ubuntu18.04自带中文输入法在输入中文选择内容的时候出现数字而不是中文

    ubuntu18 04自带中文输入法在输入中文选择内容的时候出现数字而不是中文 今天早上遇到了这个问题 xff0c sudo rm rf xff5e cache ibus libpinyin 重启电脑 解决
  • [leetcode]237. Delete Node in a Linked List

    Write a function to delete a node except the tail in a singly linked list given only access to that node Given linked li
  • [leetcode]78. Subsets

    Given a set of distinct integers nums return all possible subsets the power set Note The solution set must not contain d
  • 使用telnet发送email(内嵌图片,附件)

    使用telnet发送email 内嵌图片 xff0c 附件 因为最近想复习一下smtp协议所以无聊的本人想使用telnet发送email xff0c 虽然比较简单但还是记录下来希望可以给一些需要的朋友帮助吧 准备 xff1a 首先本人实在w
  • C/C++中 sizeof 详解

    摘要 xff1a Sizeof的作用非常简单 xff1a 求对象或者类型的大小 然而sizeof又非常复杂 xff0c 它涉及到很多特殊情况 xff0c 本篇把这些情况分门别类 xff0c 总结出了sizeof的10个特性 xff1a 0
  • Android中包含List成员变量的Parcel以及Parcel嵌套写法示例

    这个Scean类实现了Parcelable接口 xff0c 同时其内部的成员变量List lt SubScean gt subSceanList 中的SubScean类也实现了Parcelable接口 public class Scean
  • 第一节:详细透彻解读Git与SVN的区别(集中式VS分布式)

    Git是目前世界上最先进的分布式版本控制系统 xff0c 其实 Git 跟 SVN一样有自己的集中式版本库或服务器 xff0c 但是Git 更倾向于被使用于分布式模式 xff0c 也就是每个开发人员从中心版本库 服务器上chect out代
  • Android 9.0 SecureElementService 初始化流程分析

    1 相关名词解释 NFC Near Field Communication xff0c 近场通信 xff0c 一种基于13 56 MHz 的短距离通信技术 NFCC NFC Controller xff0c NFC 控制器 xff0c 负责
  • 【Linux】生产者消费者模型 - 详解

    目录 一 生产者消费者模型概念 1 为何要使用生产者消费者模型 2 生产者消费者之间的关系 3 生产者消费者模型的优点 二 基于阻塞队列的生产消费模型 1 在阻塞队列中的三种关系 2 BlockingQueue hpp 阻塞队列类 3 Lo
  • word 批量设置图片大小

    word批量修改图片大小 固定长宽篇 方法一 xff1a 这部分要说的是把word中的所有图片修改成固定的并且相同的长和宽 xff01 1 打开word xff0c 工具 xff0d 宏 xff0d 宏 xff08 或者直接按Alt 43
  • 深度学习之 人脸识别(1) 人脸预处理

    人脸识别分两个部分 xff1a 第一步 xff1a 人脸图片预处理 xff0c 即检测图片中人脸并裁剪成指定尺寸的人脸图 第二步 xff1a 人脸识别 xff0c 包括模型训练 目标人脸分类训练 预测目标人脸 1 人脸检测原理 人脸识别 x
  • 制作自己的个人网站方法

    随着个人创业的流行 xff0c 很多个人也需要一个比较详细的网站来展示自己 xff0c 开展个人业务 xff0c 或者积累粉丝等等 那么怎么制作自己的个人网站呢 xff1f 又该怎么制作得更个性好看 xff1f 下面就跟大家分享下制作方法
  • 傻瓜书,VMware里的Ubuntu

    转自 xff1a http bbs cnw com cn thread 136057 1 1 html 傻瓜书 xff0c VMware里的Ubuntu 0 预备知识 什么是Ubuntu 如果不了解这一点 xff0c 本文的内容似乎与您无关
  • 痞子衡单片机排行榜(2022Q4)

    痞子衡单片机排行榜 2022Q4 继2020年开办的 痞子衡嵌入式半月刊 之后 xff0c 从2023年1月份开始痞子衡将为大家带来新项目 痞子衡单片机排行榜 一年分四季 xff0c 每个季度发布一期 xff0c 以MCU主频和Corema
  • [pdf]使用spire读取PDF的文字和图片

    概述 最近在梳理某项目的数据标准 xff0c 从标准网下载了很多PDF格式的标准文件 xff0c 需要提取文字和图片 xff0c 所以写了个程序提取 xff1b 本文使用了免费版的Spire 约束 免费版的Spire一次只能提取PDF的10
  • JetPack系列之ViewBinding

    viewBinding的作用启用视图绑定Activity中使用视图绑定Framgent中使用视图绑定与findViewById相比 viewBinding的作用 启用视图绑定之后 xff0c 系统会为每个xml生成一个绑定类 xff0c 我