Butter Knife 使用说明

来源:互联网 发布:图片转换cad软件 编辑:程序博客网 时间:2024/06/05 17:21

Annotate fields with @Bind and a view ID for Butter Knife to find and automatically cast the corresponding view in your layout.
Butter Knife 使用 @Bind 的注释字段和 view 的 ID 自动映射到你的layout中的 view。

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…
}
}
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.

代替了映射 ,代码生成了 view 查询表。使用 bind 代替了 这下面段你能看到和调试的代码。

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(2130968579);
activity.title = (android.widget.TextView) activity.findViewById(2130968577);
}
RESOURCE BINDING

Bind pre-defined resources with @BindBool, @BindColor, @BindDimen, @BindDrawable, @BindInt, @BindString, which binds an R.bool ID (or your specified type) to its corresponding field.

Bind 预定义 资源@BindBool, @BindColor, @BindDimen, @BindDrawable, @BindInt, @BindString, 绑定了 R.bool 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
// …
}
NON-ACTIVITY BINDING

You can also perform binding on arbitrary objects by supplying your own view root.

你也可以绑定你自定view。

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;
}
}
Another use is simplifying the view holder pattern inside of a list adapter.
另一种 用法是可以简化列表 adapter中的 view 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 {
@Bind(R.id.title) TextView name;
@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 to ButterKnife.bind can be made anywhere you would otherwise put findViewById calls.

Other provided binding APIs:

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).
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.
VIEW LISTS

You can group multiple views into a List or array.

@Bind({ R.id.first_name, R.id.middle_name, R.id.last_name })
List 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 DISABLE = new ButterKnife.Action() {
@Override public void apply(View view, int index) {
view.setEnabled(false);
}
};
static final ButterKnife.Setter

0 0
原创粉丝点击