Android ButterKnife basics

reference

Github

Ready to work

Add in the project's build.gradle file

compileOptions {
    
    
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
}
api 'com.jakewharton:butterknife:8.4.0'
annotationProcessor 'com.jakewharton:butterknife-compiler:8.4.0'

The official version is already above 10, but it is not suitable for us to use, we can lower the version.

If we want to add to the library, the above integration method alone will not work, and we need to add in the build.gradle file in the project:

annotationProcessor 'com.jakewharton:butterknife-compiler:8.4.0'

Install plugin

Android ButterKniffe Plugin Plus

initialization

Activity

@Override
protected void onCreate(Bundle savedInstanceState) {
    
    
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    ButterKnife.bind(this);
}

Fragment

Unbinder unbinder; 

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
    
    
	  View inflate = inflater.inflate(R.layout.fragment, container, false);
      unbinder = ButterKnife.bind(this, inflate);
      return inflate;
}

@Override
public void onDestroyView() {
    
    
  super.onDestroyView();
  unbinder.unbind();
}

Adapter

static class ViewHolder {
    
      
  public ViewHolder(View view) {
    
      
      ButterKnife.bind(this, view);  
  }  
}  

Since the life cycle of Fragment is different from Activity, when binding a view in the CreateView method, the corresponding view needs to be set to null in onDestoryView, and then the ButterKnife needs to be unbound.

use

Insert picture description here

Right-click or shortcut key to pop up the selected layout file

Insert picture description here
Select the last item to use.

Guess you like

Origin blog.csdn.net/m0_48440239/article/details/114688016