《butterknife 注解详解.docx》由会员分享,可在线阅读,更多相关《butterknife 注解详解.docx(7页珍藏版)》请在金锄头文库上搜索。
1、butterknife 注解详解IntroductionAnnotate fields withBindand a view ID for Butter Knife to find and automatically cast the corresponding view in your layout.class ExampleActivity extends Activity Bind(R.id.title) TextView title; Bind(R.id.subtitle) TextView subtitle; Bind(R.id.footer) TextView footer; Ov
2、erride public void onCreate(Bundle savedInstanceState) super.onCreate(savedInstanceState); setContentView(R.layout.simple_activity); ButterKnife.bind(this); / TODO Use fields. Instead of slow reflection, code is generated to perform the view look-ups. Callingbinddelegates to this generated code that
3、 you can see and debug.The generated code for the above example is roughly equivalent to the following:public void bind(ExampleActivity activity) activity.subtitle = (android.widget.TextView) activity.findViewById(2130968578); activity.footer = (android.widget.TextView) activity.findViewById(2130968
4、579); activity.title = (android.widget.TextView) activity.findViewById(2130968577);RESOURCE BINDINGBind pre-defined resources withBindBool,BindColor,BindDimen,BindDrawable,BindInt,BindString, which binds anR.boolID (or your specified type) to its corresponding field.class ExampleActivity extends Act
5、ivity BindString(R.string.title) String title; BindDrawable(R.drawable.graphic) Drawable graphic; BindColor(R.color.red) int red; / int or ColorStateList field BindDimen(R.dimen.spacer) Float spacer; / int (for pixel size) or float (for exact value) field / .NON-ACTIVITY BINDINGYou can also perform
6、binding on arbitrary objects by supplying your own view root.public class FancyFragment extends Fragment Bind(R.id.button1) Button button1; Bind(R.id.button2) Button button2; Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) View view = inflat
7、er.inflate(R.layout.fancy_fragment, container, false); ButterKnife.bind(this, view); / TODO Use fields. return view; Another use is simplifying the view holder pattern inside of a list adapter.public class MyAdapter extends BaseAdapter Override public View getView(int position, View view, ViewGroup
8、parent) ViewHolder holder; if (view != null) holder = (ViewHolder) view.getTag(); else view = inflater.inflate(R.layout.whatever, parent, false); holder = new ViewHolder(view); view.setTag(holder); holder.name.setText(John Doe); / etc. return view; static class ViewHolder Bind(R.id.title) TextView n
9、ame; Bind(R.id.job_title) TextView jobTitle; public ViewHolder(View view) ButterKnife.bind(this, view); You can see this implementation in action in the provided sample.Calls toButterKnife.bindcan be made anywhere you would otherwise putfindViewByIdcalls.Other provided binding APIs: Bind arbitrary o
10、bjects using an activity as the view root. If you use a pattern like MVC you can bind the controller using its activity withButterKnife.bind(this, activity). Bind a views children into fields usingButterKnife.bind(this). If you usetags in a layout and inflate in a custom view constructor you can cal
11、l this immediately after. Alternatively, custom view types inflated from XML can use it in theonFinishInflate()callback.VIEW LISTSYou can group multiple views into aListor array.Bind( R.id.first_name, R.id.middle_name, R.id.last_name )List nameViews;Theapplymethod allows you to act on all the views
12、in a list at once.ButterKnife.apply(nameViews, DISABLE);ButterKnife.apply(nameViews, ENABLED, false);ActionandSetterinterfaces allow specifying simple behavior.static final ButterKnife.Action DISABLE = new ButterKnife.Action() Override public void apply(View view, int index) view.setEnabled(false);
13、;static final ButterKnife.Setter ENABLED = new ButterKnife.Setter() Override public void set(View view, Boolean value, int index) view.setEnabled(value); ;An AndroidPropertycan also be used with theapplymethod.ButterKnife.apply(nameViews, View.ALPHA, 0.0f);LISTENER BINDINGListeners can also automati
14、cally be configured onto methods.OnClick(R.id.submit)public void submit(View view) / TODO submit data to server.All arguments to the listener method are optional.OnClick(R.id.submit)public void submit() / TODO submit data to server.Define a specific type and it will automatically be cast.OnClick(R.id.submit)public void sayHi(Button button) button.setText(Hello!);Specify multiple IDs in a single binding for common event handling.OnClick( R.id.door1, R.id.door2, R.id.door3 )public void pickDoor(DoorView door) if (door.hasPrizeBehind()