ButterKnife最常用的用法

本文详细介绍了ButterKnife插件的安装与配置步骤,并通过实例展示了如何在Activity、Fragment及Adapter中使用该插件进行视图绑定,提高Android开发效率。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

step1.
装ButterKnife插件

step2.
在model里的.gradle文件里
dependencies compile 'com.jakewharton:butterknife:8.4.0'
annotationProcessor 'com.jakewharton:butterknife-compiler:8.4.0'
顶部 apply plugin: 'com.jakewharton.butterknife'

step3.
在projects工程里的.gradle文件里
dependencies classpath 'com.jakewharton:butterknife-gradle-plugin:8.4.0'

step4.
在布局文件前面点击快捷键alt+insert或者shift+alt+insert

=============================================================================
在activity中:
ButterKnife.bind(this);
在fragment中:
private Unbinder unbinder;
unbinder = ButterKnife.bind(this, view);

@Override
public void onDestroyView() {
unbinder.unbind();
super.onDestroyView();
}
在adapter中:
@Override
public View getView(int position, View convertView, ViewGroup parent) {

ViewHolder holder;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.person_item_layout, null);
holder = new ViewHolder(convertView);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
return convertView;
}

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

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

@BindView(R.id.my_list_view)
ListView mListView;

void onButtonClick(View view) {}

@OnClick({R.id.button_enable, R.id.button_disable, R.id.button_alpha_0, R.id.button_alpha_1})
void editViewsClicked() {}

@OnItemClick(R.id.my_list_view)
void onItemClick(int position) {}

@OnItemLongClick(R.id.my_list_view)
boolean onItmeLongClick(int position) {retun true;}

@OnCheckedChanged(R.id.simple_activity_checkbox)
void onCheckedChanged(CompoundButton button, boolean checked) {}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值