Android之ButterKnife用法详解

    xiaoxiao2021-03-25  136

    转自:http://blog.csdn.net/leavessilent/article/details/60872096

    相信很多开发Android的小伙伴,都厌倦了findViewById(),都是基本重复的操作,所以我们可以使用依赖注入框架来偷懒。目前,用的较多的两种大概是ButterKnife和dagger,英文译为黄油刀和匕首,听名字就很酷。

    今天我们就来详细介绍ButterKnife的用法

    ButterKnife是Square公司的Android之神JakeWharton开源的一款依赖注入框架,一把好用的黄油刀.

    GitHub地址

    ## 添加依赖 * 首先在Module的build.gradle中 dependencies { compile 'com.jakewharton:butterknife:8.5.1' annotationProcessor 'com.jakewharton:butterknife-compiler:8.5.1' } 1234 1234

    在build.gradle第一行加上

    apply plugin: 'com.android.application' apply plugin: 'com.jakewharton.butterknife' 123 123 在工程的build.gradle中 dependencies { classpath 'com.jakewharton:butterknife-gradle-plugin:8.5.1' } 123 123

    在Activiy中使用

    @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.simple_activity); mUnbinder = ButterKnife.bind(this); // TODO Use fields... } @Override protected void onDestroy() { super.onDestroy(); mUnbinder.unbind(); } 123456789101112 123456789101112

    首先在onCreate方法中调用`ButterKnife.bind(this)` 会得到一个 `Unbinder` 对象,然后在 `onDestroy` 方法中调用`unbind()`方法解除绑定。

    * 绑定View

    @BindView(R.id.tv_title) View mTitleTv; 123 123 当然它还可以一次性绑定多个view @BindViews({ R.id.image_first, R.id.image_second, R.id.image_third}) List<ImageView> mImageViews; 12 12 ButterKnife还提供了一些简洁的操作view的方法 // apply方法会自动遍历传入的数组和集合,将所有imageviw透明度设为50% ButterKnife.apply(mImageViews, View.ALPHA, 0.5f); 1234 1234 此外我们还可以自定义对view的操作: // 代码无实际意义,只是为了演示。 ButterKnife.apply(mImageViews, new ButterKnife.Action<ImageView>() { @Override public void apply(@NonNull ImageView view, int index) { view.setVisibility(View.GONE); } }); 1234567 1234567

    在这里index是每个ImageView的下标,或者叫索引。

    绑定资源 @BindString(R.string.title) String title; @BindDrawable(R.drawable.graphic) Drawable graphic @BindColor(R.color.red) int redColor; 123456789 123456789

    记住这些成员变量前面都不能加private修饰符。

    绑定监听事件

    单个view的监听

    @OnClick(R.id.btn_back}) public void onClick(View view) { // do sth } 1234 1234

    多个View的监听

    @OnClick({R.id.tv_finish, R.id.btn_back}) public void onClick(View view) { switch (view.getId()) { case R.id.tv_finish: // do sth break; case R.id.btn_back: // do sth break; } } 1234567891011 1234567891011 其他如onFocusChange、OnItemSelected等都与onClick事件类似,读者可自行实验。 1 1

    我们可以看到,使用ButterKnife之后,我们再也不用findViewById,也不用一个一个设置view.setOnClickListener,是不是很方便。下面我们再来介绍它在fragment中的用法

    在Fragment中使用

    初始化 @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { View view= inflater.inflate(getLayoutResId(), container, false); mUnbinder = ButterKnife.bind(this, view); return view; } @Override public void onDestroyView() { super.onDestroyView(); mUnbinder.unbind(); } 12345678910111213 12345678910111213

    再fragment中,除了绑定和解绑方法有些区别不同之外,其他用法与activity中一模一样。

    在适配器中使用

    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); } } } 1234567891011121314151617181920212223242526 1234567891011121314151617181920212223242526

    自定义view中使用(不需要指定id)

    public class FancyButton extends Button { @OnClick public void onClick() { // TODO do something! } } 123456 123456

    在这里ButterKnife的所有用法都介绍完了, 它可以减少我们很多重复的findView操作,使代码变得优雅简洁。有木有感觉 ButterKnife 特别强大。

    转载请注明原文地址: https://ju.6miu.com/read-2975.html

    最新回复(0)