ButterKnife 是一個快速 Android View 注入框架,開發者是Jake Wharton,簡單的來說,ButterKnife 是用注解的方式替代findViewById和setXXXListener
項目GitHub地址:https://github.com/JakeWharton/butterknife/
Android Studio 配置步驟可以直接參考Github上的介紹,很簡單。
ButterKnife 是在編譯時注解,不會在運行時產生負擔,Build工程后你會發現它生成了需要的代碼,即它不是使用反射或者在運行時生成代碼,所以它不會導致任何性能問題,也不會影響應用速度。
看過下面這句話,使用之后發現挺有道理。
ButterKnife 不能算是一個真正的注入,更像是View的代言。
1. 替代findViewById()
在變量聲明的時候加上注解,如果找不到id,會在編譯時報錯,注意View變量聲明的時候不能為private或者static。
@BindView(R.id.hello_world_tv)
TextView helloWorldTv;
@BindView(R.id.a_hello_world_btn)
Button aHelloWorldBtn;
在setContentView(view)后設置bind()。
setContentView(R.layout.activity_main); // ButterKnife.inject(this) should be called after setContentView() ButterKnife.bind(this);
ButterKnife 同樣可以在Fragment、Adapter中使用,注意Fragment中要在onDestroyView()中調用unbind()。
public class SimpleFragment extends Fragment { private Unbinder unbinder; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment_simple, container, false); unbinder = ButterKnife.bind(this, view); return view; } @Override public void onDestroyView() { unbinder.unbind(); super.onDestroyView(); } }
2. 設置監聽
ButterKnife 支持設置多種Listener,注意這里的方法仍然不能是private和static
@OnClick
public void onClick(View view) { // 處理onClick事件 }
@OnLongClick
public boolean onLongClick(View view){ // return true; // Click事件被onLongClick處理掉 // return false; // Click事件不被onLongClick處理,后續仍會觸發onClick事件 }
@OnPageChange
OnPageChange.Callback
@OnTextChanged
OnTextChanged.Callback
@OnTouch
@OnTouch({R.id.hello_world_tv}) public boolean onTouch(View view, MotionEvent event){ if (view.getId() == R.id.hello_world_tv) { helloWorldTv.setText("Wa ha ha ha ha..."); } return true; }
@OnItemClick
@OnItemClick(R.id.my_list_view) // public void onItemClick(AdapterView<?> parent, View view, int position, long id) void onItemClick(int position) {//though there are 4 parameters, you can just write the one you want to use Toast.makeText(this, "You clicked: " + adapter.getItem(position).getName(), Toast.LENGTH_SHORT).show(); }
@OnItemLongClick
@OnItemLongClick(R.id.my_list_view) boolean onItemLongClick(int position) { Toast.makeText(this, "You Long Click: " + adapter.getItem(position).getName(), Toast.LENGTH_SHORT).show(); return true; //if return false, the onItemClick() will be invoked when touch up }
@OnCheckedChanged
public void OnCheckedChanged(CompoundButton buttonView, boolean isChecked) { switch (buttonView.getId()) { case R.id.a_simple_checkbox: Toast.makeText(this, "a_isChecked = " + isChecked, Toast.LENGTH_SHORT).show(); break; case R.id.b_simple_checkbox: Toast.makeText(this, "b_isChecked = " + isChecked, Toast.LENGTH_SHORT).show(); break; case R.id.c_simple_checkbox: Toast.makeText(this, "c_isChecked = " + isChecked, Toast.LENGTH_SHORT).show(); break; default: break; } }
其他方法的使用有待繼續學習。
3. 綁定資源
注意變量聲明同樣不能為private或者static。
@BindString(R.string.app_name) String appName;//sting @BindColor(R.color.red) int textColor;//顏色 @BindDrawable(R.mipmap.ic_launcher) Drawable drawable;//drawble @BindDrawable(R.drawable.selector_image) Drawable selector;
4. 多個View進行統一操作
未使用,待學習。
參考:
http://blog.csdn.net/itjianghuxiaoxiong/article/details/50177549
http://blog.csdn.net/jdsjlzx/article/details/51281844
待學習:
1. 常用Listener的使用