Summary of using Butter Knife

Introduction

Use ** ButterKnife to build Android apps! ** I knew that, so I will summarize the introduction method and usage.

The development environment is as follows.

What is Butter Knife?

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.

How to install Butter Knife

  • build.gradle(Project)
    Add the following description in dependencies.
dependencies{
        classpath 'com.neenbedankt.gradle.plugins:android-apt:1.7'
        classpath 'com.jakewharton:butterknife-gradle-plugin:8.5.1'
}
  • build.gradle(app)
    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'
}

How to use Butter Knife

For the usage, I referred to the Official site.

Rough processing flow

  1. Use @BindView </ code> to declare the ID and variable to be associated.
  2. Write @ButterKnife.bind () </ code> and associate the ID with the variable.

Declaration of ID and View to associate

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
    }
  • The above code is a way to get the position of the clicked item.

Associate ID with view

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

Annotated but not working

  1. Check if ButterKnife.bind is described
  2. Check the description of gradle If your Gradle plugin is less than 22, write apt </ code> instead of annotationProcessor </ code>. It may cause it not to work.

About the event when clicking an item in ListView

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.

Changes from less than ver7

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