티스토리 뷰
ButterKnife?
- Jake Wharton 님께서 개발하신 Android용 라이브러리이며 annotation processing을 통해 boilerplate code를 자동으로 생성하여 뷰와 필드 혹은 메소드를 바인딩 해주는 방식으로 동작한다.
- 예제를 살펴보면 현재 구축해둔 혹은 구축할 예정인 프로젝트의 소스가 크게 간결해 질수 있다는것을 파악할수 있다.
기능
- @BindView 를 이용해서 findViewById의 호출을 없애는것.
- 원하는 view를 묶어서 명령을 처리하는것.
- @OnClick을 활용하여 지저분한 listener의 선언을 없애는것.
- Resources 들에 대한 필드 선언과 할당을 합치는것.
기능1. Eliminate findViewById.
//-------------------------------------Original Code
public class MainActivity extends Activity
{
TextView tvBanner;
ImageView ivBanner;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
tvBanner = (TextView)findViewById(R.id.tv_banner);
ivBanner = (ImageView)findViewById(R.id.iv_banner);
}
}
//-------------------------------------Using Butterknife
public class MainActivity extends Activity
{
@BindView(R.id.tv_banner)
TextView tvBanner;
@BindView(R.id.iv_banner)
ImageView ivBanner;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
Butterknife.bind(this);
}
}
기능2. Group multiple views.
// Group multiple views into a List
@BindViews({R.id.tv_first, R.id.tv_second, R.id.tv_third})
List<TextView> contentViews;
// Create Action (apply or set)
static final ButterKnife.Action<View> DISABLE = new ButterKnife.Action<View>() {
@Override public void apply(View view, int index) {
view.setEnabled(false);
}
};
static final ButterKnife.Setter<View, Boolean> ENABLED = new ButterKnife.Setter<View, Boolean>() {
@Override public void set(View view, Boolean value, int index) {
view.setEnabled(value);
}
};
// call apply or set method
Butterknife.apply(contentView, DISABLE); // set enable false
Butterknife.apply(contentView, ENABLED, false); // set enable false
기능3. Eliminate listener.
@OnClick(R.id.btn_join)
void onClickJoin()
{
// implements join process
}
@OnClick({R.id.btn_join, R.id.btn_signin})
void onClick(View view)
{
int id = view.getId();
// implements join or signin
}
기능4. Eliminate resource lookup.
@BindString(R.string.title) String title;
@BindDrawable(R.drawable.background) Drawable background;
@BindColor(R.color.colorPrimary) int primary;
Reference.
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
TAG
- 비트연산자
- robocopy
- 행렬
- mathjax
- Math
- Kotlin
- 알고리즘
- 수식
- 비트마스크
- algorithm
- C
- 안드로이드
- Bit
- DNS
- highlightjs
- Android
- syntax highlighting
- mysql
- Highlighter
- python
- Matrix
- 도메인
- 비트
- 삼항연산자
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
글 보관함