Spent nearly one weeks, the three frames have tried to use a bit, as to whether it is practical, I think this is the people, benevolent see, if your technology is not enough to break, then I suggest you use butterkinife on it, as for the other two, it is really some trouble to use, and, Probably don't know how it works.
Let's talk about it, why use annotations? Just for a few lines of code? I believe that no programmer without shortcut keys, more than a few findviewbyid should not bother it? Many people say that it is to reduce Findviewbyid and Onclicklistener, But I think, their real reason is to achieve the decoupling of code, programmers write code the highest level is to make the code pluggable, you can arbitrarily put their own functions and reduce the need for their own functions, and it has a benefit, it is convenient unit testing. Butterknife
Butterknife is a framework that many people prefer, why?
First: simple, whether it is the use of code or the environment of the building is relatively simple
Second: Bottom Friction
These two reasons are enough. The use of reflection is similar, but its underlying non-reflective mechanism, because we all know that the reflection mechanism is very good memory. Butterknife uses pre-compiled processing, so the internal friction is very low. Butterknife provides a lot of methods, but the common one is that two Findviewbyid and Setonclicklistener code
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 ...
}
}
The code after using Butterknife is this:
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" views ...
}
}
characteristics of Butter knifeView injection support in the Activity View injection Support view event callback function injection
The following event callback functions are currently supported:
View: @OnLongClick and @OnFocusChanged.
TextView: @OnEditorAction.
Adapterview: @OnItemClick and @OnItemLongClick.
Compoundbutton: @OnCheckedChanged.
inject in activity:
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" views ...
}
}
inject in Fragment:
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 = In Flater.inflate (R.layout.fancy_fragment, container, false);
Butterknife.inject (this, view);
TODO use "injected" views ...
return view;
}
}
inject in Viewholder mode:
public class Myadapter extends Baseadapter {
@Override public View getView (int position, view view, ViewGroup parent) {
Viewholder holder;
if (view! = null) {
holder = (viewholder) view.gettag ();
} else {
view = inflater.inflate (R.layout.whatever, p Arent, 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);}}
}
Inject callback function:
Here are some examples of methods for injecting callback functions:
With the button parameter
@OnClick (r.id.submit) public
void Sayhi (Button button) {
button.settext ("hello!");
}
No parameter
@OnClick (r.id.submit) public
Void Submit () {
//TODO submit data to server ...
}
Inject multiple View event
@OnClick ({r.id.door1, R.id.door2, R.id.door3}) public
void Pickdoor (Doorview door) {
I F (Door.hasprizebehind ()) {
Toast.maketext (this, "You win!", Length_short). Show ();
} else {
Toast.maketext (This, "Try again", Length_short). Show ();
}
}
Reset Function
If you need to set the injected View to Null when the interface is destroyed, you can use the Reset function:
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 = In Flater.inflate (R.layout.fancy_fragment, container, false);
Butterknife.inject (this, view);
TODO use "injected" views ...
return view;
}
@Override void Ondestroyview () {
super.ondestroyview ();
Views.reset (this);
}
}
Dependency Injection Related concepts
Dependency (Dependency): If a property in Class A is an instance of Class B, then Class B is a dependency of Class A, in this article we refer to Class A as the host, and the full text is represented by host , Class B is called dependency (Dependency), and the full text is represented by Dependency. One Host may be the Dependency of another class.
Host: If Class B is a Dependency of Class A, then class A is the host (host) of Class B.
Dependency Injection: If class B is a dependency,b assignment that is not written to a class or constructor, but is passed in by a constructor or other function parameter, this assignment is called Dependency injection. Dagger
Dagger because of its own complexity, in fact, is a difficult to get started library, difficult to learn, difficult to use good. But functionally speaking, it is also a very valuable library. and the forthcoming Dagger 2.0 has been handed over to Google for development and maintenance by Square, and from now on it is the official Google Library, so Dagger will have a big boost both in terms of official support and popularity.
(1). Dagger suitable for what kind of project
Dagger is a dependency injection library, and dependency injection is an excellent programming idea that allows you to decouple projects to improve the readability, scalability, and maintainability of a project and make unit testing easier. Therefore, Dagger applies to all projects.
(2). Dagger suitable for individuals and teams
Dagger is suitable for individuals and teams who have the ability to learn and are willing to learn. It is important to note that if you are the head of the development team, make sure that all of your players (or at least most of the players) meet these conditions before deciding to enable Dagger, otherwise Dagger may backfire, after all-it is not butterknife. Roboguice
Roboguice is a library developed on the Android platform based on Google Guice, which greatly simplifies the development of Android apps and some tedious and repetitive code. For example, the code might need to use Findviewbyid to find a view in XML and cast it to the desired type, and there might be a lot of similar code in OnCreate. Roboguice allows you to use annotation to describe the relationship between the ID and the view, and the rest of the work is done by the Roboguice library. Roboguice and Butterknife function similarly, But it is not butterknife easy to use, whether it is code writing or environment configuration is relatively more complex.