-
Notifications
You must be signed in to change notification settings - Fork 48
/
ViewBindCustomView.kt
53 lines (43 loc) · 1.51 KB
/
ViewBindCustomView.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package com.hi.dhl.demo.binding.viewbind
import android.content.Context
import android.util.AttributeSet
import android.widget.LinearLayout
import com.hi.dhl.binding.viewbind
import com.hi.dhl.demo.binding.databinding.LayoutMergeItemBinding
import com.hi.dhl.demo.binding.databinding.LayoutViewCustomBinding
/**
* <pre>
* author: dhl
* date : 2021/1/14
* desc :
* </pre>
*/
class ViewBindCustomView @JvmOverloads constructor(
context: Context,
attr: AttributeSet? = null,
defStyleAttr: Int = 0,
) : LinearLayout(context, attr, defStyleAttr) {
lateinit var onDialogClickListener: OnDialogClickListener
// 当根布局为 merge 标签,使用此方法进行初始化
val binding: LayoutViewCustomBinding by viewbind(this)
// 当根布局是非 merge 标签,使用此方法进行初始化
// val binding: LayoutViewCustomBinding by viewbind()
init {
with(binding) {
result.setText("这是 ViewGroup 通过 ViewBinding 绑定")
result.setOnClickListener {
if (::onDialogClickListener.isInitialized) {
onDialogClickListener.onClick()
}
}
LayoutMergeItemBinding.bind(root)
.mergeTvTitle.setText("在 ViewGroup 中使用 merge 标签")
}
}
fun setDialogClickListener(onDialogClickListener: OnDialogClickListener) {
this.onDialogClickListener = onDialogClickListener
}
interface OnDialogClickListener {
fun onClick()
}
}