Butterknifeクイックエントリーアプリケーション

5782 ワード

Butterknifeクイックエントリーアプリケーション
ツールとバージョン
   2018-01-20
  butterknife  8.8.1'
androidstudio  3.0

背景
Bind Android views and callbacks to fields and methods. 
butterknife             ,      ,           APP  。

手順の使用
1、gradleの構成
dependencies {
    compile 'com.jakewharton:butterknife:8.8.1'
    annotationProcessor 'com.jakewharton:butterknife-compiler:8.8.1'
}

    Kotlin, annotationProcessor   kapt

2、butterknifeのバインド
2.1、  activity
class ExampleActivity extends Activity {

@Override public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.simple_activity);
ButterKnife.bind(this);//  activity,   setContentView  
// TODO Use fields...
}
}

2.2、  fragment
public class FancyFragment extends Fragment {
@BindView(R.id.button1) Button button1;
@BindView(R.id.button2) Button button2;

@Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fancy_fragment, container, false);
ButterKnife.bind(this, view);
// TODO Use fields...
return view;
}
}

2.3、Adapter  
public class MyAdapter extends BaseAdapter {
@Override public View getView(int position, View view, ViewGroup parent) {
ViewHolder holder;
if (view != null) {
  holder = (ViewHolder) view.getTag();
} else {
  view = inflater.inflate(R.layout.whatever, parent, false);
  holder = new ViewHolder(view);
  view.setTag(holder);
}

holder.name.setText("John Doe");
// etc...

return view;
}

static class ViewHolder {
@BindView(R.id.title) TextView name;
@BindView(R.id.job_title) TextView jobTitle;

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

     APIs:
           ,     MVC     ,          ButterKnife.bind(this, activity).     

 ButterKnife.bind(this)      ,          merge                   ,   xml        ,   onFinishInflate()     。

2.4、view  
@BindView(R.id.title) TextView title;
@BindView(R.id.subtitle) TextView subtitle;
@BindView(R.id.footer) TextView footer;

2.5、    view
@BindViews({ R.id.first_name, R.id.middle_name, R.id.last_name })
List nameViews;

2.6、  apply  ,        view
static final ButterKnife.Action DISABLE = new ButterKnife.Action() {
@Override public void apply(View view, int index) {
view.setEnabled(false);
}
};
static final ButterKnife.Setter ENABLED = new ButterKnife.Setter() {
@Override public void set(View view, Boolean value, int index) {
view.setEnabled(value);
}
};

ButterKnife.apply(nameViews, DISABLE);
ButterKnife.apply(nameViews, ENABLED, false);

       :
ButterKnife.apply(nameViews, View.ALPHA, 0.0f);

2.7、    
class ExampleActivity extends Activity {
@BindString(R.string.title) String title;
@BindDrawable(R.drawable.graphic) Drawable graphic;
@BindColor(R.color.red) int red; // int or ColorStateList field
@BindDimen(R.dimen.spacer) Float spacer; // int (for pixel size) or float (for exact value) field
// ...
}

2.8、        
@OnClick(R.id.submit)
public void submit(View view) {
// TODO submit data to server...
}

      。
@OnClick(R.id.submit)
public void submit() {
// TODO submit data to server...
}

         ,      。
@OnClick(R.id.submit)
public void sayHi(Button button) {
button.setText("Hello!");
}

  view       。
@OnClick({ R.id.door1, R.id.door2, R.id.door3 })
public void pickDoor(DoorView door) {
if (door.hasPrizeBehind()) {
    Toast.makeText(this, "You win!", LENGTH_SHORT).show();
} else {
    Toast.makeText(this, "Try again", LENGTH_SHORT).show();
}
}

   view            id
public class FancyButton extends Button {
@OnClick
public void onClick() {
// TODO do something!
}
}

2.9、  

   ButterKnife   fragment , Fragment onDestroyView     。
public class FancyFragment extends Fragment {
@BindView(R.id.button1) Button button1;
@BindView(R.id.button2) Button button2;
private Unbinder unbinder;

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

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

2.10、@Nullable @Optional
   @BindView @OnClick ,    view        ,      ,      @Nullable @Optional
@Nullable @BindView(R.id.might_not_be_there) TextView mightNotBeThere;

@Optional @OnClick(R.id.maybe_missing) void o   nMaybeMissingClicked() {
 // TODO ...
}

  :  ”support-annotations“ library   @Nullable  。

2.11、    listener(Multi-method Listeners)。                     ,           callback,           ,     callback            .

@OnItemSelected(R.id.list_view)
void onItemSelected(int position) {
// TODO ...
}

@OnItemSelected(value = R.id.maybe_missing, callback = NOTHING_SELECTED)
void onNothingSelected() {
// TODO ...
}

2.12、ButterKnife.findById()      Activity,Dialog View    View.ButterKnife         ,                ,        View      .

View view = LayoutInflater.from(context).inflate(R.layout.thing, null);
TextView firstName = ButterKnife.findById(view, R.id.first_name);
TextView lastName = ButterKnife.findById(view, R.id.last_name);
ImageView photo = ButterKnife.findById(view, R.id.photo);

3、コンパイルを混同する場合はproguardファイルに追加する必要があります
-keep class butterknife.** { *; }
-dontwarn butterknife.internal.**
-keep class **$$ViewBinder { *; }

-keepclasseswithmembernames class * {
    @butterknife.* ;
}

-keepclasseswithmembernames class * {
    @butterknife.* ;
}