频道栏目
首页 > 资讯 > Android > 正文

Android --- butterknife框架使用

17-01-20        来源:[db:作者]  
收藏   我要投稿

介绍

butterknife也是一个依赖注入框架,借助annonation实现view的快速初始化,解除findViewById的烦恼

工程引入

配置project的build.gradle,来引入android-apt插件

buildscript {
  repositories {
    mavenCentral()
   }
  dependencies {
    classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'
  }
}

配置module的build.gradle,apply theandroid-aptplugin,and add theButter Knifedependencies.

apply plugin: 'android-apt`

android{
...
}

dependencies{
 compile 'com.jakewharton:butterknife:8.2.1'
 apt 'com.jakewharton:butterknife-compiler:8.2.1'
}

注意
butter knife尽量不要在library中使用,如果要用,还得转R2,太麻烦了

使用

使用@BindView注解view

class ExampleActivity extends Activity {
  @BindView(R.id.title) TextView title;
  @BindView(R.id.subtitle) TextView subtitle;
  @BindView(R.id.footer) TextView footer;

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

Resource的注解

@BindBool, @BindColor, @BindDimen, @BindDrawable, @BindInt, @BindString

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
  // ...
}

non-activity的注解

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

也可以在ViewHolder中使用

View List

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

使用apply方法,能一次操作整个View的List

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

包括设置属性

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

Listener的绑定

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

绑定的释放

由于Fragment的生命周期区别于activity,当我们在Fragment的onCreatView做绑定的时候,要在onDestoryView中设置views为null。Butter Knife 返回一个Unbider实例来解决这个问题,
eg:

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();
  }
}
相关TAG标签
上一篇:Android中事件的传递
下一篇:android studio离线打包mui应用
相关文章
图文推荐

关于我们 | 联系我们 | 广告服务 | 投资合作 | 版权申明 | 在线帮助 | 网站地图 | 作品发布 | Vip技术培训 | 举报中心

版权所有: 红黑联盟--致力于做实用的IT技术学习网站