@@ -25,7 +25,7 @@ Add it in your root build.gradle at the end of repositories:
25
25
``` groovy
26
26
allprojects {
27
27
repositories {
28
- ...
28
+ // ...
29
29
maven { url 'https://www.jitpack.io' }
30
30
}
31
31
}
@@ -49,25 +49,96 @@ dependencies {
49
49
}
50
50
```
51
51
52
- ## Wiki
52
+ ## Usage
53
53
54
- #### Kotlin usage
54
+ : pencil : ** [ Usage documentation ] ( https://dylancaicoding.github.io/ViewBindingKTX ) **
55
55
56
- - [ Using the extension function ] ( https://github.com/DylanCaiCoding/ViewBindingKtx/wiki/使用拓展函数 )
56
+ ## Sample
57
57
58
- - [ Modify base class ] ( https://github.com/DylanCaiCoding/ViewBindingKtx/wiki/改造基类-( Kotlin) )
58
+ Get the binding instance using the Kotlin property delegate:
59
59
60
- - [ Compatible with BRVAH] ( https://github.com/DylanCaiCoding/ViewBindingKtx/wiki/兼容-BRVAH-(Kotlin) )
60
+ ``` kotlin
61
+ class MainActivity : AppCompatActivity () {
61
62
62
- #### Java usage
63
+ private val binding: ActivityMainBinding by binding()
64
+ // private val binding by binding(ActivityMainBinding::inflate)
63
65
64
- - [ Modify base class] ( https://github.com/DylanCaiCoding/ViewBindingKtx/wiki/改造基类-(Java) )
66
+ override fun onCreate (savedInstanceState : Bundle ? ) {
67
+ super .onCreate(savedInstanceState)
68
+ binding.tvHelloWorld.text = " Hello Android!"
69
+ }
70
+ }
71
+ ```
72
+
73
+ ``` kotlin
74
+ class HomeFragment : Fragment (R .layout.fragment_home) {
75
+
76
+ private val binding: FragmentHomeBinding by binding()
77
+ // private val binding by binding(FragmentHomeBinding::bind)
78
+
79
+ private val childBinding: LayoutChildBinding by binding(Method .INFLATE )
80
+ // private val childBinding by binding { LayoutChildBinding.inflate(layoutInflater) }
81
+
82
+ override fun onViewCreated (view : View , savedInstanceState : Bundle ? ) {
83
+ super .onViewCreated(view, savedInstanceState)
84
+ binding.container.addView(childBinding.root)
85
+ }
86
+ }
87
+ ```
88
+
89
+ Use in the base class:
90
+
91
+ ``` kotlin
92
+ class MainActivity : BaseBindingActivity <ActivityMainBinding >() {
93
+
94
+ override fun onCreate (savedInstanceState : Bundle ? ) {
95
+ super .onCreate(savedInstanceState)
96
+ binding.tvHelloWorld.text = " Hello Android!"
97
+ }
98
+ }
99
+ ```
100
+
101
+ ``` kotlin
102
+ class HomeFragment : BaseBindingFragment <FragmentHomeBinding >() {
65
103
66
- - [ Compatible with BRVAH] ( https://github.com/DylanCaiCoding/ViewBindingKtx/wiki/兼容-BRVAH-(Java) )
104
+ override fun onViewCreated (view : View , savedInstanceState : Bundle ? ) {
105
+ super .onViewCreated(view, savedInstanceState)
106
+ binding.tvHelloWorld.text = " Hello Android!"
107
+ }
108
+ }
109
+ ```
110
+
111
+ Use multiple ways to compatible [ BaseRecyclerViewAdapterHelper] ( https://github.com/CymChad/BaseRecyclerViewAdapterHelper ) :
112
+
113
+ ``` kotlin
114
+ class FooAdapter : BaseQuickAdapter <Foo , BaseViewHolder >(R .layout.item_foo) {
115
+
116
+ override fun onCreateDefViewHolder (parent : ViewGroup , viewType : Int ): BaseViewHolder {
117
+ return super .onCreateDefViewHolder(parent, viewType).withBinding { ItemFooBinding .bind(it) }
118
+ }
119
+
120
+ override fun convert (holder : BaseViewHolder , item : Foo ) {
121
+ holder.getViewBinding<ItemFooBinding >().apply {
122
+ tvFoo.text = item.value
123
+ }
124
+ }
125
+ }
126
+ ```
127
+
128
+ ``` kotlin
129
+ class FooAdapter : BaseBindingQuickAdapter <Foo , ItemFooBinding >() {
130
+
131
+ override fun convert (holder : BaseBindingHolder <ItemFooBinding >, item : Foo ) {
132
+ holder.getViewBinding<ItemFooBinding >().apply {
133
+ tvFoo.text = item.value
134
+ }
135
+ }
136
+ }
137
+ ```
67
138
68
- #### Others
139
+ See the [ usage documentation ] ( https://dylancaicoding.github.io/ViewBindingKTX ) for more usage.
69
140
70
- - [ Q&A ] ( https://github.com/DylanCaiCoding/ViewBindingKtx/wiki/Q&A )
141
+ ##
71
142
72
143
## Change log
73
144
@@ -79,7 +150,7 @@ dependencies {
79
150
| ------------------------------------------------------------ | ------------------------------------------------------------ |
80
151
| [ Longan] ( https://github.com/DylanCaiCoding/Longan ) | A collection of Kotlin utils which makes Android application development faster and easier. |
81
152
| [ LoadingStateView] ( https://github.com/DylanCaiCoding/LoadingStateView ) | Decoupling the code of toolbar or loading status view. |
82
- | [ MMKV-KTX] ( https://github.com/DylanCaiCoding/MMKV-KTX ) | Easier to use the MMKV . |
153
+ | [ MMKV-KTX] ( https://github.com/DylanCaiCoding/MMKV-KTX ) | Use MMKV with property delegates . |
83
154
| [ ActivityResultLauncher] ( https://github.com/DylanCaiCoding/ActivityResultLauncher ) | Replace startActivityForResult() method gracefully. |
84
155
85
156
## Thanks
0 commit comments