简单明了,步骤如下:
1、框架的导入
<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">eclipse:官网下载jar包,复制到lib文件夹</span>
Android Studio: File->Project Structure->Dependencies->Library dependency 搜索butterknife即可
2、使用情景
①、Activity和View的绑定
class BaseActivity 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...
}
@Overrid public void onDestory(){
<span style="white-space:pre"> </span>super.onDestory();
<span style="white-space:pre"> </span>ButterKnife.unBind(this);<span style="white-space:pre"> </span>
}
}
②Fragment的绑定
public abstract class BaseFragment extends Fragment {
protected View mRootView;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
mRootView =inflater.inflate(getContentViewId(),container,false);
ButterKnife.bind(this,mRootView);//绑定framgent
return mRootView;
}
@Override
public void onDestroyView() {
super.onDestroyView();
ButterKnife.unbind(this);//解绑
}
}
③Adapter中的绑定
public class TestAdapter extends BaseAdapter {
private List<String> list;
private Context context;
public TestAdapter(Context context, List<String> list) {
this.list = list;
this.context = context;
}
@Override
public int getCount() {
return list==null ? 0 : list.size();
}
@Override
public Object getItem(int position) {
return list.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = LayoutInflater.from(context).inflate(R.layout.layout_list_item, null);
holder = new ViewHolder(convertView);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.textview.setText("item=====" + position);
return convertView;
}
static class ViewHolder {
@Bind(R.id.hello_world)
TextView textview;
public ViewHolder(View view) {
ButterKnife.bind(this, view);
}
}
}
④点击事件绑定
参数的指定可选
@OnClick(R.id.submit)
public void submit(View view) {
// TODO submit data to server...
}
<pre name="code" class="java" style="font-size: 18px; line-height: 26px;">@OnClick(R.id.submit)
public void submit() {
// TODO submit data to server...
}
定义一个特定类型,它将自动转换
@OnClick(R.id.submit)
public void sayHi(Button button) {
button.setText("Hello!");
}
多个view绑定一个onClick事件
@OnClick({ R.id.view1, R.id.view2, R.id.view3 })
public void click(View view) {
<span style="white-space:pre"> </span>
}
总的来说常用的功能如上,由于之前用过Xutils3.0做注解,感觉和ButterKnife差不多,具体区别以后再体会。希望有大神看到本篇博文可以评论指教一番