반응형
- Annotate fields with @BindView and a view ID for Butter Knife to find and automatically cast the corresponding view in your layout.
📌 Butter Knife Installation (Gradle)
dependencies {
...
compile 'com.jakewharton:butterknife:8.5.1'
annotationProcessor 'com.jakewharton:butterknife-compiler:8.5.1'
...
}
📄 Butter Knife (ACTIVITY BINDING) Source Code
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...
}
}
📄 Butter Knife (NON-ACTIVITY BINDING) Source Code
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);
}
}
📄 Butter Knife (LISTENER BINDING) Source Code
@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();
}
}
@OnClick( {R.id.button1, R.id.button2, R.id.button3} )
@Override
public void onClick(View v) {
// here source code
}
🚀 REFERENCE
반응형
'#모바일 [Mobile] > Android' 카테고리의 다른 글
[Android] ButterKnife Proguard 설정하기 (How to configure Proguard settings for ButterKnife?) (0) | 2019.08.01 |
---|---|
[Android] AsyncTask (0) | 2019.07.25 |
[Android] Ripple Drawable (버튼 눌림 효과, 물결 효과) (0) | 2019.07.24 |
[Android] Date Picker Dialog (0) | 2019.07.22 |
[Android] Android Studio Assets Folder 만들기 (0) | 2019.06.27 |
댓글