본문 바로가기
#모바일 [Mobile]/Android

[Android - OpenSource] Butter Knife

by cy_mos 2019. 7. 25.
반응형
[Android] Application Developement Butter Knife YouTube

[Android] Butter Knife Logo

 

  • 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

 

Butter Knife

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) TextVie

jakewharton.github.io

 

ButterKnife

Introduction of ButterKnife annotation library and its all features

www.slideshare.net

 

반응형

댓글