Detailed explanation of butterknife 8.0 used in Android annotation
preface:
Most of the time, the development of APP projects is mainly based on UI pages. At this time, we need to call a large number of codes such as findviewbyid and setonclicklistener. When there are few controls, we can accept them. When there are many controls, there is sometimes an impulse to smash the keyboard. So at this time, we think we can use annotation to separate us from this heavy work, and make the code more concise and easy to maintain. Today, we mainly learn butterknife, which focuses only on view, resource and action annotation framework.
Butterknife introduction
Butterknife is a view, resource and action injection framework focusing on Android system.
Official website: http://jakewharton.github.io/butterknife/
gitHub: https://github.com/JakeWharton/butterknife/
Comparison of butterknife before and after use:
See how we did it before using the view annotation
1.) before use
2.) after use
3.) butterknife advantages
Analyze the advantages of butterknife by using the front to back comparison above
Do you feel very simple and easy to use after comparing before and after use. Next, let's look at how to use it?
How butterknife works:
1) . add the following configuration in build.gradle of project
2.) add the following configuration in build.gradle of module
3.) injection and reset injection
Activity
Fragment: due to different view life cycles, you need to bind in oncreateview and unbind in ondestroyview
ViewHolder
4.) view injection @ bindview, @ bindviews
5.) resource injection
6) . Single Event Injection
A control specifies an event callback
You can also specify an event callback for multiple controls
Custom controls can also be bound to their own events without ID
7.) multi event callback
Some view listeners have multiple callback methods. For example, EditText adds addtextchangedlistener
It can be changed as follows by annotation
8.) selective injection
By default, both @ bind and listener injections are required. If the target view is not found, an error will be reported. In order to suppress this behavior, you can mark the field and method with @ optional annotation to make the injection optional. If the targetview exists, it will be injected. If it does not exist, nothing will be done. Or use Android's "support annotations" Modified by @ nullable in library
9.) butterknife. Apply() function
You can modify the action, setter and property of a view set element or a single view through the butterknifeapply() function
10.)ButterKnife.findById()
Butterknife also provides the findbyid function. You can obtain the views in activity, dialog and view through findbyid(). It is a generic type and does not need to be forced
Butterknife auto generate plug-in installation:
Search for zelezny download and add in Android Studio - > file - > Settings - > plugins - > to quickly generate the instance object of the corresponding component without writing it manually. When using, right-click on the layout resource code of the activity or fragment or viewholder to import annotations - > generate -- generate butterknife injections, and then the selection box as shown in the figure appears.
Plug in GitHub address: https://github.com/avast/android-butterknife-zelezny
The above gives a use flow chart, but the flow chart will not be for the latest version 8.0.1, but they are similar
The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.