Use ** ButterKnife to build Android apps! ** I knew that, so I will summarize the introduction method and usage.
The development environment is as follows.
Since it is introduced in various articles and books, I think it is one of the libraries that is easy to introduce newly.
ButterKnife associates id and view with annotations ( @ ~ </ code>)
You can ** reduce the description related to view ** such as findViewByID and OnClickListener.
dependencies{
classpath 'com.neenbedankt.gradle.plugins:android-apt:1.7'
classpath 'com.jakewharton:butterknife-gradle-plugin:8.5.1'
}
apply plugin: Add the following description after'com.android.application' </ code>.
apply plugin: 'com.jakewharton.butterknife'
Add the following description in dependencies.
dependencies{
compile 'com.jakewharton:butterknife:8.5.1'
apt 'com.jakewharton:butterknife-compiler:8.5.1'
}
For the usage, I referred to the Official site.
@BindView </ code> to declare the ID and variable to be associated.
@ButterKnife.bind () </ code> and associate the ID with the variable.
Enter the corresponding ID in the [] part of the code.
--General writing
@BindView(R.id.[ID])
ListView listView;
--Event when the button is clicked
@OnClick(R.id.[ID])
public void onClick(View v) {
//Describe the processing at the time of clicking
}
If you want to do the same process with multiple buttons, you can implement it by writing the code as follows.
@OnClick({R.id.[ID],R.id.[ID]})
public void onClick(View v) {
//Describe the processing at the time of clicking
}
--Event when Clicking Item in ListView
@OnItemClick(R.id.[ID])
void OnListItemClick(int position) {
//Processing when clicking an Item
}
--For Activity (argument: activity)
OnCreate(){
ButterKnife.bind(this);
}
--For Fragment (arguments: activity, view)
For Fragment you need to pass a view.
OnCreateView(){
ButterKnife.bind(this,view);
}
Tips
apt </ code> instead of annotationProcessor </ code>.
It may cause it not to work.
It was in KeithYokoma's article, but if the header etc. is registered in the listview, the position of the clicked item and the position to get it may be different. Please note that there seems to be one.
Please note that the writing method may differ depending on the ver.
less than ver7 | ver7 or later |
---|---|
@InjectView |
@BindView |
@InjectViews |
@BindView |
ButterKnife.inject |
ButterKnife.bind |
ButterKnife.reset |
ButterKnife.unbind |
Recommended Posts