Now in the development of Android project, Butterknife is a more commonly used annotated framework, which simplifies the reuse of Findviewbyid and improves the efficiency of programming.
But why use Butterknife? On the one hand, in order to improve the efficiency of programming, on the one hand butterknife to the system performance is not affected, because Butterknife is at compile time to generate a new class, not the runtime to reflect, so there is no impact
Butterknife now the latest version is Butterknife8, but the main use of butterknife6 and butterknife7 in development
Butterknife6 and Butterknife7 usage is slightly different.
(a) introduction of the Butterknife annotated Framework
In Android Studio can, soon introduced directly, we can, select items-right-click->open modules setting, then select Dependencies, select the Green Add button, enter com.jakewharton:butterknife:7.0.1 or com.jakewharton:butterknife:6.1.0 etc., introduce the framework, you can download the jar online, and then select the Add as library, Add to Project
(b) The main use of Butterknife
(i) In the activity class, use:
Demo: As long as you use Injectview, and then initialize in the OnCreate method
Butterknife.inject (class name. this);
@InjectView (R.id.listview) listview listview; @InjectView (r.id.tv_black) TextView mblack; @InjectView (r.id.message_title) TextView mtitle; Private hashmap<string,object> map; Private Context Mcontext; @Override protected void onCreate (Bundle arg0) { super.oncreate (arg0); Setcontentview (r.layout.activity_group_post); Butterknife.inject (grouppostactivity.this); Initview (); }
Butterknife7, replace it .@Bind can, initialize the switch toButterknife.bind (this);
(ii) Use in the Fragment class
public class Simplefragment extends Fragment { @InjectView (r.id.fragment_text_view) TextView Mtextview; Public simplefragment () { } @Override public View Oncreateview (layoutinflater inflater, ViewGroup Container, Bundle savedinstancestate) { View view = Inflater.inflate (R.layout.fragment_simple, container, FALSE); Butterknife.inject (this, view); Mtextview.settext ("TextView in Fragment is found!"); return view;} }
(iii) use in event processing
Onclicklistener can write like this.
@OnClick (R.id.basic_finish_a_button) void Finisha (view view) { finish ();
(IIII) used in ListView and GridView
@InjectViews ({r.id.label_first_name, r.id.label_middle_name, r.id.label_last_name}) list<textview> Labelviews;
can also be used in the adapter and so on.
Reference documentation is provided below
Reference blog: http://blog.csdn.net/u012468376/article/details/50594531 (Butterknife7)
Http://www.cnblogs.com/mengdd/archive/2015/06/23/4595973.html (BUTTERKNIFE6)
Example: Https://github.com/mengdd/AndroidButterKnifeSample official website: Http://jakewharton.github.io/butterknife/Java doc:http ://jakewharton.github.io/butterknife/javadoc/github Open Source project: Https://github.com/JakeWharton/butterknife
Android Annotation Framework Butterknife basic usage