• Stars
    star
    426
  • Rank 101,884 (Top 3 %)
  • Language
    Kotlin
  • License
    Apache License 2.0
  • Created almost 4 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Simple API implement DataBinding and ViewBinding. 简单的 API 实现 DataBinding 和 ViewBinding,欢迎 star

Binding

Simple API implement DataBinding and ViewBinding. Welcome star
简单的 API 实现 DataBinding 和 ViewBinding,欢迎 star

English   ·  中文

If the image cannot be viewed, please click here to view it img1 | img2

Thanks

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.

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 , Navigation etc.
  • A simple API requires only one line of code to implement DataBinding or ViewBinding
  • Support the use of DataBinding or ViewBinding in the ActivityAppCompatActivityFragmentActivityFragmentDialog
  • Support the use of DataBinding or ViewBinding in the ListAdapterPagedListAdapterPagingDataAdapterRecyclerView.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)
  • 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:
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(context: Context,lifecycle: Lifecycle) : Dialog(context, R.style.AppDialog) {

    // use DataBinding life cycle listening
    val binding: DialogAppBinding by databind(R.layout.dialog_data_binding, lifecycle)
    
    // use ViewBinding life cycle listening
    val binding: DialogAppBinding by viewbind(lifecycle)

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding.apply { result.setText("DialogAppBinding") }
    }
}

Extension method that supports DataBinding to bind data when initialized,Thanks to @br3ant contribute,see example

val binding: ActivityDataBindBinding by databind(R.layout.activity_data_bind) {
    val account = Account()
    account.name = "test"
    this.account = account
}

Do not want to generate a binding class for a layout, add the following attributes to the root view of the layout file

<LinearLayout tools:viewBindingIgnore="true" >
</LinearLayout>

proguard

-keepclassmembers class ** implements androidx.viewbinding.ViewBinding {
    public static ** bind(***);
    public static ** inflate(***);
    public static ** inflate(**,**);
}

change log

2023-04-13(V1.2.0)

  • 兼容 lifecycle 2.6.0 FullLifecycleObserver 被删除的问题

2022-10-07(V1.1.9)

  • Compatible with jdk >= 1.8 version

2022-7-13(V1.1.7)

  • 兼容不传 ViewGroup 的情况#34

2022-5-12(V1.1.5)

  • 修改 ViewGroup 销毁的时候释放资源 #33

2022-5-03(V1.1.4)

  • 修复了生命周期问题
  • 修复了销毁之后再次使用,由于 delegate property 会被再次初始化,出现的异常 #31

2021-5-09(V1.1.3)

  • fix not found inflate(LayoutInflater) in the ViewGroup #26

2021-3-09(V1.1.2)

  • Fixed the issue that the diff with Fragment Lifecycle #18
  • Binding has been migrated to Maven Central

2021-1-25(V1.0.9)

  • Fixed the issue that the diff with Fragment Lifecycle and View Lifecycle #15
  • Fixed the issue that the layout attribute of the root view of Fragment is invalid #13

2021-1-14(V1.0.8)

  • Support using DataBinding or ViewBinding in custom ViewGroup
  • add use cases in ViewGroup

2020-12-31

2020-12-28(V1.0.6)

  • Support Activity and Fragment to automatically bind LifecycleOwner。see issue

2020-12-21(V1.0.5)

  • Support using DataBinding and ViewBinding in navigation fragment,see example

2020-12-17(V1.0.4)

  • Support all Adapters related to RecyclerView.ViewHolder (ListAdapter, PagingDataAdapter, RecyclerView.Adapter, etc.) to use DataBinding and ViewBinding,see example

  • Extension method that supports DataBinding to bind data when initialized,Thanks to @br3ant contribute,see example

2020-12-15(V1.0.3)

  • Use of DataBinding in Dialog, by databind(R.layout.dialog_data_binding) or by databind(R.layout.dialog_data_binding, lifecycle)
  • Avoid memory leaks, have life cycle awareness, and automatically destroy data when the life cycle is in onDestroyed()
  • The minimum SDK version is reduced to 14

2020-12-14:

  • Demo adds DataBinding example
  • Demo adds ViewBinding example
  • Demo adds kotlin-parcelize example

2020-12-13(V1.0.1)

  • Use ViewBinding in Dialog, add by viewbind() or by viewbind(lifecycle)

2020-12-12(V1.0.0)

  • A simple API requires only one line of code to implement DataBinding or ViewBinding
  • Support the use of DataBinding or ViewBinding in the ActivityAppCompatActivityFragmentActivityFragment
  • Avoid a lot of template code
  • Avoid memory leaks, have life cycle awareness, and automatically destroy data when the life cycle is in onDestroyed()

