Skip to content

Commit 383b4e8

Browse files
Update README_CN.md
1 parent 2c32d68 commit 383b4e8

File tree

1 file changed

+82
-14
lines changed

1 file changed

+82
-14
lines changed

README_CN.md

Lines changed: 82 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,13 @@
2020
- 支持 DataBinding 自动设置 lifecycleOwner
2121

2222
## Gradle
23-
23+
2424
在根目录的 build.gradle 添加:
2525

2626
```groovy
2727
allprojects {
2828
repositories {
29-
...
29+
// ...
3030
maven { url 'https://www.jitpack.io' }
3131
}
3232
}
@@ -50,25 +50,94 @@ dependencies {
5050
}
5151
```
5252

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 封装到基类进行使用:
5491

55-
#### Kotlin 用法
92+
```kotlin
93+
class MainActivity : BaseBindingActivity<ActivityMainBinding>() {
5694

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+
```
58101

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+
```
60111

61-
- [兼容 BRVAH](https://github.com/DylanCaiCoding/ViewBindingKtx/wiki/兼容-BRVAH-(Kotlin))
112+
提供多种方式兼容 [BaseRecyclerViewAdapterHelper](https://github.com/CymChad/BaseRecyclerViewAdapterHelper)
62113

63-
#### Java 用法
114+
```kotlin
115+
class FooAdapter : BaseQuickAdapter<Foo, BaseViewHolder>(R.layout.item_foo) {
64116

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+
```
66128

67-
- [兼容 BRVAH](https://github.com/DylanCaiCoding/ViewBindingKtx/wiki/兼容-BRVAH-(Java))
129+
```kotlin
130+
class FooAdapter : BaseBindingQuickAdapter<Foo, ItemFooBinding>() {
68131

69-
#### 其它
132+
override fun convert(holder: BaseBindingHolder<ItemFooBinding>, item: Foo) {
133+
holder.getViewBinding<ItemFooBinding>().apply {
134+
tvFoo.text = item.value
135+
}
136+
}
137+
}
138+
```
70139

71-
- [Q&A](https://github.com/DylanCaiCoding/ViewBindingKtx/wiki/Q&A)
140+
还有更多的用法,包括 Java 用法和不使用反射的用法,详细的请查看[使用文档](https://dylancaicoding.github.io/ViewBindingKTX)
72141

73142
## 更新日志
74143

@@ -80,15 +149,14 @@ dependencies {
80149
| ------------------------------------------------------------ | ---------------------------------------------- |
81150
| [Longan](https://github.com/DylanCaiCoding/Longan) | 简化 Android 开发的 Kotlin 工具类集合 |
82151
| [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 |
84153
| [ActivityResultLauncher](https://github.com/DylanCaiCoding/ActivityResultLauncher) | 优雅地替代 `startActivityForResult()` |
85154

86155
## 相关文章
87156

88157
讲解本库的封装思路
89158

90159
- [《优雅地封装和使用 ViewBinding,该替代 Kotlin synthetic 和 ButterKnife 了》](https://juejin.cn/post/6906153878312452103)
91-
92160
- [《ViewBinding 巧妙的封装思路,还能这样适配 BRVAH》](https://juejin.cn/post/6950530267547172901)
93161

94162
## Thanks

0 commit comments

Comments
 (0)