Butter Knife使用(憎らしいfindviewbyidを放棄)
1409 ワード
findViewByIdと吐き気のタイプ変換を取り除く
ビュー、Activity、Dialogでビューを検索します(このメソッドでは、戻り値を変換するために使用される汎用型が使用されるため、タイプ変換は必要ありません).
リソースバインド
Fragment:
@BindView(R.id.title) TextView title;
//TextView title = (TextView)findViewByID(R.id.title);
@BindViews({ R.id.first_name, R.id.middle_name, R.id.last_name })
List nameViews;
ビュー、Activity、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);
:545a3c856c5f
:http://www.jianshu.com/p/9ad21e548b69
:
。 , 。
リソースバインド
@BindString(R.string.title) String title;
@BindDrawable(R.drawable.graphic) Drawable graphic;
@BindColor(R.color.red) int red;
@BindDimen(R.dimen.spacer) Float spacer;
Fragment:
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);
}
}