contact me

  • 个人微信:hi-dhl
  • 公众号:ByteCode,包含 Jetpack ,Kotlin ,Android 10 系列源码,译文,LeetCode / 剑指 Offer / 多线程 / 国内外大厂算法题 等等一系列文章


最后推荐我一直在更新维护的项目和网站:

  • 计划建立一个最全、最新的 AndroidX Jetpack 相关组件的实战项目 以及 相关组件原理分析文章,正在逐渐增加 Jetpack 新成员,仓库持续更新,欢迎前去查看:AndroidX-Jetpack-Practice

  • LeetCode / 剑指 offer / 国内外大厂面试题 / 多线程 题解,语言 Java 和 kotlin,包含多种解法、解题思路、时间复杂度、空间复杂度分析

  • 最新 Android 10 源码分析系列文章,了解系统源码,不仅有助于分析问题,在面试过程中,对我们也是非常有帮助的,仓库持续更新,欢迎前去查看 Android10-Source-Analysis

  • 整理和翻译一系列精选国外的技术文章,每篇文章都会有译者思考部分,对原文的更加深入的解读,仓库持续更新,欢迎前去查看 Technical-Article-Translation

  • 「为互联网人而设计,国内国外名站导航」涵括新闻、体育、生活、娱乐、设计、产品、运营、前端开发、Android 开发等等网址,欢迎前去查看 为互联网人而设计导航网站

More Repositories

1

PokemonGo

神奇宝贝 (PokemonGo) 基于 Jetpack + MVVM + Repository 设计模式 + Data Mapper + Kotlin Flow 的实战项目,如果这个仓库对你有帮助,请仓库右上角帮我 star 一下,非常感谢。
Kotlin
1,595
star
2

AndroidX-Jetpack-Practice

本仓库致力于建立最全、最新的的 AndroidX Jetpack 相关组件的实践项目 以及组件对应的分析文章(持续更新中)如果对你有帮助,请在右上角 star 一下,感谢
Kotlin
1,499
star
3

Leetcode-Solutions-with-Java-And-Kotlin

LeetCode 系列题解, 在线阅读 https://offer.hi-dhl.com
Kotlin
423
star
4

ComposingBuilds-vs-buildSrc

Composing builds 和 buildSrc 优势劣势对比,仓库提供了 buildSrc 和 Composing builds 两个构建脚本,下文有使用方法,如果对你有帮助,请在右上角 star 一下,感谢
Kotlin
406
star
5

Android10-Source-Analysis

致力于分享一系列 Android 10 系统源码,持续更新中...... 如果对你有帮助,请在右上角 star 一下,感谢
355
star
6

SyncKit

将本地的项目同步到远程设备,本地写代码,远程编译,将编译的结果同步到本地
Kotlin
201
star
7

Technical-Article-Translation

本仓库致力于致力于分享一系列精选国外的技术文章(持续更新)
181
star
8

KtKit

KtKit 小巧而实用,用 Kotlin 语言编写的工具库(长期更新中)
Kotlin
164
star
9

JProgressView

一个灵活的进度条,支持图形:圆形、圆角矩形、矩形等等,陆续会添加更多的图形,如果对你有帮助,请在右上角 star 一下,感谢
Kotlin
76
star
10

JDataBinding

JDataBinding 是基于 DataBinding 封装的基础库,持续更新中 ...... 如果对你有帮助,请在右上角 star 一下,感谢
Kotlin
75
star
11

fast_guides

10分钟入门Shell脚本编程
Shell
50
star
12

MAD-Skills

Google 近期发布了 MAD Skills 新系列教程,旨在帮助开发者使用最新的技术,开发更好的应用程序,将会包含(双语视频、文章、案例、源码)
50
star
13

DeviceMonitorPlugin

解决在Android Studio 3.2找不到Android Device Monitor工具
Java
42
star
14

HarmonyPractice

这个仓库主要用于演示 ArkTS 语法和 鸿蒙组件的使用,以及鸿蒙的实战项目
Objective-C
36
star
15

DebugApkSmali

基于Smali文件 Android Studio 动态调试 APP
Smali
35
star
16

screenVideo

screenVideo是一个通用的视频截图工具,目前已经适配大部分机型,对于个别机型不能使用的欢迎issuses
Java
13
star
17

KtPractice

这个仓库用于实践和测试 Kotlin 、 Java 性能 和 新语法相关的代码案例,正在陆续添加新的案例。欢迎 star
Kotlin
13
star
18

AOSP-PackageInstaller

系统安装器PackageInstaller(7.1.2、8.1.0、9.0.0、10.0.0)相关的源码
12
star
19

hi-dhl

11
star
20

Leetcode-Solution-CPP-C

LeetCode / 剑指 offer 系列题解, 在线阅读 https://offer.hi-dhl.com
C++
5
star
21

PersonalBlog

personal blog
HTML
1
star