当前位置:网站首页>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
边栏推荐
- Nacos作为服务配置中心实战
- Function recursion and solving interesting problems
- Summary of actual business optimization scheme - main directory - continuous update
- Configure iptables
- Test questions of daily safety network (February 2024)
- Practice of Druid SQL and security in meituan review
- 玻璃体中的硫酸软骨素
- 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
- Sentinel rule persistence into Nacos
- 解决:cnpm : 無法加載文件 ...\cnpm.ps1,因為在此系統上禁止運行脚本
猜你喜欢
Use stm32cube MX / stm32cube ide to generate FatFs code and operate SPI flash
纠结
Use bitnami / PostgreSQL repmgr image to quickly set up PostgreSQL ha
Résolution: cnpm: impossible de charger le fichier... Cnpm. PS1 parce que l'exécution de scripts est désactivée sur ce système
[mathematical modeling] - analytic hierarchy process (AHP)
解决:cnpm : 无法加载文件 ...\cnpm.ps1,因为在此系统上禁止运行脚本
ctfshow-web362(SSTI)
ESP32 LVGL8. 1 - label (style 14)
ESP32 LVGL8. 1 - input devices (input devices 18)
WiFi ap6212 driver transplantation and debugging analysis technical notes
随机推荐
Loop path
Daily CISSP certification common mistakes (April 18, 2022)
The corresponding permissions required to automatically open the app in the setting interface through accessibility service
深入理解 Golang 中的 new 和 make 是什么, 差异在哪?
About the operation of unit file reading (I)
Go language GUI framework Fyne Chinese garbled or not displayed
Fundamentals of machine learning theory -- some terms about machine learning
机器学习理论基础篇--关于机器学习的一些术语
配置iptables
ESP32 LVGL8. 1 - slider slider (slider 22)
Daily CISSP certification common mistakes (April 13, 2022)
PyGame tank battle
ESP32 LVGL8. 1 - calendar (calendar 25)
CANopen STM32 transplantation
How to virtualize the video frame and background is realized in a few simple steps
Creation and use of QT dynamic link library
迁移学习进阶
How can programmers quickly develop high-quality code?
Setting up keil environment of GD single chip microcomputer
kettle庖丁解牛第17篇之文本文件输出