English | 中文
ViewBinding reduces exceptions caused by id or type errors, which are recommended by both Google officials and Jake Wharton, but it can be a bit cumbersome to use, so this library can help you use ViewBinding with as little code as possible in any usage scenario.
Add it in your root build.gradle at the end of repositories:
allprojects {
repositories {
// ...
maven { url 'https://www.jitpack.io' }
}
}
Add dependencies and configurations:
android {
buildFeatures {
viewBinding true
}
}
dependencies {
// The following are optional, please add as needed
implementation 'com.github.DylanCaiCoding.ViewBindingKTX:viewbinding-ktx:2.1.0'
implementation 'com.github.DylanCaiCoding.ViewBindingKTX:viewbinding-nonreflection-ktx:2.1.0'
implementation 'com.github.DylanCaiCoding.ViewBindingKTX:viewbinding-base:2.1.0'
implementation 'com.github.DylanCaiCoding.ViewBindingKTX:viewbinding-brvah:2.1.0'
}
:pencil: >> Usage documentation <<
Get the binding instance using the Kotlin property delegate:
class MainActivity : AppCompatActivity() {
private val binding: ActivityMainBinding by binding()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding.tvHelloWorld.text = "Hello Android!"
}
}
class HomeFragment : Fragment(R.layout.fragment_home) {
private val binding: FragmentHomeBinding by binding()
private val childBinding: LayoutChildBinding by binding(Method.INFLATE)
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
binding.container.addView(childBinding.root)
}
}
Use in the base class:
class MainActivity : BaseBindingActivity<ActivityMainBinding>() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding.tvHelloWorld.text = "Hello Android!"
}
}
class HomeFragment: BaseBindingFragment<FragmentHomeBinding>() {
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
binding.tvHelloWorld.text = "Hello Android!"
}
}
Use in BaseRecyclerViewAdapterHelper:
class FooAdapter : BaseQuickAdapter<Foo, BaseViewHolder>(R.layout.item_foo) {
override fun convert(holder: BaseViewHolder, item: Foo) {
holder.getBinding(ItemFooBinding::bind).apply {
tvFoo.text = item.value
}
}
}
See the usage documentation for more usage.
Library | Description |
---|---|
Longan | Probably the best Kotlin utils library for Android. |
LoadingStateView | Decoupling the code of toolbar or loading status view. |
MMKV-KTX | Use MMKV with property delegates. |
Tracker | A lightweight tracking framework based on the tracking idea of Buzzvideo. |
Thanks ViewBindingPropertyDelegate for providing an idea without reflection
Copyright (C) 2020. Dylan Cai
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.