Android ButterKnife 使用方法总结

ButterKnife Introduction(介绍)


ButterKnife 是一个专注于Android系统的View注入框架,可视化一键生成。

英:

  1. Eliminate findViewById calls by using @BindView on fields.
  2. Group multiple views in a list or array. Operate on all of them at once with actions, setters, or properties.
  3. Eliminate anonymous inner-classes for listeners by annotating methods with @OnClick and others.
  4. Eliminate resource lookups by using resource annotations on fields.

译:

  1. 使用@BindView 来代替findViewById 完成View的引用。
  2. 将多个View组合成list或者array,使用actions,setters或者属性来同时操作它们。
  3. 使用@OnClick等注解字段来注解方法,从而来代替监听器中的匿名内部类。
  4. 使用@BindString等注解字段来注解字段,从而来代替Context.getString等获取资源的方式。

How to build (如何够贱):


Project build.gradle 添加:

dependencies {
    classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'
    classpath 'com.jakewharton:butterknife-gradle-plugin:8.4.0'
}

分支 module build.gradle 添加:(如果只有主项目可忽略)

apply plugin: 'com.jakewharton.butterknife'

dependencies {
    annotationProcessor "com.jakewharton:butterknife-compiler:8.4.0"
}

分支 lib_common build.gradle 添加:

apply plugin: 'com.jakewharton.butterknife'

dependencies {
    compile "com.jakewharton:butterknife:8.4.0"
    annotationProcessor "com.jakewharton:butterknife-compiler:8.4.0"
}

How to use it (如何使用):


activity使用:

import butterknife.ButterKnife;
import butterknife.Unbinder;

public abstract class BaseActivity extends AppCompatActivity {

    // Unbinder
    private Unbinder unbind;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // setView
        setContentView(R.layout.main);
        // ButterKnife
        unbind = ButterKnife.bind(this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unbind.unbind();
    }
}

Fragment使用:

import butterknife.ButterKnife;
import butterknife.Unbinder;

public abstract class BaseFragment extends Fragment {

    protected View rootView;
    private Unbinder unbind;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        if (rootView == null) {
            rootView = inflater.inflate(this.getLayoutResource(), container, false);
        }
        unbind = ButterKnife.bind(this, rootView);

        return rootView;
    }

    //获取布局文件
    protected abstract int getLayoutResource();

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbind.unbind();
    }

}

Adaper使用:

import butterknife.ButterKnife;

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.item, parent, false);
            holder = new ViewHolder(view);
            view.setTag(holder);
        }

        holder.name.setText("hello");
        return view;
    }

    static class ViewHolder {
        @BindView(R.id.name)
        TextView name;

        public ViewHolder(View view) {
            ButterKnife.bind(view);
        }
    }
}

Butterknife常用的注解:

Butterknife支持Activity,Fragment,View,Dialog,ViewHolder类内部的View绑定

// 绑定View,避免findViewById,也可以用在ViewHolder里,必须是public
@BindView(R2.id.textview)  
TextView mTextView; 

// 绑定多个view,只能用List不能用ArrayList
@BindViews({ R2.id.button1, R2.id.button2,  R2.id.button3})  
List<Button> buttonList; 

// 绑定点击事件
@OnClick(R2.id.submit)
public void submit(View view) {...}

// 多个id绑定点击事件(8.4.0不支持此写法)
// 在 view.getId()方法中,系统返回的值是R类中的id而不是R2中的id,而且两个R类中相同变量的id并不相同,所以会翻车。
@OnClick({R.id.example_btn1, R.id.example_btn2, R.id.example_btn3})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.example_btn1:
                break;
            case R.id.example_btn2:
                break;
            case R.id.example_btn3:
                break;
        }
     }

//(8.4.0需要单个绑定)
@OnClick(R2.id.txt_bind_we_chat_btn)
public void onBindWeChatClick(View v) {
    bindWeChat();
}

@OnClick(R2.id.txt_bind_phone_btn)
  public void onBIndPhoneClick(View v) {
    bindPhoe();
}

@OnClick(R2.id.back)
public void onBackClick() {
    finish();
}

// 绑定长按事件 
@OnLongClick( R2.id.button1 )  
public boolean showToast(){   
    return true ;  
}

// itemSelected监听
@OnItemSelected(R2.id.list_view)
void onItemSelected(int position) {...}

// itemClick监听
@OnItemClick(R2.id.list_view) 
void onItemClick(int position) {...}

// 焦点改变监听
@OnFocusChange(R2.id.list_view) 
void onFocusChanged(boolean focused){...}

// itemLongClick长按监听
@OnItemLongClick(R2.id.list_view) 
boolean onItemLongClick(int position){...}

// viewpager切换监听
@OnPageChange(R2.id.example_pager) 
void onPageSelected(int position){...}

// et内容改变监听
@OnTextChanged(R2.id.example_et) 
void onTextChanged(CharSequence text)

// 绑定string.xml里的字符串
@BindString(R2.string.example_name)   
String exampleName;

// 绑定string里面array数组 
@BindArray(R2.array.city)  
String [] citys;  

// 绑定色值在color文件中 
@BindColor( R2.color.black) 
int black;

// 绑定图片资源
@BindBitmap( R.mipmap.wifi) 
Bitmap bitmap;

// 绑定dimens
@BindDimen(R.dimen.example_width) 
int exampleWidth;

// 绑定图片
@BindDrawable(R.drawable.example_pic)
Drawable examplePic;

// 绑定Integer类型的resource ID
@BindInt

// 绑定boolean值
@BindBool

ButterKnife Zelezny插件(自动生成代码)


How to install (如何安装)

  • in Android Studio:FileSettingsPluginsBrowse repositories→搜索Android ButterKnife ZeleznyInstallRestart Android Studio(安装后重启生效)

  • download ButterKnife:FileSettingsPluginsInstall plugin from disk(安装后重启生效)

How to use it (如何使用):

zelezny_animated.gif

 

  1. Make sure you have latest Butterknife lib on your classpath
  2. Right click on usage of desired layout reference (e.g. R.layout.main in your Activity or Fragment), then Generate and Generate ButterKnife Injections
  3. Pick injections you want, you also have an option to create ViewHolder for adapters.
  4. Click Confirm and enjoy injections in your code with no work!

ButterKnife 代码混淆:


-keep class butterknife.** { *; }  
-dontwarn butterknife.internal.**  
-keep class **$$ViewBinder { *; }  
  
-keepclasseswithmembernames class * {  
    @butterknife.* <fields>;  
}  
  
-keepclasseswithmembernames class * {  
    @butterknife.* <methods>;  
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值