简介
Butterknife中文又名黄油刀,是 J神JakeWharton开源的一款Android视图的字段和方法绑定快速注解框架.也是Android开发中比较常用的一款快速注解框架了,避免了不断的重复findViewById,在各种场合下快速绑定view的多种事件,大大提高了开发的效率.
优点:
1、强大的View绑定和Click事件处理功能,简化代码,提升开发效率
2、方便的处理Adapter里的ViewHolder绑定问题
3、运行时不会影响APP效率,使用配置方便
4、代码清晰,可读性强
…
配置使用
在项目的app目录下的build.gradle中的dependencies中添加如下代码依赖
dependencies {
implementation 'com.jakewharton:butterknife:8.8.1'
annotationProcessor 'com.jakewharton:butterknife-compiler:8.8.1'
}
Warning(很重要)
注:
1.
使用ButterKnife修饰的方法和控件,不能用private or static 修饰,否则会报错。
2.
ButterKnife.bind(this)必须在初始化绑定布局文件之后,否则会报错
3.
在Fragment中需要在视图销毁时解绑Butterknife,否则会造成内存泄漏.
简单用法实例
在Activity中绑定ButterKnife
public class MainActivity extends AppCompatActivity {
@BindView(R.id.butter)
Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
}
@OnClick(R.id.butter)
public void onClick(){
Log.e("111111","11111");
Toast.makeText(this, "绑定单个view事件", Toast.LENGTH_SHORT).show();
}
}
在Fragment中绑定ButterKnife
public class ExampleFragment extends Fragment {
private Unbinder unbinder;//先声明一个Unbinder对象
@BindView(R.id.example)
Button example;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = View.inflate(getContext(),R.layout.fragment_example,null);
unbinder = ButterKnife.bind(this,view);
return view;
}
@Override
public void onDestroyView() {
super.onDestroyView();
unbinder.unbind();//视图销毁时必须解绑
}
}
在Adapter的ViewHolder中绑定Butterknife
@NonNull
@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.recy_dynamic_state_item,parent,false);
MyViewHolder myViewHolder = new MyViewHolder(itemView);//此处将view传入
itemView.setOnClickListener(this);
return myViewHolder;
}
public class MyViewHolder extends RecyclerView.ViewHolder {
@BindView(R.id.iv_photo)
SimpleDraweeView simpleDraweeView;
@BindView(R.id.tv_title)
TextView tvTitle;
@BindView(R.id.tv_detail)
TextView tvDetail;
@BindView(R.id.date)
TextView date;
@BindView(R.id.avatar_user)
SimpleDraweeView avatarUser;
@BindView(R.id.username)
TextView userName;
public MyViewHolder(View itemView) {
super(itemView);
ButterKnife.bind(this,itemView);//此处进行绑定
}
}
具体用例
绑定View:
1.单个控件id注解: @BindView()
@BindView(R.id.example)
Button example;
2.多个控件id注解: @BindViews()
@BindViews({R.id.butter,R.id.butter1,R.id.butter2})
List<Button> buttons;
绑定资源文件
1.绑定string 字符串:@BindString()
@BindString(R.string.app_name) //绑定资源文件中string字符串
String name;
2.绑定array数组:@BindArray()
<resources>
<string name="app_name">开眼视频</string>
<string-array name="weather">
<item>高温</item>
<item>低温</item>
<item>阴天</item>
<item>雨天</item>
<item>晴天</item>
</string-array>
</resources>
@BindArray(R.array.weather) //绑定string资源里面array数组
String [] weathers ;
3.绑定颜色值:@BindColor()
@BindColor( R.color.colorPrimary) //绑定color文件中颜色值
int colorPrimary;
4.绑定Bitmap:@BindBitmap()
@BindBitmap(R.mipmap.ic_launcher)
Bitmap bitmap;
5.其他资源绑定:
@BindBool(R.bool.is_tablet) boolean isTablet; //绑定boolean
@BindFont(R.font.comic_sans) Typeface comicSans; //绑定字体文字
@BindDimen(R.dimen.horizontal_gap) int gapPx //绑定尺寸
@BindDimen(R.dimen.horizontal_gap) float gap; //绑定尺寸
@BindAnim(R.anim.fade_in) Animation fadeIn; //绑定动画
@BindDrawable(R.drawable.placeholder) Drawable placeholder;//绑定Drawable
绑定监听事件
1.单个控件点击事件
@OnClick(R.id.butter)
public void onClick(){
Toast.makeText(this, "绑定单个view事件", Toast.LENGTH_SHORT).show();
}
2.多个控件同一点击事件
@OnClick({R.id.share_wechat,R.id.share_moments,R.id.share_weibo})
public void onClick(){
Toast.makeText(this, "多个控件同一点击事件", Toast.LENGTH_SHORT).show();
}
3.多个控件点击事件
@OnClick({R.id.share_wechat,R.id.share_moments,R.id.share_weibo})
public void onClick(View v) {
switch (v.getId()){
case R.id.share_wechat:
sharePlatform(Wechat.NAME);
break;
case R.id.share_moments:
sharePlatform(WechatMoments.NAME);
break;
case R.id.share_weibo:
sharePlatform(SinaWeibo.NAME);
break;
}
}
4.绑定控件长按事件:@OnLongClick( )
@OnLongClick( R.id.button1 ) //给 button1 设置一个长按事件
public boolean onLongClick (){
Toast.makeText(this, "is a long click", Toast.LENGTH_SHORT).show();
return true ;
}
5.其他事件绑定
//选中,选中取消
@OnCheckedChanged(R.id.example)
public void onChecked(boolean checked) {
Toast.makeText(this, checked ? "Checked!" : "Unchecked!", Toast.LENGTH_SHORT).show();
}
//软键盘的功能按键
@OnEditorAction(R.id.example)
public boolean onEditorAction(KeyEvent key) {
Toast.makeText(this, "Pressed: " + key, Toast.LENGTH_SHORT).show();
return true;
}
//焦点改变
@OnFocusChange(R.id.example)
public void onFocusChanged(boolean focused) {
Toast.makeText(this, focused ? "Gained focus" : "Lost focus", Toast.LENGTH_SHORT).show();
}
//Item长按,返回true则可以拦截onItemClick
@OnItemLongClick(R.id.example_list)
public boolean onItemLongClick(int position) {
Toast.makeText(this, "Long clicked position " + position + "!", Toast.LENGTH_SHORT).show();
return true;
}
//Item点击事件
@OnItemClick(R.id.example_list)
public void onItemClick(int position) {
Toast.makeText(this, "Clicked position " + position + "!", Toast.LENGTH_SHORT).show();
}
//Item被选择事件
@OnItemSelected(R.id.example_list)
public void onItemSelected(int position) {
Toast.makeText(this, "Selected position " + position + "!", Toast.LENGTH_SHORT).show();
}
//EditText里面的文本变化事件
@OnTextChanged(R.id.example)
public void onTextChanged(CharSequence text) {
Toast.makeText(this, "Text changed: " + text, Toast.LENGTH_SHORT).show();
}
//页面改变事件
@OnPageChange(R.id.example_pager)
public void onPageSelected(int position) {
Toast.makeText(this, "Selected " + position + "!", Toast.LENGTH_SHORT).show();
}
//触摸事件
@OnTouch(R.id.example)
public boolean onTouch() {
Toast.makeText(this, "Touched!", Toast.LENGTH_SHORT).show();
return false;
}