配置Butterknife
一.在主Moduel中的build.gradle
中添加如下配置:
dependencies {
implementation 'com.jakewharton:butterknife:8.8.1'
annotationProcessor 'com.jakewharton:butterknife-compiler:8.8.1'
}
使用场景
Activity
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();
}
}
值得注意的是:ButterKnife.bind(this)必须在初始化绑定布局文件之后,否则会报错
Fragment
public class ExampleFragment extends Fragment {
//捆绑分离工具
private 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);
//绑定fragment
unbinder = ButterKnife.bind(this,view);
return view;
}
@Override
public void onDestroyView() {
super.onDestroyView();
unbinder.unbind();//视图销毁时解绑
}
}
值得注意的是:在Fragment中需要在视图销毁时解绑Butterknife,否则会造成内存泄漏.
Adapter-ViewHolder
@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.date)
TextView date;
public MyViewHolder(View itemView) {
super(itemView);
ButterKnife.bind(this,itemView);//此处进行绑定
}
具体使用
View
单个控件
@BindView()
@BindView(R.id.example)
Button example;
多个控件
@BindViews({})
@BindViews({R.id.butter,R.id.butter1,R.id.butter2})
List<Button> buttons;
资源文件
字符串
@BindString()
@BindString(R.string.app_name) //绑定资源文件中string字符串
String name;
数组
@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 ;
颜色
@BindColor()
@BindColor( R.color.colorPrimary) //绑定color文件中颜色值
int colorPrimary;
Bitmap
@BindBitmap()
@BindBitmap(R.mipmap.ic_launcher)
Bitmap bitmap;
其他
@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
监听事件
点击事件
@OnClick( )
单个控件
@OnClick(R.id.butter)
public void onClick(){
Toast.makeText(this, "绑定单个view事件", Toast.LENGTH_SHORT).show();
}
多控件同事件
@OnClick({R.id.share_wechat,R.id.share_moments,R.id.share_weibo})
public void onClick(){
Toast.makeText(this, "都执行此事件", Toast.LENGTH_SHORT).show();
}
多控件多事件
@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;
}
}
长按事件
@OnLongClick( )
@OnLongClick( R.id.button1 ) //给 button1 设置一个长按事件
public boolean onLongClick (){
Toast.makeText(this, "is a long click", Toast.LENGTH_SHORT).show();
return true ;
}
其他事件
//选中,选中取消
@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;
}
Action接口与Setter接口
Action接口主要是为了对View或者Views进行管理初始化等操作,而Setter接口其实就是对view或者views的属性或者值进行操作.使用 ButterKnife.apply()方法启用接口.
ButterKnife.Action<View> action = new ButterKnife.Action<View>() {
@Override
public void apply(@NonNull View view, int index) {
if (view instanceof Button ){
Button button = (Button) view;
button.setText("点击我");
}
}
};
ButterKnife.Setter<View,Boolean> setter = new ButterKnife.Setter<View, Boolean>() {
@Override
public void set(@NonNull View view, Boolean value, int index) {
view.setEnabled(value);
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
ButterKnife.apply(buttons,action);//初始化或修改每个view的属性值
ButterKnife.apply(buttons,setter,true);
}