ButterKnife官方使用指南

看到了ButterKnife之后,感觉它实在是太棒了,可以省略掉一大堆无趣的findViewById(),整个代码看起来都舒服多了。这篇使用说明来自它的官方网站的简易介绍,用起来非常简单,但是也是有挺多的情况,所以还是觉得自己翻译出来,方便以后查阅吧!


使用@BindViewID注解相应的变量,ButterKnife就会在你的layout文件中找到所对应的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...
  }
}

上面例子中,所生成的代码大致与下面代码等同:

public void bind(ExampleActivity activity) {
  activity.subtitle = (android.widget.TextView) activity.findViewById(2130968578);
  activity.footer = (android.widget.TextView) activity.findViewById(2130968579);
  activity.title = (android.widget.TextView) activity.findViewById(2130968577);
}

资源绑定

使用@BindBool, @BindColor, @BindDimen, @BindDrawable, @BindInt, @BindString与一个对应的ID来绑定定义好的资源,

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

非ACTIVITY绑定

我们还可以在已知View的情况下,在任意的对象中,绑定该View中所含有的控件。比如在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;
  }
}

另外一个是在ViewHolder中:

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

其它的绑定方式

可使用Activity当做一个根View可以绑定任何对象。如果你使用了MVC模式,你可以使用ButterKnife.bind(this, activity)来绑定Controller。 可使用ButterKnife.bind(this)来绑定一个View里面的子View。如果你在layout文件中使用了<merge>标签并且在View的构造器中填充,你可以在这之后立马调用它。或者,你也可以在onFinishInflate()回调中调用。

VIEW LISTS

将所需要的控件,全部填充到一个List中。

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

apply()方法可以对List中所有的View执行某个操作。

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

可以指定一些简单的动作。

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

当然也可以在apply()方法中指定一个Android中控件的属性名。

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

绑定LISTENER

监听器也可以自动配置到相应的View上。

@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!");
}

还可以为将一个监听器函数,绑定到多个控件上。

@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!
  }
}

重置绑定

FragmentActivity的生命周期不同。当在FragmentonCreateView()中使用了绑定,就需要在onDestroyView()中将变量置为nullButterKnife在调用绑定时会返回一个Unbinder的实例,在适当的生命周期回调中,调用这个实例的unbind()方法。

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

其它绑定

通常@Bind和监听器绑定都是必须的。如果在目标View中为找到相应ID的控件,则会抛出异常。

为了抑制住这中异常,创建一个可选的绑定,可以使用@Nullable@Optional来注解变量或方法。

注 : 可使用任何名为@Nullable的注解来注解变量,但推荐Android support-annotations中的@Nullable

@Nullable @BindView(R.id.might_not_be_there) TextView mightNotBeThere;

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

多方法监听器

可在注解中加入参数来区分。

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

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

下载

GRADLE

compile 'com.jakewharton:butterknife:(insert latest version)'
annotationProcessor 'com.jakewharton:butterknife-compiler:(insert latest version)'

Read more

Volcano 与 Kubernetes GPU 调度学习笔记

本笔记系统整理 Volcano 调度器、Kubernetes 调度框架、GPU Device Plugin、HAMi 等云原生 AI 调度领域的核心知识,适合用于学习、复习和工程实践参考。 目录 * 第一部分:Volcano 入门 * 1. Volcano 是什么 * 2. 安装与快速使用 * 3. 核心特性一览 * 第二部分:Volcano 整体架构 * 4. Volcano 解决的核心问题 * 5. 整体架构与数据流 * 6. 三层抽象模型 * 第三部分:Volcano 核心实现原理 * 7. Session 机制 * 8. Gang Scheduling 实现 * 9. Queue 与 DRF 公平调度

容器镜像(4):镜像的常用工具箱

容器镜像(4):镜像的常用工具箱

前几篇在讲多架构镜像时已经用过 skopeo 和 crane 做镜像复制,这篇系统整理这两个工具的完整能力,同时介绍几个日常操作镜像时同样好用的工具。 一、skopeo:不依赖 Daemon 的镜像瑞士军刀 skopeo 的核心价值是绕过 Docker daemon,直接与 Registry API 交互。上一篇用它做镜像复制和离线传输,但它的能力远不止于此。 1.1 安装 # Ubuntu / Debian sudo apt install -y skopeo skopeo --version # skopeo version 1.15.1 1.2 inspect:免拉取检查镜像元数据 docker inspect 需要先把镜像拉到本地,skopeo inspect 直接向 Registry

容器镜像(3):多架构镜像构建

容器镜像(3):多架构镜像构建

一、什么是多架构镜像 1.1 OCI Image Index 上一篇介绍了单平台镜像的结构:一个 Manifest 指向 Config 和若干 Layer blob。多架构镜像在此之上多了一层——OCI Image Index(也叫 Manifest List),是一个轻量的索引文件,把多个单平台 Manifest 组织在一起: $ docker manifest inspect golang:1.22-alpine { "schemaVersion": 2, "mediaType": "application/vnd.oci.image.index.v1+json", "manifests&

容器镜像(2):containerd 视角下的镜像

容器镜像(2):containerd 视角下的镜像

一、为什么需要了解 containerd 如果你只用 docker run 跑容器,从来不关心底层,那可以不了解 containerd。但如果你在用 Kubernetes,或者想真正理解"容器运行时"是什么,containerd 是绕不开的。 事实上,当你执行 docker run 的时候,containerd 早就在后台悄悄工作了——Docker 从 1.11 版本开始,就把核心运行时剥离出来交给 containerd 负责。 1.1 Docker 的架构演变 早期的 Docker(1.10 及之前)是一个"大一统"的单体程序:一个 dockerd