View binding without an annotation processing and with just a few methods added to your dex.
private val recycler by bindView<RecyclerView>(R.id.my_view_id)
The library is hosted in the central repository.
repositories {
<...>
mavenCentral()
}
Add gradle dependency:
api "com.dmitryborodin:viewbinder-android:2.1.0"
Or just copy code from sources to your project. This library is very slim and was written in idea that you'll copy few functions instead of adding dependency.
If you are using Fragments, add the following code to your base Fragment:
private val resetter = BindingResetter()
fun<T> Fragment.bindView(@IdRes id: Int): ResettableLazy<T> = abstractBind(id, resetter)
And in onStop() implement
override fun onStop() {
super.onStop()
resetter.reset()
}
For non-Fragments no extra code is needed. Just bind your views.
Examples is in samples folder.
DialogFragment is a Fragment; Same for SupportLibrary
If your view is optional - just mark is as nullable
private val recycler by bindView<RecyclerView?>(R.id.my_view_id)
or
private val recycler : RecyclerView? by bindView<RecyclerView?>(R.id.my_view_id)
- Kotlin
- Build tools or support library version > 26
In Android studio run "Replace in path"
search for
@BindView\((.*)\) internal lateinit var (.*): (.*)
replace with
private val $2 by bindView<$3>($1)
Similar to KotterKnife, but:
- Available in jcenter and bintray
- Less abstractions/code/methods - simpler to copy code without maven dependencies
- No annotation processing involved -> fast compile time.
- Just a few methods in the library -> good for dex limit
- Clear and managable declaration of binded views -> good for you
- XML naming conventions are still independent from view names in code.
- Works only with Kotlin
- You have to add two lines to your parent Fragments
- No easy way to set click listeners with meta programming
- If you have kotlin-reflect library if your module available, unnecessary reflection calls will happen on each kotlin delegation creation (in out case for each view binding)