[Android 知识点] ButterKnife的使用

来源:互联网 发布:讯龙恢复软件下载 编辑:程序博客网 时间:2024/06/07 00:09

依赖

compile 'com.jakewharton:butterknife:8.4.0'annotationProcessor 'com.jakewharton:butterknife-compiler:8.4.0'

作用

绑定控件

使用@BindView和黄色刀具的视图ID注释字段,以在布局中查找并自动投射相应的视图。

class ExampleActivity 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...  }}

绑定非活动控件

public class FancyFragment extends Fragment {  @BindView(R.id.button1) Button button1;  @BindView(R.id.button2) Button button2;  @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {    View view = inflater.inflate(R.layout.fancy_fragment, container, false);    ButterKnife.bind(this, view);    // TODO Use fields...    return view;  }

VH模式

 @Override public View getView(int position, View view, ViewGroup parent) {    ViewHolder holder;    if (view != null) {      holder = (ViewHolder) view.getTag();    } else {      view = inflater.inflate(R.layout.whatever, parent, false);      holder = new ViewHolder(view);      view.setTag(holder);    }    holder.name.setText("John Doe");    // etc...    return view;  }  static class ViewHolder {    @BindView(R.id.title) TextView name;    @BindView(R.id.job_title) TextView jobTitle;    public ViewHolder(View view) {      ButterKnife.bind(this, view);    }  }}

onFinishInflate()

自定义view的回调函数

List多个绑定

@BindViews({ R.id.first_name, R.id.middle_name, R.id.last_name })List<EditText> nameViews;

apply方法允许您一次对列表中的所有视图执行操作

ButterKnife.apply(nameViews, DISABLE);ButterKnife.apply(nameViews, ENABLED, false);
static final ButterKnife.Action<View> DISABLE = new ButterKnife.Action<View>() {  @Override public void apply(View view, int index) {    view.setEnabled(false);  }};static final ButterKnife.Setter<View, Boolean> ENABLED = new ButterKnife.Setter<View, Boolean>() {  @Override public void set(View view, Boolean value, int index) {    view.setEnabled(value);  }};

Android属性也可以与apply方法一起使用

ButterKnife.apply(nameViews, View.ALPHA, 0.0f);

事件监听

形参是可选因素

@OnClick(R.id.submit)public void submit(View view) {  // TODO submit data to server...}@OnClick(R.id.submit)public void submit() {  // TODO submit data to server...}@OnClick(R.id.submit)public void sayHi(Button button) {  button.setText("Hello!");}@OnClick({ R.id.door1, R.id.door2, R.id.door3 })public void pickDoor(DoorView door) {  if (door.hasPrizeBehind()) {    Toast.makeText(this, "You win!", LENGTH_SHORT).show();  } else {    Toast.makeText(this, "Try again", LENGTH_SHORT).show();  }}//自定义view不用idpublic class FancyButton extends Button {  @OnClick  public void onClick() {    // TODO do something!  }}

Fragment中的绑定

public class FancyFragment extends Fragment {  @BindView(R.id.button1) Button button1;  @BindView(R.id.button2) Button button2;  private Unbinder unbinder;  @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {    unbinder = ButterKnife.bind(this, view);    // TODO Use fields...  }  @Override public void onDestroyView() {    super.onDestroyView();    unbinder.unbind();  }}

异常处理@Nullable 和 @Optional

当bind控件找不到会抛出异常,可以用@Nullable 和 @Optional 解决

@Nullable @BindView(R.id.might_not_be_there) TextView mightNotBeThere;@Optional @OnClick(R.id.maybe_missing) void onMaybeMissingClicked() {  // TODO ...}

View, Activity, or Dialog绑定

View view = LayoutInflater.from(context).inflate(R.layout.thing, null);TextView firstName = ButterKnife.findById(view, R.id.first_name);TextView lastName = ButterKnife.findById(view, R.id.last_name);ImageView photo = ButterKnife.findById(view, R.id.photo);

资源绑定

  • @BindBool
  • @BindColor
  • @BindDimen
  • @BindDrawable
  • @BindInt
  • @BindString,
0 0
原创粉丝点击