I read about annotations in Android and came across such a lib - ButterKnife.
What is the use of its use? What does it provide besides replacing the findViewById method?
I read about annotations in Android and came across such a lib - ButterKnife.
What is the use of its use? What does it provide besides replacing the findViewById method?
There is not only
Button button = (Button) findViewById(R.id.button);
can be replaced by
@InjectView(R.id.button) Button mButton;
But Kolbeki inject.
Say:
button.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { // ... } });
will turn into:
@OnClick(R.id.button) public void onButtonClick() { // ... }
Adapters simplify code. If you wrote your adapter for ListView
, you should have written ViewHolder
for it. When there is a lot of it, it’s not very nice and convenient to holder
elements in the holder
. Using ButterKnife is easy:
static class ViewHolder{ @InjectView(R.id.image_in_item) ImageView image; @InjectView(R.id.textview_in_item) TextView text; public ViewHolder(View view){ ButterKnife.inject(this, view); } }
If the question is precisely why ButterKnife, and not, say, RoboGuice, then it is smaller. RoboGuice draws a lot of dependencies and slower, for it is in runtime, but ButterKnife does it during compilation.
Yes, and Lieb wrote Jake Wharton. This is the same who wrote ActionBarSherlock and other cool stuff.
UPD: as comrade pavlofff correctly noted, in new versions of the library, @InjectView
was replaced with @Bind
, plus other changes. It is better to look directly into the library repositories .
@InjectView
library @InjectView
been replaced by @Bind
and somewhat expanded by derivatives. Could you indicate this in the answer, but it is somewhat confusing. - pavlofffSource: https://ru.stackoverflow.com/questions/476282/
All Articles