- 1.Introduction 简介 【黄油切刀】
- 1)Annotate fields with @Bind and a view ID for Butter Knife to find and automatically cast the corresponding view in your layout. 在定义视图元素前添加注释符号@Bind(ID) 注释框架会自动关联映射到相关联的布局文件中 ,运用在activity中
Butterknife.bind(this) 必须声明在setContentView之后
大致等价于:
- 2)Instead of slow reflection, code is generated to perform the view look-ups. Calling bind delegates to this generated code that you can see and debug.绑定操作
- 3)可以绑定关联映射各种定制视图View root, 比如在fragment中,利用方法Butterknife.bind(this,view);You can also perform binding on arbitrary objects by supplying your own view root.
- 4)第三个使用场景:ViewHolder 分流适配器管理ltem的视图Another use is simplifying the view holder pattern inside of a list adapter.
- 5)Bind arbitrary objects using an activity as the view root. If you use a pattern like MVC you can bind the controller using its activity with ButterKnife.bind(this, activity). 也是可以传入一个activity的
- 6)Bind a view’s children into fields using ButterKnife.bind(this). If you use tags in a layout and inflate in a custom view constructor you can call this immediately after. Alternatively, custom view types inflated from XML can use it in the onFinishInflate() callback.
- 7)使用场景四:多视图 apply方法 ,添加一个列表,成功和失败两种情况
- 8)使用场景五:控件监听器映射绑定,Listeners can also automatically be configured onto methods.可以自己定义方法名和传入参数。
- 9)使用场景六:在fragmnet中通常需要解除绑定,销毁视图后解除绑定,activity其实也有的Fragments have a different view lifecycle than activities. When binding a fragment in onCreateView, set the views to null in onDestroyView. Butter Knife has an unbind method to do this automatically.
- 10)可选项,虽然这么说,但是还是建议在开发时加上,原因是因为@Nullable 是为了抑制因找不到目标组件而出现的异常,By default, both @Bind and listener bindings are required. An exception will be thrown if the target view cannot be found.To suppress this behavior and create an optional binding, add a @Nullable annotation to the field or method,Note: Any annotation named @Nullable can be used for this purpose. It is encouraged to use the @Nullable annotation from Android’s “support-annotations” library,
参考:
http://tools.android.com/tech-docs/support-annotations
- 11)使用场景七:多个监听以及仿造findViewById 的Butterknife.findById()Method annotations whose corresponding listener has multiple callbacks can be used to bind to any one of them. Each annotation has a default callback that it binds to. Specify an alternate using the callback parameter.Also included are findById methods which simplify code that still has to find views on a View, Activity, or Dialog. It uses generics to infer the return type and automatically performs the cast.
http://www.tuicool.com/articles/Q3mmay/
http://blog.csdn.net/sbvfhp/article/details/47451835
http://jakewharton.github.io/butterknife/
http://jakewharton.github.io/butterknife/
http://blog.csdn.net/bboyfeiyu/article/details/52572963
- 1)Annotate fields with @Bind and a view ID for Butter Knife to find and automatically cast the corresponding view in your layout. 在定义视图元素前添加注释符号@Bind(ID) 注释框架会自动关联映射到相关联的布局文件中 ,运用在activity中
ButterKnife
最新推荐文章于 2022-09-28 20:35:50 发布