bufferknife使用

(1)官方原版
http://jakewharton.github.io/butterknife/

Introduction
Annotate fields with @BindView and a view ID for Butter Knife to find and automatically cast the corresponding view in your layout.
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...
}
} Instead of slow reflection, code is generated to perform the view look-ups. Calling bind delegates to this generated code that you can see and debug.
The generated code for the above example is roughly equivalent to the following:
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);
}
Resource Binding
Bind pre-defined resources with @BindBool, @BindColor, @BindDimen, @BindDrawable, @BindInt, @BindString, which binds an R.bool ID (or your specified type) to its corresponding field.
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
// ...
}
Non-Activity Binding
You can also perform binding on arbitrary objects by supplying your own view root.
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;
}
} Another use is simplifying the view holder pattern inside of a list adapter.
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);
}
}
} You can see this implementation in action in the provided sample.
Calls to ButterKnife.bind can be made anywhere you would otherwise put findViewById calls.
Other provided binding APIs:
• Bind arbitrary objects using an activity as the view root. If you use a pattern like MVC you can bind the controller using its activity with ButterKnife.bind(this, activity).
• Bind a view's children into fields using ButterKnife.bind(this). If you use tags in a layout and inflate in a custom view constructor you can call this immediately after. Alternatively, custom view types inflated from XML can use it in the onFinishInflate() callback.

View Lists
You can group multiple views into a List or array.
@BindViews({ R.id.first_name, R.id.middle_name, R.id.last_name })
List nameViews; The apply method allows you to act on all the views in a list at once.
ButterKnife.apply(nameViews, DISABLE);
ButterKnife.apply(nameViews, ENABLED, false); Action and Setter interfaces allow specifying simple behavior.
static final ButterKnife.Action DISABLE = new ButterKnife.Action() {
@Override public void apply(View view, int index) {
view.setEnabled(false);
}
};
static final ButterKnife.Setter ENABLED = new ButterKnife.Setter() {
@Override public void set(View view, Boolean value, int index) {
view.setEnabled(value);
}
}; An Android Property can also be used with the apply method.
ButterKnife.apply(nameViews, View.ALPHA, 0.0f);
Listener Binding
Listeners can also automatically be configured onto methods.
@OnClick(R.id.submit)
public void submit(View view) {
// TODO submit data to server...
} All arguments to the listener method are optional.
@OnClick(R.id.submit)
public void submit() {
// TODO submit data to server...
} Define a specific type and it will automatically be cast.
@OnClick(R.id.submit)
public void sayHi(Button button) {
button.setText("Hello!");
} Specify multiple IDs in a single binding for common event handling.
@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();
}
} Custom views can bind to their own listeners by not specifying an ID.
public class FancyButton extends Button {
@OnClick
public void onClick() {
// TODO do something!
}
}
Binding Reset
Fragments have a different view lifecycle than activities. When binding a fragment in onCreateView, set the views to null in onDestroyView. Butter Knife returns an Unbinder instance when you call bind to do this for you. Call its unbind method in the appropriate lifecycle callback.
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();
}
}
Optional Bindings
By default, both @Bind and listener bindings are required. An exception will be thrown if the target view cannot be found.
To suppress this behavior and create an optional binding, add a @Nullable annotation to fields or the @Optional annotation to methods.
Note: Any annotation named @Nullable can be used for fields. It is encouraged to use the @Nullable annotation from Android's "support-annotations" library.
@Nullable @BindView(R.id.might_not_be_there) TextView mightNotBeThere;

@Optional @OnClick(R.id.maybe_missing) void onMaybeMissingClicked() {
// TODO ...
}
Multi-Method Listeners
Method annotations whose corresponding listener has multiple callbacks can be used to bind to any one of them. Each annotation has a default callback that it binds to. Specify an alternate using the callback parameter.
@OnItemSelected(R.id.list_view)
void onItemSelected(int position) {
// TODO ...
}

@OnItemSelected(value = R.id.maybe_missing, callback = NOTHING_SELECTED)
void onNothingSelected() {
// TODO ...
}
Bonus
Also included are findById methods which simplify code that still has to find views on a View, Activity, or Dialog. It uses generics to infer the return type and automatically performs the cast.
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); Add a static import for ButterKnife.findById and enjoy even more fun.

Download

Gradle
compile 'com.jakewharton:butterknife:8.4.0'
apt 'com.jakewharton:butterknife-compiler:8.4.0'
License
Copyright 2013 Jake Wharton

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
(2)简单使用
http://www.cnblogs.com/wanqieddy/p/3967895.html
butterknife简单使用
一、原理。
 最近发现一个很好用的开源框架,蛮不错的,可以简化你的代码,是关于注解的。不多说直接进入使用步骤讲解。
二、步骤。
1、准备阶段,先到官网(  http://jakewharton.github.io/butterknife/ )上jar包,下载下来。
2、把下载下来的jar包,放到项目的libs下,就会自动导入项目了。
3、配置eclips,鼠标对准需要注解的项目,单击右键 poperties –>java Compiler –>
Annotation Procession –> 钩一下 Enable project specific settings 其它的就会自动钩上了
–> Factory Path ( 钩一下Enable project specific settings )–> 最后Add …. JARs 把刚刚下载的jar包来。这样eclips配置就可以了。

5、是用注解,直接上代码。
    xml部分
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >

android:id="@+id/tv_test"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="@string/hello_world"
tools:context=".MainActivity" />


 java部分package com.msquirrel.main;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;
import android.os.Bundle;
import android.app.Activity;
import android.widget.TextView;

public class MainActivity extends Activity {

@InjectView(R.id.tv_test)
TextView tvTest;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.inject(this);
tvTest.setText("test");
}

@OnClick(R.id.tv_test)
public void sayHi() {
tvTest.setText("Hello!");
}
}这样就算完成了,就可以使用注解了。

