标签:compiler container ati from tps layout field todo view
ButterKnife 即 “黄油刀”,此开源库可以让你的应用开发过程像用刀切黄油一样干净利落。此库的核心功能是通过注解来实现视图的注入,从而在代码中避免冗余的方法调用和丑陋的内部类监听器。
此开源库Github地址:https://github.com/JakeWharton/butterknife
使用前提
在项目中添加ButterKnife的依赖,即在app模块的build.gradle中添加:
dependencies { compile ‘com.jakewharton:butterknife:8.5.1‘ annotationProcessor ‘com.jakewharton:butterknife-compiler:8.5.1‘ }
使用说明
一、绑定控件
ButterKnife通过@BindView注解和视图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... } }
ButterKnife是通过自动生成代码来执行View的查找,上面的例子中,ButterKnife最终自动生成的代码大概是这样的:
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); }
注意:使用ButterKnife时,成员变量不可以是static 或 private 修饰的。
二、资源绑定
ButterKnife预定义了注解@BindBool, @BindColor, @BindDimen, @BindDrawable, @BindInt, @BindString,用来执行资源的绑定。如下所示:
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 // ... }
三、非Activity绑定
除了Activity,你也可以将任意的对象与View绑定。
例如在Fragment中:
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; } }
或在适配器中:
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); } } }
任何原来需要使用findViewById的地方都可以用ButterKnife.bind替代。
四、绑定视图列表
可以将多个视图放在List或数组中
@BindViews({ R.id.first_name, R.id.middle_name, R.id.last_name })
List<EditText> nameViews;
apply方法让你可以同时操作列表中的所有视图:
ButterKnife.apply(nameViews, DISABLE); ButterKnife.apply(nameViews, ENABLED, false);
其中,Action和Setter接口可以用来指定简单的操作:
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); } };
apply方法还能用来设置View的属性:
ButterKnife.apply(nameViews, View.ALPHA, 0.0f);
五、监听接口的绑定
监听器可以自动在方法上配置,如下:
@OnClick(R.id.submit) public void submit(View view) { // TODO submit data to server... }
监听器方法的参数是可选的:
@OnClick(R.id.submit) public void submit() { // TODO submit data to server... }
监听器方法的参数可以是具体的类型,它会被自动转型:
@OnClick(R.id.submit) public void sayHi(Button button) { button.setText("Hello!"); }
也可以在单个绑定中指定多个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(); } }
自定义视图自己的监听器可以省去ID:
public class FancyButton extends Button { @OnClick public void onClick() { // TODO do something! } }
六、绑定的重置
Fragment生命周期和Activity不同,当在onCreateView中绑定视图后,要在onDestroyView中将这些视图置为null,ButterKnife提供了Unbinder接口来自动做这件事,如下:
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(); } }
七、其它
对于无法直接绑定视图的时候,ButterKnife提供了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);
标签:compiler container ati from tps layout field todo view
原文地址:http://www.cnblogs.com/mengyi/p/6669469.html