From: http://www.cnblogs.com/whoislcj/p/5620128.html) author: li to write code
Introduction: Most of the App project development is still based on THE UI page, then we need to call a lot of findViewById and setOnClickListener code, when the control is less we can still accept, more controls sometimes there will be an impulse to hit the keyboard. So at this time, we thought we could use annotations to get us out of this heavy work, and also make the code more concise and easy to maintain. Today, we mainly learn to focus on View, Resource and Action annotation framework ButterKnife.
ButterKnife is an Android View, Resource, and Action injection framework. Website: http://jakewharton.github.io/butterknife/ making: https://github.com/JakeWharton/butterknife/ # # ButterKnife contrast before and after use Look at how didn’t use the View annotations before we do
###### before use
public class ExampleActivity extends AppCompatActivity { private final static String TAG = ExampleActivity.class.getSimpleName(); String butterKnifeStr; Drawable butterKnifeDrawable; Button butterKnifeBtn; ImageView butterKnifeIv; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_butter_knife); initResource(); initViews(); } private void initViews() { butterKnifeBtn = (Button) findViewById(R.id.btn_butter_knife); butterKnifeBtn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Log.e(TAG, "onButterKnifeBtnClick"); }}); butterKnifeIv = (ImageView) findViewById(R.id.iv_butter_knife); butterKnifeBtn.setText(butterKnifeStr); butterKnifeIv.setImageDrawable(butterKnifeDrawable); } private void initResource() { butterKnifeStr = getString(R.string.title_btn_butter_knife); butterKnifeDrawable = getDrawable(R.mipmap.ic_launcher); }Copy the code
}
###### after use
ButterKnife’s powerful View binding and Click event handling functions simplify code by comparing the advantages of ButterKnife before and after the above use. Convenient processing Adapter ViewHolder binding problem will not affect the efficiency of APP running, easy configuration, clear code, readable
Do you feel very simple and easy to use after using before and after. Now, how does it work? How to use ButterKnife: 1.) Add the following configuration to Project build.gradle
2.) Add the following configuration to build.gradle of Module
3.) Inject and reset the injected Activity
Fragment: Need onCreateView bind and onDestroyView unbind due to different view life cycles
ViewHolder
4.) View infuse @bindView, @bindviews @bindView (R.i.b.tn_butter_knife) Button butterKnifeBtn; @BindViews({R.id.tv_butter_knife1,R.id.tv_butter_knife2,R.id.tv_butter_knife3}) List textViews;
5.) Resource injection
6.) Single event injection specifies an event callback for a control
/ @OnClick(R.id.btn_butter_knife) public void onButterKnifeBtnClick() { } /
/ @OnClick(R.id.btn_butter_knife) public void onButterKnifeBtnClick(View view) { Log.e(TAG, “onButterKnifeBtnClick”); } /
You can also specify an event callback for multiple controls
Custom controls can also bind to their own events without an ID
Some View listeners have multiple callback methods, such as EditText adding addTextChangedListener
You can annotate this as follows
By default, injection of @bind and listener is required, and an error will be reported if the target view is not found. To suppress this behavior, mark fields and methods with the @optional annotation to make injection Optional, inject if targetView exists, and do nothing if it doesn’t. Or use Android’s “support-Annotations” Library. @nullable
9.) The butterknife.apply () function modifies the actions, setters, and properties of view elements or individual views using the ButterKnifeapply() function
FindById () ¶ findById() ¶ findById() ¶ findById() ¶ findById() ¶ View = LayoutInflater. From (context).inflate(r.layout.thing, null); View = LayoutInflater. 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);
Install the plugin for automatic generation of ButterKnife: Go to AndroidStudio->File->Settings->Plugins-> Zelezny to download and add the plugin. You can quickly generate the corresponding component instance object without manually writing. To use it, right-click on the Activity or Fragment or ViewHolder’s Layout resource code to import the annotation — >Generate — Generate ButterKnife Injections and the selection box shown in the image appears. Plug-in making address: https://github.com/avast/android-butterknife-zelezny gave a flow chart of use, but the flow chart for the latest version 8.0.1, but are similar
In our line of work, when we slack off, it means the stop of progress, and the stop of progress means being eliminated, we can only go forward, until the nirvana of phoenix one day!