Android ButterKnife框架学习
2016-07-05 17:33
621 查看
基本介绍
使用@BindView注解,并传入控件Id,ButterKnife就可以自动的找到布局中相对应的控件并且绑定到类成员上。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... } }
需要在setContentView调用后调用ButterKnife.bind(this)。
相对于缓慢的反射而言,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); }
资源绑定
使用@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 // ... }
非Activity绑定
只要传进去你的根布局,你就可以在任意的对象中进行绑定例如,在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; } }
另一个常见的用法是在ListView的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 { @BindView(R.id.title) TextView name; @BindView(R.id.job_title) TextView jobTitle; public ViewHolder(View view) { ButterKnife.bind(this, view); } } }
View列表
你可以把多个View放到列表中@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); } };
监听器绑定
监听器可以自动的配置到方法上@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(); } }
自定义View可以绑定他们的监听器不需要通过指定id
public class FancyButton extends Button { @OnClick public void onClick() { // TODO do something! } }
重置绑定
Fragment与Activity生命周期不一样,我们在onCreateView中绑定Fragment,在onDestroyView中将view设为null。当你调用bind的时候,ButterKnife返回了一个Unbinder实例。你需要在适当的生命周期中调用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(); } }
多个方法的监听器
当一个监听器包含多个回调函数时,使用方法注入能够对其中任何一个函数进行绑定。每一个注解都会绑定到一个默认的回调。当然,你也可以指定callback参数:@OnItemSelected(R.id.list_view) void onItemSelected(int position) { // TODO ... } @OnItemSelected(value = R.id.maybe_missing, callback = NOTHING_SELECTED) void onNothingSelected() { // TODO ... }
findById
ButterKnife也提供了一个findById的方法来简化findViewById在view,activity,dialog中的调用。他是用泛型对返回类型进行判断并进行强转。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);
如果你只是用这个方法就静态引入ButterKnife.findById就可以了。
使用
在你的(project)的build.gradle中添加buildscript { repositories { mavenCentral() } dependencies { classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8' } }
在你的(module)的build.gradle中添加
apply plugin: 'android-apt' android { ... } dependencies { compile 'com.jakewharton:butterknife:8.1.0' apt 'com.jakewharton:butterknife-compiler:8.1.0' }
确保apply这一行放在了你的文件的顶部。
更详细官方的介绍请看官方使用教程
ButterKnife在代码和例子都可以在git上看到
我个人的git地址
相关文章推荐
- TabLayout的自定义实现
- 让Android服务变为前台服务
- android动态测量控件
- Android画图
- android 显示Gift图片
- Tabhost的用法
- android用wifi进行adb调试
- android动画的实现
- Android高清巨图加载方案
- Android事件分发机制
- Android使用Font Awesome显示小图标(二)
- 五步搞定Android开发环境部署——非常详细的Android开发环境搭建教程
- Android Studio --自动删除没有用的资源,给APK减减肥
- 【Android 应用开发】Android 开发环境下载地址
- Android Studio你不知道的调试技巧
- android 屏幕适配攻略(1) 核心概念与单位详解
- Android之图片的颜色处理
- Android apk瘦身
- [Android Studio 权威教程]断点调试和高级调试
- Android 不同应用通过SharedPreference实现共享数据