Butterknife 简单使用
2016-04-12 13:47
211 查看
介绍
Activity中使用@Bind来对id进行绑定。
class ExampleActivity extends Activity { @Bind(R.id.title) TextView title; @Bind(R.id.subtitle) TextView subtitle; @Bind(R.id.footer) TextView footer; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.simple_activity); ButterKnife.bind(this); // TODO Use fields... } }
在Fragment中使用:
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 = inflater.inflate(R.layout.fancy_fragment, container, false); ButterKnife.bind(this, view); // TODO Use fields... return view; } }
在ViewHolder中使用:
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, 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 name; @Bind(R.id.job_title) TextView jobTitle; public ViewHolder(View view) { ButterKnife.bind(this, view); } } }
绑定时机:
在 activity 中使用 ButterKnife.bind(this, activity),进行绑定。
Bind a view's children into fields using ButterKnife.bind(this).
自定义view中在onFinishInflate() 进行绑定。
多个View或者数组等进行绑定:
@Bind({ R.id.first_name, R.id.middle_name, R.id.last_name }) List<EditText> nameViews;
The apply method allows you to act on all the views in a list at once.
ButterKnife.apply(nameViews, DISABLE); ButterKnife.apply(nameViews, ENABLED, false);
Action and Setter interfaces allow specifying simple behavior.
static final ButterKnife.Action<View> DISABLE = new ButterKnife.Action<View>() { @Override public void apply(View view, int index) { view.setEnabled(false); } }; static final ButterKnife.Setter<View, Boolean> ENABLED = new ButterKnife.Setter<View, Boolean>() { @Override public void set(View view, Boolean value, int index) { view.setEnabled(value); } };
An Android Property can also be used with the apply method.
ButterKnife.apply(nameViews, View.ALPHA, 0.0f);
LISTENER 绑定
@OnClick(R.id.submit) public void submit(View view) { // TODO submit data to server... }
在listener 中,所有参数可选。
@OnClick(R.id.submit) public void submit() { // TODO submit data to server... }
@OnClick(R.id.submit) public void sayHi(Button button) { button.setText("Hello!"); }
多个view绑定同一个监听器
@OnClick({ R.id.door1, R.id.door2, R.id.door3 }) public void pickDoor(DoorView door) { if (door.hasPrizeBehind()) { Toast.makeText(this, "You win!", LENGTH_SHORT).show(); } else { Toast.makeText(this, "Try again", LENGTH_SHORT).show(); } }
自定义Views的自身的事件绑定
public class FancyButton extends Button { @OnClick public void onClick() { // TODO do something! } }
BINDING RESET
Fragment解绑
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 = inflater.inflate(R.layout.fancy_fragment, container, false); ButterKnife.bind(this, view); // TODO Use fields... return view; } @Override public void onDestroyView() { super.onDestroyView(); ButterKnife.unbind(this); } }
OPTIONAL BINDINGS 可选绑定
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, see Android Tools Project.
@Nullable @Bind(R.id.might_not_be_there) TextView mightNotBeThere; @Nullable @OnClick(R.id.maybe_missing) void onMaybeMissingClicked() { // TODO ... }
MULTI-METHOD LISTENERS
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.
@OnItemSelected(R.id.list_view) void onItemSelected(int position) { // TODO ... } @OnItemSelected(value = R.id.maybe_missing, callback = NOTHING_SELECTED) void onNothingSelected() { // TODO ... }
BONUS 额外的
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.
View view = LayoutInflater.from(context).inflate(R.layout.thing, null); 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); Add a static import for ButterKnife.findById and enjoy even more fun.
MAVEN 配置
<dependency> <groupId>com.jakewharton</groupId> <artifactId>butterknife</artifactId> <version>7.0.1</version> </dependency>
GRADLE配置
compile 'com.jakewharton:butterknife:7.0.1'
Be sure to suppress this lint warning in your build.gradle.
lintOptions {
disable 'InvalidPackage'
}
Some configurations may also require additional exclusions.
packagingOptions {
exclude 'META-INF/services/javax.annotation.processing.Processor'
}
混淆配置
-keep class butterknife.** { *; } -dontwarn butterknife.internal.** -keep class **$$ViewBinder { *; } -keepclasseswithmembernames class * { @butterknife.* <fields>; } -keepclasseswithmembernames class * { @butterknife.* <methods>; }
原文是其官方文档: http://jakewharton.github.io/butterknife/
相关文章推荐
- 《Javascript秘密花园》学习笔记(上)
- Bootstrap CSS 表单
- js菜鸟备忘
- HTML 5 Video + DOM
- 去除HTML5 SUMMARY 标签前的三角形
- R Markdown生成动态分析报告(.html)
- Effective JavaScript Item 51 在类数组对象上重用数组方法
- HTML 5 视频
- HTML 5 简介
- extjs5 store load默认Get 导致的问题
- 如何标准化特征向量HOW TO NORMALISE FEATURE VECTORS
- html总结
- javascript设置了location.href不跳转问题
- JavaScript事件
- 简单处理excel 转成 json
- CEF生成JSON数据
- C# 对象与JSON互转
- js深化学习
- jQuery.extend 函数详解
- javascript面向对象编程笔记