ButterKnife使用小结

项目官网:http://jakewharton.github.io/butterknife/

Github主页:https://github.com/JakeWharton/butterknife

这个注解框架强大好用,国外牛逼博主  JakeWharton 杰作,下面 代码片段时最新的 7.01.jar,跟以前的使用变化很大。

直接下载jar包,引入项目就行,基本使用代码片如下:

package com.example.butterknife;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import butterknife.ButterKnife;
import butterknife.Bind;
import butterknife.OnClick;
import butterknife.OnItemClick;
import butterknife.OnLongClick;
import java.util.List; import static android.widget.Toast.LENGTH_SHORT; public class SimpleActivity extends Activity { //一个可以实现全局View都调用的方法,需要用集合或者数组来传入View对象
private static final ButterKnife.Action<View> ALPHA_FADE = new ButterKnife.Action<View>() {
@Override public void apply(View view, int index) {
AlphaAnimation alphaAnimation = new AlphaAnimation(0, 1);
alphaAnimation.setFillBefore(true);
alphaAnimation.setDuration(500);
alphaAnimation.setStartOffset(index * 100);
view.startAnimation(alphaAnimation);
}
};

//最基本的注解单个View对象
@Bind(R.id.title) TextView title;
@Bind(R.id.subtitle) TextView subtitle;
@Bind(R.id.hello) Button hello;
@Bind(R.id.list_of_things) ListView listOfThings;
@Bind(R.id.footer) TextView footer;

//扩展,可以注解多个View对象
@Bind({ R.id.title, R.id.subtitle, R.id.hello })
List<View> headerViews; private SimpleAdapter adapter;

//实现VIew的onclick方法,Id用onclick注解以后,后面直接方法名就可以使用,不需要再二次调用sayHello方法
@OnClick(R.id.hello) void sayHello() {
Toast.makeText(this, "Hello, views!", LENGTH_SHORT).show();
ButterKnife.apply(headerViews, ALPHA_FADE);
}
//同理onclick
@OnLongClick(R.id.hello) boolean sayGetOffMe() {
Toast.makeText(this, "Let go of me!", LENGTH_SHORT).show();
return true;
} @OnItemClick(R.id.list_of_things) void onItemClick(int position) {
Toast.makeText(this, "You clicked: " + adapter.getItem(position), LENGTH_SHORT).show();
} @Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.simple_activity);
//初始化Butter Knife
ButterKnife.bind(this); // Contrived code to use the bound fields.
title.setText("Butter Knife");
subtitle.setText("Field and method binding for Android views.");
footer.setText("by Jake Wharton");
hello.setText("Say Hello"); adapter = new SimpleAdapter(this);
listOfThings.setAdapter(adapter);
}
}

  

开发中最常用的适配器,用注解代码如下:

package com.example.butterknife;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import butterknife.ButterKnife;
import butterknife.Bind; public class SimpleAdapter extends BaseAdapter {
private static final String[] CONTENTS =
"The quick brown fox jumps over the lazy dog".split(" "); private final LayoutInflater inflater; public SimpleAdapter(Context context) {
inflater = LayoutInflater.from(context);
} @Override public int getCount() {
return CONTENTS.length;
} @Override public String getItem(int position) {
return CONTENTS[position];
} @Override public long getItemId(int position) {
return position;
} @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.simple_list_item, parent, false);
holder = new ViewHolder(view);
view.setTag(holder);
} String word = getItem(position);
holder.word.setText("Word: " + word);
holder.length.setText("Length: " + word.length());
holder.position.setText("Position: " + position);
// Note: don't actually do string concatenation like this in an adapter's getView. return view;
} static class ViewHolder {
@Bind(R.id.word) TextView word;
@Bind(R.id.length) TextView length;
@Bind(R.id.position) TextView position; ViewHolder(View view) {
ButterKnife.bind(this, view);
}
}
}

  

开发版本,没有正式发布的时候,可以再Application中设置:

package com.example.butterknife;

import android.app.Application;
import butterknife.ButterKnife; public class SimpleApp extends Application {
@Override public void onCreate() {
super.onCreate();
ButterKnife.setDebug(BuildConfig.DEBUG);
}
}

  

上一篇:MVC - 知识点


下一篇:Python - Django - ORM 多对多表结构的三种方式