20
20
- 支持 DataBinding 自动设置 lifecycleOwner
21
21
22
22
## Gradle
23
-
23
+
24
24
在根目录的 build.gradle 添加:
25
25
26
26
``` groovy
27
27
allprojects {
28
28
repositories {
29
- ...
29
+ // ...
30
30
maven { url 'https://www.jitpack.io' }
31
31
}
32
32
}
@@ -50,25 +50,94 @@ dependencies {
50
50
}
51
51
```
52
52
53
- ## Wiki
53
+ ## 用法
54
+
55
+ :pencil : ** [ 使用文档] ( https://dylancaicoding.github.io/ViewBindingKTX ) **
56
+
57
+ ## 示例
58
+
59
+ 使用 Kotlin 属性委托的方式获取 binding 对象:
60
+
61
+ ``` kotlin
62
+ class MainActivity : AppCompatActivity () {
63
+
64
+ private val binding: ActivityMainBinding by binding()
65
+ // private val binding by binding(ActivityMainBinding::inflate)
66
+
67
+ override fun onCreate (savedInstanceState : Bundle ? ) {
68
+ super .onCreate(savedInstanceState)
69
+ binding.tvHelloWorld.text = " Hello Android!"
70
+ }
71
+ }
72
+ ```
73
+
74
+ ``` kotlin
75
+ class HomeFragment : Fragment (R .layout.fragment_home) {
76
+
77
+ private val binding: FragmentHomeBinding by binding()
78
+ // private val binding by binding(FragmentHomeBinding::bind)
79
+
80
+ private val childBinding: LayoutChildBinding by binding(Method .INFLATE )
81
+ // private val childBinding by binding { LayoutChildBinding.inflate(layoutInflater) }
82
+
83
+ override fun onViewCreated (view : View , savedInstanceState : Bundle ? ) {
84
+ super .onViewCreated(view, savedInstanceState)
85
+ binding.container.addView(childBinding.root)
86
+ }
87
+ }
88
+ ```
89
+
90
+ 把 ViewBinding 封装到基类进行使用:
54
91
55
- #### Kotlin 用法
92
+ ``` kotlin
93
+ class MainActivity : BaseBindingActivity <ActivityMainBinding >() {
56
94
57
- - [ 使用拓展函数] ( https://github.com/DylanCaiCoding/ViewBindingKtx/wiki/使用拓展函数 )
95
+ override fun onCreate (savedInstanceState : Bundle ? ) {
96
+ super .onCreate(savedInstanceState)
97
+ binding.tvHelloWorld.text = " Hello Android!"
98
+ }
99
+ }
100
+ ```
58
101
59
- - [ 改造基类] ( https://github.com/DylanCaiCoding/ViewBindingKtx/wiki/改造基类-(Kotlin) )
102
+ ``` kotlin
103
+ class HomeFragment : BaseBindingFragment <FragmentHomeBinding >() {
104
+
105
+ override fun onViewCreated (view : View , savedInstanceState : Bundle ? ) {
106
+ super .onViewCreated(view, savedInstanceState)
107
+ binding.tvHelloWorld.text = " Hello Android!"
108
+ }
109
+ }
110
+ ```
60
111
61
- - [ 兼容 BRVAH ] ( https://github.com/DylanCaiCoding/ViewBindingKtx/wiki/兼容-BRVAH-(Kotlin) )
112
+ 提供多种方式兼容 [ BaseRecyclerViewAdapterHelper ] ( https://github.com/CymChad/BaseRecyclerViewAdapterHelper ) :
62
113
63
- #### Java 用法
114
+ ``` kotlin
115
+ class FooAdapter : BaseQuickAdapter <Foo , BaseViewHolder >(R .layout.item_foo) {
64
116
65
- - [ 改造基类] ( https://github.com/DylanCaiCoding/ViewBindingKtx/wiki/改造基类-(Java) )
117
+ override fun onCreateDefViewHolder (parent : ViewGroup , viewType : Int ): BaseViewHolder {
118
+ return super .onCreateDefViewHolder(parent, viewType).withBinding { ItemFooBinding .bind(it) }
119
+ }
120
+
121
+ override fun convert (holder : BaseViewHolder , item : Foo ) {
122
+ holder.getViewBinding<ItemFooBinding >().apply {
123
+ tvFoo.text = item.value
124
+ }
125
+ }
126
+ }
127
+ ```
66
128
67
- - [ 兼容 BRVAH] ( https://github.com/DylanCaiCoding/ViewBindingKtx/wiki/兼容-BRVAH-(Java) )
129
+ ``` kotlin
130
+ class FooAdapter : BaseBindingQuickAdapter <Foo , ItemFooBinding >() {
68
131
69
- #### 其它
132
+ override fun convert (holder : BaseBindingHolder <ItemFooBinding >, item : Foo ) {
133
+ holder.getViewBinding<ItemFooBinding >().apply {
134
+ tvFoo.text = item.value
135
+ }
136
+ }
137
+ }
138
+ ```
70
139
71
- - [ Q&A ] ( https://github.com/DylanCaiCoding/ViewBindingKtx/wiki/Q&A )
140
+ 还有更多的用法,包括 Java 用法和不使用反射的用法,详细的请查看 [ 使用文档 ] ( https://dylancaicoding. github.io/ViewBindingKTX ) 。
72
141
73
142
## 更新日志
74
143
@@ -80,15 +149,14 @@ dependencies {
80
149
| ------------------------------------------------------------ | ---------------------------------------------- |
81
150
| [ Longan] ( https://github.com/DylanCaiCoding/Longan ) | 简化 Android 开发的 Kotlin 工具类集合 |
82
151
| [ LoadingStateView] ( https://github.com/DylanCaiCoding/LoadingStateView ) | 深度解耦标题栏或加载中、加载失败、无数据等视图 |
83
- | [ MMKV-KTX] ( https://github.com/DylanCaiCoding/MMKV-KTX ) | 让 MMKV 更加易用 |
152
+ | [ MMKV-KTX] ( https://github.com/DylanCaiCoding/MMKV-KTX ) | 用属性委托的方式使用 MMKV |
84
153
| [ ActivityResultLauncher] ( https://github.com/DylanCaiCoding/ActivityResultLauncher ) | 优雅地替代 ` startActivityForResult() ` |
85
154
86
155
## 相关文章
87
156
88
157
讲解本库的封装思路
89
158
90
159
- [ 《优雅地封装和使用 ViewBinding,该替代 Kotlin synthetic 和 ButterKnife 了》] ( https://juejin.cn/post/6906153878312452103 )
91
-
92
160
- [ 《ViewBinding 巧妙的封装思路,还能这样适配 BRVAH》] ( https://juejin.cn/post/6950530267547172901 )
93
161
94
162
## Thanks
0 commit comments