Binding
Simple API implement DataBinding and ViewBinding. 简单的 API 实现 DataBinding 和 ViewBinding,欢迎 star
Install / Use
/learn @hi-dhl/BindingREADME
<p align="center"> Binding </p>
<p align="center"> Simple API implement DataBinding and ViewBinding. Welcome star<br/> 简单的 API 实现 DataBinding 和 ViewBinding,欢迎 star </p> <p align="center"> <a href="https://github.com/hi-dhl/Binding">English</a> · <a href="https://github.com/hi-dhl/Binding/blob/main/doc/README_CN.md">中文</a> </p> <p align="center"> <a href="https://github.com/hi-dhl"><img src="https://img.shields.io/badge/GitHub-HiDhl-4BC51D.svg?style=flat"></a> <img src="https://img.shields.io/badge/language-kotlin-orange.svg"/> <a href="https://search.maven.org/search?q=g:%22com.hi-dhl%22%20AND%20a:%22binding%22"><img src="https://img.shields.io/maven-central/v/com.hi-dhl/binding.svg?label=Maven%20Central"/></a> <img src="https://img.shields.io/badge/platform-android-lightgrey.svg"/> </p> <p align="center"> If the image cannot be viewed, please click here to view it <a href="http://img.hi-dhl.com/vbdb.png"> img1 </a> | <a href="http://img.hi-dhl.com/ViewBidnding.png"> img2 </a></p> <p align="center"> <image src="http://img.hi-dhl.com/vbdb.png" width = 600px/> </p> <p align="center"> <image src="http://img.hi-dhl.com/ViewBidnding.png" width = 600px/> </p>Thanks
- Thanks to Google Translation
- the idea from Simple one-liner ViewBinding in Fragments and Activities with Kotlin
- learn skills from open source libraries such as Anko 、 ViewBindingDelegate 、architecture-components-samples and jetpack
About Binding
Binding has been migrated to Maven Central because jCenter will be deprecated
Binding simplifies the use of DataBinding and ViewBinding, and only requires one line of code to implement DataBinding and ViewBinding.
The future plan of Binding provides a general findViewById solution. Due to the iterative update of technology from butterknife, DataBinding, Kotlin synthesis method (Synthetic view) to the current ViewBinding, there may be new technologies in the future. No matter how the technology changes, just need Update Binding, the external use remains unchanged.
-
Kotlin synthesis method (Synthetic view) is so much more convenient than ViewBinding, why is it abandoned by Google, please check this article Kotlin 插件的落幕,ViewBinding 的崛起。
-
This article 竟然如此简单,DataBinding 和 ViewBinding analyzes the difference between DataBinding and ViewBinding from the perspective of use, and also introduces how to use simpler Ways to implement DataBinding and ViewBinding.
Thank you for your suggestions. At present, Binding has been adapted to a large number of scenarios. At the same time, it also provides a lot of practical cases of DataBinding and ViewBinding. If you encounter Binding incompatible scenarios during use, please raise an issue and I will solve it as soon as possible. .
If this repository is helpful to you, please give me star, thank you very much for your support, and welcome you to submit a PR ❤️❤️❤️
Binding the following advantages:
- Support using DataBinding or ViewBinding in custom ViewGroup
- Provides many cases including
Ativity,Fragment,Dialog,Adapter,include,merge,ViewStub,Navigationetc. - A simple API requires only one line of code to implement DataBinding or ViewBinding
- Support the use of DataBinding or ViewBinding in the
Activity、AppCompatActivity、FragmentActivity、Fragment、Dialog - Support the use of DataBinding or ViewBinding in the
ListAdapter、PagedListAdapter、PagingDataAdapter、RecyclerView.Adapter - Support the use of DataBinding and ViewBinding in Navigaion Fragment management framework, BottomSheetDialogFragment and other scenarios
- Avoid a lot of template code
- Avoid memory leaks, have life cycle awareness, and automatically destroy data when the life cycle is in
onDestroyed()
Download
Binding has been migrated to Maven Central because jCenter will be deprecated
add jcenter
Add the following code to the build.gradle file at the Project level
allprojects {
repositories {
// aliyun center 包含 mavenCentral 和 jcenter
maven { url "https://maven.aliyun.com/repository/public" }
// maven
mavenCentral()
}
}
add dependency
Add the following code to the module level build.gradle file, and you need to enable DataBinding or ViewBinding
android {
buildFeatures {
dataBinding = true
viewBinding = true
}
}
dependencies {
implementation 'com.hi-dhl:binding:${binding_version}'
}
The latest version
simple API
Binding provides a simple API as shown below.
ViewBinding
val binding: ActivityViewBindBinding by viewbind()
DataBinding
val binding: ActivityDataBindBinding by databind(R.layout.activity_data_bind)
or
val binding: ActivityDataBindBinding by databind()
let's see how to use in Ativity, Fragment, Dialog, Adapter, include, merge, ViewStub , Navigation , ViewGroup etc.
Usage
Use DataBinding and ViewBinding in Custom ViewGroup,
-
Use of ViewBinding :
- When the root layout is a non-merge label, use this method to initialize
val binding: LayoutViewCustomBinding by viewbind() - When the root layout is the merge tag, use this method for initialization
val binding: LayoutViewCustomBinding by viewbind(this)
- When the root layout is a non-merge label, use this method to initialize
-
Use of DataBinding
val binding: LayoutViewCustomDataBinding by databind(R.layout.layout_view_custom_data)
A detailed example is shown below。
class ViewBindCustomView @JvmOverloads constructor(
context: Context,
attr: AttributeSet? = null,
defStyleAttr: Int = 0,
) : LinearLayout(context, attr, defStyleAttr) {
// ViewBinding
// When the root layout is the merge tag, use this method for initialization
val binding: LayoutViewCustomBinding by viewbind(this)
// When the root layout is a non-merge label, use this method to initialize
val binding: LayoutViewCustomBinding by viewbind()
// DataBinding
val binding: LayoutViewCustomDataBinding by databind(R.layout.layout_view_custom_data)
init {
with(binding) {
result.setText("Use DataBinding and ViewBinding in Custom ViewGroup")
}
}
}
Use DataBinding and ViewBinding in Adapter (ListAdapter, PagingDataAdapter, RecyclerView.Adapter, etc.), add by viewbind() or by databind(), the example is as follows,see example
class ProductViewHolder(view: View) : RecyclerView.ViewHolder(view) {
// DataBinding
val binding: RecycleItemProductBinding by databind()
fun bindData(data: Product?, position: Int) {
binding.apply {
product = data
executePendingBindings()
}
}
}
class ProductViewHolderHeader(view: View) : RecyclerView.ViewHolder(view) {
// ViewBinding
val binding: RecycleItemProductHeaderBinding by viewbind()
fun bindData(data: Product?, position: Int) {
binding.apply {
name.text = "通过 ViewBinding 绑定的 head"
}
}
}
use in Activity, AppCompatActivity, and FragmentActivity, add by viewbind() or by databind(R.layout.activity_main).
class MainActivity : AppCompatActivity() {
// DataBinding
val binding: ActivityMainBinding by databind(R.layout.activity_main)
// ViewBinding
val binding: ActivityMainBinding by viewbind()
}
There are two ways in Fragment, and their use positions are different, as shown below.
- Method 1: Use in
onCreateView,see example(It is not recommended to use Method 1,It is recommended to use Method 2,see issue #13) - Method 2: Use in
onViewCreated,see example ViewBindFragment.kt and DataBindRecycleFragment.kt
Method 1:
class FragmentNav1 : Fragment() {
// DataBinding
val binding: FragmentMainBinding by databind()
// ViewBinding
val binding: FragmentMainBinding by viewbind()
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View {
return binding.root
}
}
Method 2:
class FragmentNav1 : Fragment(R.layout.fragment_main) {
// DataBinding
val binding: FragmentMainBinding by databind()
// ViewBinding
val binding: FragmentMainBinding by viewbind()
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
binding.apply { textView.setText("Binding") }
}
}
The usage in Dialog is as follows。
class AppDialog(context: Context) : Dialog(context, R.style.AppDialog) {
// DataBinding
val binding: DialogAppBinding by databind(R.layout.dialog_data_binding)
// ViewBinding
val binding: DialogAppBinding by viewbind()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding.apply { result.setText("DialogAppBinding") }
}
}
or add life cycle listening
class AppDialog
Related Skills
node-connect
349.9kDiagnose OpenClaw node connection and pairing failures for Android, iOS, and macOS companion apps
frontend-design
109.8kCreate distinctive, production-grade frontend interfaces with high design quality. Use this skill when the user asks to build web components, pages, or applications. Generates creative, polished code that avoids generic AI aesthetics.
openai-whisper-api
349.9kTranscribe audio via OpenAI Audio Transcriptions API (Whisper).
qqbot-media
349.9kQQBot 富媒体收发能力。使用 <qqmedia> 标签,系统根据文件扩展名自动识别类型(图片/语音/视频/文件)。
