Viewbinder

Additional

Language
Kotlin
Version
0.4.3-1 (May 13, 2019)
Created
Oct 25, 2017
Updated
Nov 11, 2019
Owner
Dmitry Borodin (Dmitry-Borodin)
Contributor
Dmitry Borodin (Dmitry-Borodin)
1
Activity
Badge
Generate
Download
Source code
APK file

Advertisement

viewbinder for Android

View binding without an annotation processing and with just a few methods added to your dex.

Usage:

private val recycler by bindView<RecyclerView>(R.id.my_view_id)

Add to your project:

The library is hosted in jcenter, it is available by default on new android projects

     repositories {
        <...>
        jcenter()
    }

Add gradle dependency:

api "com.viewbinder:viewbinder:0.4.3"

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.

Fragments

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.

Note:

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)

Requirements:

  1. Kotlin
  2. Build tools or support library version > 26

Migration from ButterKnife

In Android studio run "Replace in path"

search for @BindView\((.*)\) internal lateinit var (.*): (.*)

replace with private val $2 by bindView<$3>($1)