woong's

Android ButterKnife 사용하기 본문

Develop/Android

Android ButterKnife 사용하기

dlsdnd345 2017. 2. 10. 15:56
336x280(권장), 300x250(권장), 250x250, 200x200 크기의 광고 코드만 넣을 수 있습니다.

Android ButterKnife 사용하기


프로젝트를 진행하면서 findViewById , 데이터 바인딩등 반복 작업을 아무생각없이 계속 반복해서

작업하고 있었다. 이전부터 ButterKnife 를 알고 있었지만, 도입해보지 않았지만 이번 프로젝트부터

사용해 보려 합니다. 사용하면서 장단점을 비교해보고 장점이 큰경우 프로젝트 진행시에 도입해보려 합니다.


http://jakewharton.github.io/butterknife/



1. 준비 과정


1
2
compile 'com.jakewharton:butterknife:8.5.1'
annotationProcessor 'com.jakewharton:butterknife-compiler:8.5.1'
cs

 

android Build.gradle 에 라이브러리를 추가합니다.


2. 초기화


Activity


1
2
3
4
5
6
7
8
9
10
11
12
13
 @BindView(R.id.viewPagerSpace)
    ViewPager viewPagerSpace;
 
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_select_space);
        ButterKnife.bind(this);
 
        initLayout();
        init();
    }
cs


ViewHolder


1
2
3
4
5
6
7
8
9
10
11
12
13
14
/**
     * 뷰 재활용을 위한 viewHolder
     */
    public static class GalleryViewHolder extends RecyclerView.ViewHolder {
 
        @BindView(R.id.imageGallery)
        public ImageView imageGallery;
 
        public GalleryViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
 
    }
cs


Fragment


1
2
3
4
5
6
7
8
9
@BindView(R.id.recyclerView)
    RecyclerView recyclerView;
 
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view =  inflater.inflate(R.layout.fragment_life_style, container, false);
        unbinder = ButterKnife.bind(this, view);
        return view;
    }
cs


3. 형식


BindView


1
2
@BindView(R.id.recyclerView)
    RecyclerView recyclerView;
cs

OnClick

1
2
3
4
5
@OnClick(R.id.imgCamera)
    public void doCamera() {
        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        startActivityForResult(intent,RESULT_CAMERA);
    }
cs


'Develop > Android' 카테고리의 다른 글

Kotlin 사용하기 위한 준비 작업 하기  (0) 2017.05.19
Android Proguard 사용하기  (0) 2017.04.04
Android Trasitions API 사용하기  (0) 2016.10.12
Android 국가별 폴더 분기  (0) 2016.10.10
홈 버튼 오버라이드  (0) 2016.07.14
Comments