ABOUT ME

-

  • 안드로이드 코딩을 좀 더 간단하게 "ButterKnife"
    모바일/Development 2014. 7. 27. 20:07
    728x90
    반응형




    링크: https://github.com/JakeWharton/butterknife



    ButterKnife는 Annotation기능을 이용하여 코딩을 쉽게할 수 있는 방법입니다.


    ※ Bind를 시켜야 사용 가능 (Kotlin과 비슷)


    text
    class Example extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.simple_activity); // Bind 하세요. ButterKnife.bind(this); } }



     @BindView 를 이용하면 자동으로 ID 설정과 캐스트를 해줌

    text
    // With ButterKnife @BindView(R.id.title) TextView title; @BindView(R.id.subtitle) TextView subtitle; @BindView(R.id.footer) TextView footer; @BindView(R.id.button1) Button button1; // Original subtitle= (TextView ) findViewById(R.id.subtitle); ...



    ETC

    text
    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);



    Resource Binding


    text
    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 }


    ListAdapter의 Viewholder 심플화

    text
    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); } } }


    List Array 형식도 심플화 가능

    text
    @BindViews({ R.id.first_name, R.id.middle_name, R.id.last_name }) List nameViews;




    Click 기능 심플 (void 이름 자유)


    text
    @OnClick(R.id.submit) void submit() { // TODO call server... }



    OnClick 예제





    지원

    OnClick, OnEditorAction, OnCheckedChanged, OnItemClick,

    OnItemSelected, OnLongClick, OnPageChange, OnTextChanged, OnTouch



    더 많은 기능이 있지만 http://jakewharton.github.io/butterknife/ 를 직접 참고


    ※주의※ Fragment binding reset

    text
    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(); } }

    개발자: JakeWharton



    ButterKnife 사용을 위한 설정!



    앱 build.gradle 에


    dependencies { compile 'com.jakewharton:butterknife:8.7.0' annotationProcessor 'com.jakewharton:butterknife-compiler:8.7.0' }


    2015-04-07: ButterKnife 업데이트 대응 글 내용 변경 (InjectView -> FindView, inject -> bind)

    2016-08-17: ButterKnife v8.2.1

    2017-07-06: ButterKnife v8.6.0

    2017-07-09: ButterKnife v8.7.0

    728x90

    댓글