butterknife 官方使用文档和例子

luciano815 8年前
   <p>butterknife可以说是一个非常强大的视图绑定库,大大简化代码,并且不会因为反射而影响APP性能,所以推荐大家使用,看了一下butterknife的官方文档和例子写的很好,所以特翻译过来方便大家查阅,请多指教。</p>    <p>下面为译文:</p>    <h3>介绍</h3>    <p>使用@BindView这个注释和一个view的ID来自动的匹配到布局中的view。</p>    <pre>  <code class="language-java">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...    }  }</code></pre>    <p>代替耗时的反射,通过产生代码来实现view的查找,可以叫做委托绑定并产生可见且可调式的代码。</p>    <p>上面的例子产生的代码大致如下:</p>    <pre>  <code class="language-java">public void bind(ExampleActivity activity) {    activity.subtitle = (android.widget.TextView) activity.findViewById(2130968578);    activity.footer = (android.widget.TextView) activity.findViewById(2130968579);    activity.title = (android.widget.TextView) activity.findViewById(2130968577);  }</code></pre>    <h3>资源绑定</h3>    <p>用@BindBool, @BindColor, @BindDimen, @BindDrawable,@BindInt, @BindString</p>    <p>绑定默认的资源。绑定自定义的类型需要属性匹配。</p>    <pre>  <code class="language-java">class ExampleActivity extends Activity {    @BindString(R.string.title) String title;    @BindDrawable(R.drawable.graphic) Drawable graphic;    @BindColor(R.color.red) int red; // int or ColorStateList field    @BindDimen(R.dimen.spacer) Float spacer; // int (for pixel size) or float (for exact value) field    // …  }</code></pre>    <h3>没有活动的 绑定</h3>    <p>你也可以绑定随意的对象,但是必须提供你的view根节点</p>    <pre>  <code class="language-java">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;    }  }</code></pre>    <p>另一个用法是在adapter中简化view holder的例子</p>    <pre>  <code class="language-java">public class MyAdapter extends BaseAdapter {    @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);      }    }  }</code></pre>    <p>你可以在提供的示例中看到它的实现</p>    <h3>其他的绑定APIs</h3>    <p>如果活动是视图的根节点,如果你使用MVC的设计模式,你可以在控制器中使用ButterKnife.bind(this, activity).来完成绑定</p>    <p>用ButterKnife.bind(this)来绑定子视图,如果你在布局中使用了merge标签在一个自定义的视图里你可以立刻使用,或者从xml中填充自定义视图,可以在onFinishInflate()回调中使用。</p>    <h3>视图列表</h3>    <p>你可以吧视图放在一个数组或者列表里</p>    <pre>  <code class="language-java">@BindViews({ R.id.first_name, R.id.middle_name, R.id.last_name })  List<EditText> nameViews;</code></pre>    <p>使用这几种方法允许您立即在列表中的所有视图上执行</p>    <pre>  <code class="language-java">ButterKnife.apply(nameViews, DISABLE);  ButterKnife.apply(nameViews, ENABLED, false);</code></pre>    <p>动作和setter接口允许制定的简单行为:</p>    <pre>  <code class="language-java">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);    }  };</code></pre>    <p>一个安卓的属性方法也可以一同使用。</p>    <pre>  <code class="language-java">ButterKnife.apply(nameViews, View.ALPHA, 0.0f);</code></pre>    <h3>监听器绑定</h3>    <p>监听器也可以简单自然的绑定在方法中</p>    <pre>  <code class="language-java">@OnClick(R.id.submit)  public void submit(View view) {    // TODO submit data to server...  }</code></pre>    <p>监听器的参数是可选的</p>    <pre>  <code class="language-java">@OnClick(R.id.submit)  public void submit() {    // TODO submit data to server...  }</code></pre>    <p>也可以制定一个具体的类型</p>    <pre>  <code class="language-java">@OnClick(R.id.submit)  public void sayHi(Button button) {    button.setText("Hello!");  }</code></pre>    <p>常规的事件绑定多个id</p>    <pre>  <code class="language-java">@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();    }  }</code></pre>    <p>常规的视图可以被绑定到他们自己的监听器不需要具体的ID</p>    <pre>  <code class="language-java">public class FancyButton extends Button {    @OnClick    public void onClick() {      // TODO do something!    }  }</code></pre>    <h3>绑定重置</h3>    <p>Fragment有和activity不一样的生命周期,当绑定一个fragment的在onCreate,在销毁时(onDestroyView)</p>    <p>设置视图们变为空,Butter Knife返回一个Unbinderinstance当你调用绑定方法,并在合适的地方调用解绑方法。</p>    <pre>  <code class="language-java">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) {      View view = inflater.inflate(R.layout.fancy_fragment, container, false);      unbinder = ButterKnife.bind(this, view);      // TODO Use fields...      return view;    }      @Override public void onDestroyView() {      super.onDestroyView();      unbinder.unbind();    }  }</code></pre>    <h3>可选择的绑定</h3>    <p>默认的,普通绑定和监听器绑定都是需要的,如果找不到view可以能会抛异常。</p>    <p>要抑制此行为并创建可选绑定,添加一个@Nullable注释在字段前,或者 @Optional在方法前。</p>    <p>注意:任何注释@Nullable可以被用在字段前,Android’s “support-annotations” library也是鼓励这么使用。</p>    <pre>  <code class="language-java">@Nullable @BindView(R.id.might_not_be_there) TextView mightNotBeThere;    @Optional @OnClick(R.id.maybe_missing) void onMaybeMissingClicked() {    // TODO …        }</code></pre>    <h3>多元监听</h3>    <p>方法前的注释可以有多元化的监听回调,每个注释可以默认回调它的绑定,具体制定一个参数(int position)</p>    <pre>  <code class="language-java">@OnItemSelected(R.id.list_view)  void onItemSelected(int position) {    // TODO ...  }    @OnItemSelected(value = R.id.maybe_missing, callback = NOTHING_SELECTED)  void onNothingSelected() {    // TODO }</code></pre>    <h3>额外的优势</h3>    <p>也包含findById方法使代码简单,来找到视图活动弹窗的views。它使用泛型来判断类型并自动转换。</p>    <pre>  <code class="language-java">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);</code></pre>    <p>新增一个静态的ButterKnife.findById并且享受它们的快乐吧。</p>    <h3>下载</h3>    <pre>  <code class="language-java">GRADLE    compile 'com.jakewharton:butterknife:8.5.1'  annotationProcessor 'com.jakewharton:butterknife-compiler:8.5.1'</code></pre>    <p> </p>    <p> </p>    <p>来自:http://blog.csdn.net/androidmsky/article/details/56282777</p>    <p> </p>