使用 ButterKnife 繫結檢視
我們可以使用 @BindView
和 Butter Knife 的檢視 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;
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;
}
// in fragments or non activity bindings we need to unbind the binding when view is about to be destroyed
@Override
public void onDestroy() {
super.onDestroy();
unbinder.unbind();
}
}
在對話方塊中繫結檢視
我們可以使用 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);
在 ViewHolder 中繫結檢視
static class ViewHolder {
@BindView(R.id.title) TextView name;
@BindView(R.id.job_title) TextView jobTitle;
public ViewHolder(View view) {
ButterKnife.bind(this, view);
}
}
繫結資源
除了對繫結檢視有用之外,還可以使用 ButterKnife 繫結資源,例如 strings.xml
,drawables.xml
,colors.xml
,dimens.xml
等中定義的資源。
public 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
@Override
public void onCreate(Bundle savedInstanceState) {
// ...
ButterKnife.bind(this);
}
}
繫結檢視列表
你可以將多個檢視分組到 List 或陣列中。當我們需要一次對多個檢視執行一個操作時,這非常有用。
@BindViews({ R.id.first_name, R.id.middle_name, R.id.last_name })
List<EditText> 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);
//We can use Action and Setter interfaces allow specifying simple behavior.
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);
}
};
可選繫結
預設情況下,@Bind
和偵聽器繫結都是必需的。如果找不到目標檢視,則丟擲異常。但是如果我們不確定某個檢視是否存在,那麼我們可以在欄位或 @Optional
註釋中新增 @Nullable
註釋,以抑制此行為並建立可選繫結。
@Nullable
@BindView(R.id.might_not_be_there) TextView mightNotBeThere;
@Optional
@OnClick(R.id.maybe_missing)
void onMaybeMissingClicked() {
// TODO ...
}