转载http://blog.csdn.net/qiaoshi96_bk/article/details/77371035
需要的依赖
compile 'com.jakewharton:butterknife:8.5.1'
annotationProcessor 'com.jakewharton:butterknife-compiler:8.5.1'
- 1
- 2
- 1
- 2
官网地址 :http://jakewharton.github.io/butterknife/
ButterKnife的用法很简单只需要用@bindview 来标注视图ID字段,它便会在视图上自动帮你找到控件或布局的id。
如:
class ExampleActivity extends Activity {
@BindView(R.id.title) TextView title;
@BindView(R.id.subtitle) TextView subtitle;
@BindView(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...
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
相比较缓慢的反射,代码会被生成来执行视图查询,调用绑定委托给您可以看到和调试的生成的代码
上面的示例生成的代码大致相当于下面的代码:
public void bind(ExampleActivity activity) {
activity.subtitle = (android.widget.TextView) activity.findViewById(2130968578);
activity.footer = (android.widget.TextView) activity.findViewById(2130968579);
activity.title = (android.widget.TextView) activity.findViewById(2130968577);
}
- 1
- 2
- 3
- 4
- 5
- 1
- 2
- 3
- 4
- 5
资源绑定
绑定预定义的资源与@BindBool, @BindColor, @BindDimen, @BindDrawable, @BindInt, 、@bindstring,通过他们对于的id或您指定的类型到对应的字段。
class ExampleActivity extends Activity {
@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
// ...
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 1
- 2
- 3
- 4
- 5
- 6
- 7
Non-Activity绑定
您还可以通过提供自己的视图来对任意对象执行绑定。
public class FancyFragment extends Fragment {
@BindView(R.id.button1) Button button1;
@BindView(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;
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
还可以用来简化列表适配器内的视图holder模式。
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 {
@BindView(R.id.title) TextView name;
@BindView(R.id.job_title) TextView jobTitle;
public ViewHolder(View view) {
ButterKnife.bind(this, view);
}
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
您可以在提供的示例中看到这个实现:调用ButterKnife。绑定可以在任何地方进行,只要通过findViewById可以实现的。
其他绑定api提供:
- 使用Activity作为视图根绑定任意对象。如果使用像MVC这样的模式,您可以使用它的Activity与ButterKnife绑定控制器。绑定(this,Activity)。
使用ButterKnife.bind(this)将视图的子布局绑定到字段中,你可以设置视图何时绑定何时取消。
视图集合
您可以将多个视图分组到一个列表或数组中。
@BindViews({ R.id.first_name, R.id.middle_name, R.id.last_name })
List<EditText> nameViews;
//这个方法允许你立即执行集合里所有布局
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);
}
};
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
一个Android属性也可以用于应用的方法。
ButterKnife.apply(nameViews, View.ALPHA, 0.0f);
- 1
- 1
监听绑定
监听绑定会自动配置到方法中
@OnClick(R.id.submit)
public void submit(View view) {
// TODO submit data to server...
}
- 1
- 2
- 3
- 4
- 1
- 2
- 3
- 4
对监听的所有方法都是可选的
@OnClick(R.id.submit)
public void submit() {
// TODO submit data to server...
}
- 1
- 2
- 3
- 4
- 1
- 2
- 3
- 4
定义一个特定类型,它也可以自动被转换
@OnClick(R.id.submit)
public void sayHi(Button button) {
button.setText("Hello!");
}
- 1
- 2
- 3
- 4
- 1
- 2
- 3
- 4
在单个绑定中可以指定多个id,用于公共事件处理。
@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();
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
自定义视图可以通过不指定ID来绑定到它们自己的监听方法。
public class FancyButton extends Button {
@OnClick
public void onClick() {
// TODO do something!
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 1
- 2
- 3
- 4
- 5
- 6
绑定重置
Fragment的生命周期不同于Activity。在onCreateView中绑定一个Fragment时,在onDestory视图中将视图销毁。 Butter Knifer会返回一个取消绑定的实例,当你调用bind来为你做这个。在适当的方法中调用它的unbind方法
public class FancyFragment extends Fragment {
@BindView(R.id.button1) Button button1;
@BindView(R.id.button2) Button button2;
private Unbinder unbinder;
@Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fancy_fragment, container, false);
unbinder = ButterKnife.bind(this, view);
// TODO Use fields...
return view;
}
@Override public void onDestroyView() {
super.onDestroyView();
unbinder.unbind();
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
可选的绑定
默认情况下,需要使用@bind和监听来绑定。如果无法找到目标视图,将抛出一个异常。如果想要解决此问题可以创建可选绑定,可以将@null注释添加到字段中,或将@任选注释添加到方法
注意:任何被命名为@null的注释都可以用于字段。建议使用android的“ Android’s “support-annotations” library”库中的@null注释。
@Nullable @BindView(R.id.might_not_be_there) TextView mightNotBeThere;
@Optional @OnClick(R.id.maybe_missing) void onMaybeMissingClicked() {
// TODO ...
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 1
- 2
- 3
- 4
- 5
- 6
- 7
多种监听器
方法注释,其对应的监听有多个回调,可用于绑定到其中任何一个。每个注释都有一个它绑定到的默认回调。使用回调参数指定一个备用。
@OnItemSelected(R.id.list_view)
void onItemSelected(int position) {
// TODO ...
}
@OnItemSelected(value = R.id.maybe_missing, callback = NOTHING_SELECTED)
void onNothingSelected() {
// TODO ...
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
其他
当然他还包括了findById方法,它简化了仍然需要在视图、活动或对话框中找到视图的代码。它使用泛型来推断返回类型,并自动执行转换
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);
- 1
- 2
- 3
- 4
- 1
- 2
- 3
- 4
ButterKnife添加一个静态导入。使findById,更简单实用。
(function () {('pre.prettyprint code').each(function () {
var lines = (this).text().split(′\n′).length;var numbering = $('
(this).addClass(′has−numbering′).parent().append( numbering);
for (i = 1; i <= lines; i++) {
numbering.append( ('
- ').text(i));
};
$numbering.fadeIn(1700);
});
});