当前位置:网站首页>Simple use of viewbinding
Simple use of viewbinding
2022-04-23 18:43:00 【KIDD-4869】
Preface
Kotlin Plug in 1.4.20 It is proposed to abandon , Google left us a year to modify and replace , What shall I do? ? I'm used to using it directly before xml Medium id As view Used , Do you want to go back to findViewById Era ? How is that possible? , Don't hurry to hug ViewBinding The arms of .
Google also knows Kotlin Disadvantages of plug-ins , such as :
- adopt Kotlin Synthesis method (Synthetic View ) replace findViewById, This is through the global space cache ID, And Layout
irrelevant , Not for ID Perform invalid check - In different Layout In file , Used the same ID, Or delete ID , It does not prompt null exceptions , It leads to an increase in App Number of crashes
- Support only Kotlin
- The default is through HashMap cache ID Waste space , Although it can be done at the module level build.gradle Add in file
defaultCacheImplementation = “SPARSE_ARRAY” To modify the default implementation as SparseArray
…
ViewBinding Solve the above problems , But it also brings other disadvantages :
- ViewBinding Compared with kotlinx.android.synthetic It is used in a complicated way
- stay Activity 、 Fragment 、Dialog 、 Adapter in ViewBinding and DataBinding
The initialization method is somewhat different It needs to be dealt with separately include belt merge The layout of the label , And without merge Layout of labels, etc - DataBinding combination LiveData When used together, it needs to be treated separately
…
But for view binding , There are also convenient ways to write , Come and see it .
Use
First, in the Gradle The plug-in declares the use of
// Android Studio 3.6
android {
viewBinding {
enabled = true
}
dataBinding{
enabled = true
}
}
// Android Studio 4.0
android {
buildFeatures {
dataBinding = true
viewBinding = true
}
}
secondly , For the convenience of one line of code, directly use ViewBinding, We also need to introduce
implementation 'com.hi-dhl:binding:1.0.5'
And then directly Activity、Fragment、Adapter Where it needs to be used
class HomeFragment : Fragment() {
// One line of code ,FragmentHomeBinding Is the corresponding Fragment or Activity Auto corresponding Binding, It has something to do with your own name
private val binding: FragmentHomeBinding by viewbind()
private var count = 0
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
return inflater.inflate(R.layout.fragment_home, container, false)
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
//click by xml Medium id
binding.click.setOnClickListener {
count++
//dashboardView by xml Medium id
binding.dashboardView.setPointer(count % 21)
}
}
}
Layout file xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".fragment.HomeFragment">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<com.kxqin.myapplication.view.DashboardView
android:id="@+id/dashboardView"
android:layout_width="match_parent"
android:layout_height="300dp" />
<TextView
android:id="@+id/click"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Click"
android:textColor="@color/black"
android:layout_gravity="center" />
</LinearLayout>
</FrameLayout>
After that, just like kotlin Plug-ins work like that binding Call corresponding id That's it , Is it a line of code to use directly viewbinding 了 ? Is it convenient ? Hurry up kotlin Replace the plug-in before it is deactivated .
版权声明
本文为[KIDD-4869]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204210603464556.html
边栏推荐
- Deeply understand what new and make in golang are and what are the differences?
- Feature selection feature_ selection--SelectKBest
- 【科普】CRC校验(一)什么是CRC校验?
- kettle庖丁解牛第17篇之文本文件输出
- Golang 语言实现TCP UDP通信
- SQL中函数 decode()与 replace()的用法
- Nacos as service registry
- Introduction to quantexa CDI syneo platform
- Daily network security certification test questions (April 13, 2022)
- 配置iptables
猜你喜欢
ctfshow-web362(SSTI)
ESP32 LVGL8. 1 - arc (arc 19)
Use stm32cube MX / stm32cube ide to generate FatFs code and operate SPI flash
Use bitnami / PostgreSQL repmgr image to quickly set up PostgreSQL ha
PyGame tank battle
解决:cnpm : 无法加载文件 ...\cnpm.ps1,因为在此系统上禁止运行脚本
使用 bitnami/postgresql-repmgr 镜像快速设置 PostgreSQL HA
七、DOM(下) - 章节课后练习题及答案
CANopen STM32 transplantation
With the use of qchart, the final UI interface can be realized. The control of qweight can be added and promoted to a user-defined class. Only the class needs to be promoted to realize the coordinate
随机推荐
迁移学习进阶
机器学习实战 -朴素贝叶斯
机器学习理论基础篇--关于机器学习的一些术语
PyGame tank battle
Chondroitin sulfate in vitreous
纠结
解决:cnpm : 無法加載文件 ...\cnpm.ps1,因為在此系統上禁止運行脚本
Ctfshow - web362 (ssti)
STM32: LCD display
Use stm32cube MX / stm32cube ide to generate FatFs code and operate SPI flash
昇腾 AI 开发者创享日全国巡回首站在西安成功举行
Screenshot using projectmediamanager
Kettle paoding jieniu Chapter 17 text file output
机器学习理论之(7):核函数 Kernels —— 一种帮助 SVM 实现非线性化决策边界的方式
ESP32 LVGL8. 1 - roller rolling (roller 24)
Daily CISSP certification common mistakes (April 15, 2022)
使用晨曦记账本,分析某个时间段每个账户收支结余
CISSP certified daily knowledge points (April 19, 2022)
iptables初探
Setting up keil environment of GD single chip microcomputer