(3)官方原版翻译
http://it165.net/pro/html/201404/12375.html

程序员都是懒惰的,不想写一大堆像下面这样的代码
class ExampleActivity extends Activity {
TextView title;
TextView subtitle;
TextView footer;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.simple_activity);
title = (TextView) findViewById(R.id.title);
subtitle = (TextView) findViewById(R.id.subtitle);
footer = (TextView) findViewById(R.id.footer);
// TODO Use views...
}
}

这样很不好,因此我们就找到了一个方法,使用魔法一般的注解,来浓缩代码,就像下面这样
class ExampleActivity extends Activity {
@Magic(R.id.title) TextView title;
@Magic(R.id.subtitle) TextView subtitle;
@Magic(R.id.footer) TextView footer;
@Override public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.simple_activity);
// TODO Use "injected" views...
}
}

然而,这样虽然看起来很酷,很Magic,不过还是会带来严重的运行时的问题

于是,我们就利用javac 编译器中一大利器来生成前面例子中的findViewById的引用,并且继续保持代码的精简 ,继续看下面的代码
class ExampleActivity extends Activity {
@InjectView(R.id.title) TextView title;
@InjectView(R.id.subtitle) TextView subtitle;
@InjectView(R.id.footer) TextView footer;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.simple_activity);
ButterKnife.inject(this);
// TODO Use "injected" views...
}
}

在原本Magic的地方使用inject 方法来替代,下面是该方法的一段代表(This method delegates to generated code 不明),可以看并debug
public void inject(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);
}

(这段可以忽略吗?貌似翻译不对,看不太懂)有些人称这个为视图注入,并加到了传统的依赖注入框架中,也许在术语上有错误,不过或许有一点作用.......



NON-ACTIVITY INJECTION

我们可以对任意的View对象使用注入,当然了,需要提供view root的对象
public class FancyFragment extends Fragment {
@InjectView(R.id.button1) Button button1;
@InjectView(R.id.button2) Button button2;
@Override
View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fancy_fragment, container, false);
ButterKnife.inject(this, view);
// TODO Use "injected" views...
return view;
}
}

另外一个用处是简化list adapter中的View Holder模式,这个例子可以在提供的sample中看到
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);09.view.setTag(holder);
}
holder.name.setText("John Doe");
// etc...
return convertView;
}
static class ViewHolder {
@InjectView(R.id.title) TextView name;
@InjectView(R.id.job_title) TextView jobTitle;
public ViewHolder(View view) {
ButterKnife.inject(this, view);
}
}
}

其他的API(这段真心建议看一下原版)
-当使用activity作为view root节点时,你如果使用的是像MVC设计模式,你可以将控制器注入,使用它的acitvity并通过ButterKnife.inject(this, activity)方法 -将一个子View对象字段注入,使用ButterKnife.inject(this)(在前面例子的onCreate方法中就是,不懂的可以参考这个),如果你在一个layout的构造方法中使用标签并且,inflate了一个自定义View,立即调用这个方法。或者,如果是从xml中inflate自定义view,可以使用这个方法---onLayoutInflated()


CLICK LISTENER INJECTION


Click listener 也可以被注入
@OnClick(R.id.submit)
public void submit() {
//TODO submit data to server...
}

也可以加一个view对象,特定的类型会被自动转换
@OnClick(R.id.submit)
public void sayHi(Button button) {
button.setText("Hello!");
}

一次指定多个id,同时加事件
@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();
}
}

INJECTION RESET

Fragments 有比Avtivity更多的生命周期 ,如果需要在onCreateView中注入一个fragment,在onDestroyView中销毁它

Butter Knife 有一个reset 方法自动实现它

public class FancyFragment extends Fragment {
@InjectView(R.id.button1) Button button1;
@InjectView(R.id.button2) Button button2;
@Override
View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fancy_fragment, container, false);
ButterKnife.inject(this, view);
// TODO Use "injected" views...
return view;
}
@Override
void onDestroyView() {
super.onDestroyView();
ButterKnife.reset(this);
}
}



OPTIONAL INJECTIONS

默认情况下 @InjectView和@OnClick都是必须加上的,如果当前对象不存在,就会抛出一个异常

为了压制这个异常,可以在变量或者方法上加入一下注解
@Optional @InjectView(R.id.might_not_be_there)
TextView mightNotBeThere;
@Optional @OnClick(R.id.maybe_missing)
void onMaybeMissingClicked() {
// TODO ...
}

BONUS


这段看不太懂,大意估计是除了以上注入方法之外还有一种,类型会自动转换,需要静态导入ButterKnife.findById
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);


Download:当前版本 4.0.1


具体:http://github.com/JakeWharton/butterknife

jar包:http://repo1.maven.org/maven2/com/jakewharton/butterknife/4.0.1/butterknife-4.0.1.jar



Maven配置:如果你是用maven开发的童鞋,请自觉加上如下依赖

com.jakewharton
butterknife
4.0.1


32
IDE的配置

• IntelliJ IDEA — 如果你是用外部工具管理工程 (如 Maven pom.xml) 应该能正常工作, 不然就看看这个 http://jakewharton.github.io/butterknife/ide-idea.html• Eclipse — http://jakewharton.github.io/butterknife/ide-eclipse.html


PROGUARD

使用注入会导致ProGuard 认为变量未被使用,因此,需要在ProGuard 中加入如下字段
-dontwarn butterknife.internal.**
-keep class **$$ViewInjector { *; }
-keepnames class * { @butterknife.InjectView *;}

Having Happy Coding!





你可能感兴趣的:(bufferknife使用)