Bind Android views and callbacks to fields and methods.
Introduction
Annotate fields with @BindView and a view ID for Butter Knife to find and automatically cast the corresponding view in your layout.
Instead of slow reflection, code is generated to perform the view look-ups. Calling bind delegates to this generated code that you can see and debug.
The generated code for the above example is roughly equivalent to the following:
|
|
RESOURCE BINDING
Bind pre-defined resources with @BindBool, @BindColor, @BindDimen, @BindDrawable, @BindInt, @BindString, which binds an R.bool ID (or your specified type) to its corresponding field.
|
|
NON-ACTIVITY BINDING
You can also perform binding on arbitrary objects by supplying your own view root.
|
|
Another use is simplifying the view holder pattern inside of a list adapter.
|
|
You can see this implementation in action in the provided sample.
Calls to ButterKnife.bind can be made anywhere you would otherwise put findViewById calls.
Other provided binding APIs:
Bind arbitrary objects using an activity as the view root. If you use a pattern like MVC you can bind the controller using its activity with ButterKnife.bind(this, activity).
Bind a view’s children into fields using ButterKnife.bind(this). If you use
VIEW LISTS
You can group multiple views into a List or array.
|
|
The apply method allows you to act on all the views in a list at once.
|
|
Action and Setter interfaces allow specifying simple behavior.
|
|
An Android Property can also be used with the apply method.
LISTENER BINDING
Listeners can also automatically be configured onto methods.
|
|
All arguments to the listener method are optional.
|
|
Define a specific type and it will automatically be cast.
|
|
Specify multiple IDs in a single binding for common event handling.
|
|
Custom views can bind to their own listeners by not specifying an ID.
|
|
BINDING RESET
Fragments have a different view lifecycle than activities. When binding a fragment in onCreateView, set the views to null in onDestroyView.
Butter Knife returns an Unbinder instance when you call bind to do this for you. Call its unbind method in the appropriate lifecycle callback.
|
|
OPTIONAL BINDINGS
By default, both @Bind and listener bindings are required. An exception will be thrown if the target view cannot be found.
To suppress this behavior and create an optional binding, add a @Nullable annotation to fields or the @Optional annotation to methods.
Note: Any annotation named @Nullable can be used for fields. It is encouraged to use the @Nullable annotation from Android’s “support-annotations” library.
|
|
MULTI-METHOD LISTENERS
Method annotations whose corresponding listener has multiple callbacks can be used to bind to any one of them. Each annotation has a default callback that it binds to. Specify an alternate using the callback parameter.
|
|
BONUS
Also included are findById methods which simplify code that still has to find views on a View, Activity, or Dialog. It uses generics to infer the return type and automatically performs the cast.
|
|
Download
GRADLE
|
|
License
|
|