Brief introduction:
Programmers are lazy and don't want to write a lot of code like this
Class Exampleactivity extends Activity {TextView title; TextView subtitle; TextView footer; @Override public void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (r.layout.simple_activity); title = (TextView) Findviewbyid (r.id.title); subtitle = (TextView) Findviewbyid (r.id.subtitle); footer = (TextView) Findviewbyid (r.id.footer);//TODO Use views ...}}
That's not good, so we found a way to use magic annotations to condense the code, just like this
Class Exampleactivity extends Activity {@Magic (r.id.title) TextView title; @Magic (r.id.subtitle) TextView subtitle;@ Magic (r.id.footer) TextView footer, @Override public void OnCreate (Bundle savedinstancestate) {super.oncreate ( Savedinstancestate); Setcontentview (r.layout.simple_activity);//TODO use ' injected ' views ...}}
However, while this looks cool and magic, it can still lead to serious runtime problems.
As a result, we use a large tool in the Javac compiler to generate a reference to the Findviewbyid in the previous example, and continue to keep the code streamlined and continue to look at the following code
Class Exampleactivity extends Activity {@InjectView (r.id.title) TextView title; @InjectView (R.id.subtitle) TextView subtitle; @InjectView (r.id.footer) TextView footer; @Override public void OnCreate (Bundle savedinstancestate) { Super.oncreate (savedinstancestate); Setcontentview (r.layout.simple_activity); Butterknife.inject (this);//TODO use "injected" ...}}
In the original Magic place using the inject method to replace, the following is the method of a section of the representative (this way delegates to generated code unknown), you can see and debug
public void inject (exampleactivity activity) {Activity.subtitle = (Android.widget.TextView) Activity.findviewbyid ( 2130968578); activity.footer = (Android.widget.TextView) Activity.findviewbyid (2130968579); activity.title = ( Android.widget.TextView) Activity.findviewbyid (2130968577);}
(Can this paragraph be ignored?) It seems that the translation is not correct, not quite understand. Some people call this view injection, and add it to the traditional dependency injection framework, perhaps in terms of errors, but perhaps a bit of a role ....
Non-activity Injection
We can use injection for any view object, and of course we need to provide the object of view root
public class Fancyfragment extends Fragment {@InjectView (r.id.button1) button button1; @InjectView (r.id.button2) button button2; @Override View Oncreateview (layoutinflater inflater, ViewGroup container, Bundle savedinstancestate) {View view = Inflater.inflate (R.layout.fancy_fragment, container, false); Butterknife.inject (this, view);//TODO use "injected" Views...return view;}}
Another use is to simplify the view holder mode in list adapter, which can be seen in the sample provided.
public class Myadapter extends Baseadapter {@Override public View getView (int position, view view, ViewGroup parent) {view Holder holder;if (view = null) {Holder = (Viewholder) View.gettag ();} else {view = Inflater.inflate (R.layout.whatever, PA rent, false); holder = new Viewholder (view); View.settag (holder);} Holder.name.setText ("John Doe"),//etc ... return Convertview;} Static class Viewholder {@InjectView (r.id.title) TextView name; @InjectView (r.id.job_title) TextView jobtitle;public Viewholder (view view) {Butterknife.inject (this, view);}}}
Other APIs (this paragraph really suggests looking at the original)
-When using activity as the view root node, if you are using an MVC design pattern, you can inject the controller, use its acitvity and pass the Butterknife.inject (this, activity) method- Inject a sub-view Object field, using Butterknife.inject (This) (in the OnCreate method of the previous example, you can refer to this), if you use the <merge> tag in a layout constructor, and , inflate a custom view and calls this method immediately. Alternatively, if you are inflate a custom view from XML, you can use this method---onlayoutinflated ()
CLICK LISTENER Injection
Click listener can also be injected
@OnClick (R.id.submit) public void Submit () {//TODO submit data to server ...}
You can also add a View object, and the specific type will be automatically converted
@OnClick (r.id.submit) public void Sayhi (Button button) {button.settext ("hello!");}
Specify multiple IDs at once, plus events
@OnClick ({r.id.door1, R.id.door2, R.id.door3}) public void Pickdoor (Doorview door) {if (Door.hasprizebehind ()) { Toast.maketext (This, "You win!", Length_short). Show (); else {Toast.maketext (this, "Try again", Length_short). Show ();}}
Injection RESET
Fragments has more life cycles than avtivity, and if you need to inject a fragment into Oncreateview, destroy it in Ondestroyview
Butter Knife has a reset method that automatically implements it
public class Fancyfragment extends Fragment {@InjectView (r.id.button1) button button1; @InjectView (r.id.button2) button button2; @Override View Oncreateview (layoutinflater inflater, ViewGroup container, Bundle savedinstancestate) {View view = Inflater.inflate (R.layout.fancy_fragment, container, false); Butterknife.inject (this, view);//TODO use "injected" Views...return view;} @Override void Ondestroyview () {Super.ondestroyview (); Butterknife.reset (this);}}
OPTIONAL Injections
By default @InjectView and @onclick must be added, and if the current object does not exist, an exception is thrown
To suppress this exception, you can add an annotation to a variable or method.
@Optional @InjectView (r.id.might_not_be_there) TextView mightnotbethere; @Optional @OnClick (r.id.maybe_missing) void Onmaybemissingclicked () {//TODO ...} BONUS
This paragraph does not understand, the main idea is that in addition to the above injection method there is another, the type will be automatically converted, you need to import static Butterknife.findbyid
View view = Layoutinflater.from (context). Inflate (r.layout.thing, NULL); TextView FirstName = Butterknife.findbyid (view, r.id.first_name); TextView LastName = Butterknife.findbyid (view, r.id.last_name); ImageView photo = Butterknife.findbyid (view, R.id.photo );
Download: Current Version 4.0.1
Specific: Http://github.com/JakeWharton/butterknife
Jar Package: Http://repo1.maven.org/maven2/com/jakewharton/butterknife/4.0.1/butterknife-4.0.1.jar
MAVEN configuration: If you are a child shoe developed with MAVEN, please consciously add the following dependencies
<dependency>
<groupId>com.jakewharton</groupId>
<artifactId>butterknife</artifactId>
<version>4.0.1</version>
</dependency>
Configuration of the IDE
IntelliJ idea-If you are using an external tool to manage your project (such as Maven pom.xml), you should be able to work properly, or you can see this http://jakewharton.github.io/butterknife/ide-idea.html
Eclipse-http://jakewharton.github.io/butterknife/ide-eclipse.html
Proguard
Using injection causes Proguard to think that the variable is not being used, so you need to include the following field in the Proguard
-dontwarn butterknife.internal.**
-keep class **$ $ViewInjector {*;}
-keepnames class * {@butterknife. Injectview *;}
Turn http://www.it165.net/pro/html/201404/12375.html