简述Butterknife示例和原理

1 篇文章 0 订阅
0 篇文章 0 订阅
本文介绍了Android框架Butterknife的使用,通过一个简单的示例展示其如何替代传统findViewById方法,并探讨其运行原理。在编译期间,Butterknife自动生成Java代码,简化了视图注入和事件绑定的过程。
摘要由CSDN通过智能技术生成

最近使用了一个注入类型的Android框架——butterknife,这种类型的框架和一般使用注解方式不同。

https://github.com/JakeWharton/butterknife

上面是butterknife的github地址,本文讲解的就是里面的案例。


由于我是使用Android studio,在app目录下的build.gradle中添加如下依赖,项目中就可以直接使用butterknife,不需要想eclipse,需要引用jar文件。

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:21.0.2'
    compile 'com.jakewharton:butterknife:6.0.0'
}


下面的时案例的代码,代码比较少,就贴下。

SimpleActivity.java

package com.example.butterknife;

public class SimpleActivity extends Activity {

    @InjectView(R.id.title)
    TextView title;
    @InjectView(R.id.subtitle)
    TextView subtitle;
    @InjectView(R.id.hello)
    Button hello;
    @InjectView(R.id.list_of_things)
    ListView listOfThings;
    @InjectView(R.id.footer)
    TextView footer;

    SimpleAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.simple_activity);

        ButterKnife.setDebug(true);
        ButterKnife.inject(this);

        // Contrived code to use the "injected" views.
        title.setText("Butter Knife");
        subtitle.setText("View \"injection\" for Android.");
        footer.setText("by Jake Wharton");
        hello.setText("Say Hello");

        adapter = new SimpleAdapter(this);
        listOfThings.setAdapter(adapter);
    }

    // hello的点击事件
    @OnClick(R.id.hello)
    void sayHello() {
        Toast.makeText(this, "Hello, views!", LENGTH_SHORT).show();
    }

    // hello的长按事件
    @OnLongClick(R.id.hello)
    boolean sayGetOffMe() {
        Toast.makeText(this, "Let go of me!", LENGTH_SHORT).show();
        return true;
    }

    // listview的item点击事件
    @OnItemClick(R.id.list_of_things)
    void onItemClick(int position) {
        Toast.makeText(this, "You clicked: " + adapter.getItem(position), LENGTH_SHORT).show();
    }
}


SimpleAdapter.java 

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.InjectView;

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 {
		@InjectView(R.id.word)
		TextView word;
		@InjectView(R.id.length)
		TextView length;
		@InjectView(R.id.position)
		TextView position;

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

simple_activity.xml

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="8dp">
  <TextView
      android:id="@+id/title"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:gravity="center"
      android:textSize="50sp"
      />
  <TextView
      android:id="@+id/subtitle"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:gravity="center"
      android:textSize="20sp"
      />
  <Button
      android:id="@+id/hello"
      android:layout_width="match_parent"
      android:layout_height="0dp"
      android:layout_weight="1"
      android:layout_margin="10dp"
      />
  <ListView
      android:id="@+id/list_of_things"
      android:layout_width="match_parent"
      android:layout_height="0dp"
      android:layout_weight="1"
      android:layout_margin="10dp"
      />
  <TextView
      android:id="@+id/footer"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:gravity="center"
      android:textSize="17sp"
      android:textStyle="italic"
      />
</LinearLayout>

实际的运行效果,当时是可以的。


butterknife的这注解,实际上只是帮助我们,少些部分代码,或者使我们的代码更加简洁。

比如

    @InjectView(R.id.hello)
    Button hello;

    // hello的点击事件
    @OnClick(R.id.hello)
    void sayHello() {
        Toast.makeText(this, "Hello, views!", LENGTH_SHORT).show();
    }

可以代替下面的代码

        Button hello = (Button) findViewById(R.id.hello);
        hello.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                Toast.makeText(SimpleActivity.this, "Hello, views!", LENGTH_SHORT).show();
            }
        });

只不过使用Java 注解的方式,可以使代码更加简洁。


==========================================华丽丽的分割线=====================================================

下面简单介绍下原理,我们以InjectView为例,先看下下面的代码。

package butterknife;

import java.lang.annotation.Retention;
import java.lang.annotation.Target;

import static java.lang.annotation.ElementType.FIELD;
import static java.lang.annotation.RetentionPolicy.CLASS;

/**
 * Bind a field to the view for the specified ID. The view will automatically be cast to the field
 * type.
 * <pre><code>
 * {@literal @}InjectView(R.id.title) TextView title;
 * </code></pre>
 *
 * @see Optional
 */
@Retention(CLASS) @Target(FIELD)
public @interface InjectView {
  /** View ID to which the field will be bound. */
  int value();
}

Inject上的Retention表示这个注解会加载到哪个时期,一共有三种,source,class,runtime,分别表示这个注解会保留到源码级别,class级别(就是java编译生成的哪个class文件),运行时界别(代码在运行的时候,都会有的)。


InjectView是class级别说明,injectview在代码编译的时候做了一些手脚,然后代码运行的时候,就可以自动做这些操作。

我在项目目录下的编译生成的文件夹里发现 了一些情况。

在/butterknife-sample/app/build/intermediates/classes/debug/com/example/butterknife这个目录里面,基本上都是class文件,

但是我发现了2个Java文件(是的,是java文件,竟然是java文件,不是说java文件编译过后是class文件,怎么会有class文件呢?疑问)

SimpleActivity$$ViewInjector.java

SimpleAdapter$ViewHolder$$ViewInjector.java

联想到Android项目可以自动生成R.java文件,所以我们有理由相信这写java文件是由于编译自动生成的(至于怎么生成的,后面再表)。


先看下这个文件里面到底是什么内容。

SimpleActivity$$ViewInjector.java

// Generated code from Butter Knife. Do not modify!
package com.example.butterknife;

import android.view.View;

import butterknife.ButterKnife.Finder;

public class SimpleActivity$$ViewInjector {
    
    public static void inject(Finder finder, final com.example.butterknife.SimpleActivity target, Object source) {
        View view;
        view = finder.findRequiredView(source, 2131230759, "field 'title'");
        target.title = (android.widget.TextView) view;
        view = finder.findRequiredView(source, 2131230783, "field 'subtitle'");
        target.subtitle = (android.widget.TextView) view;
        view = finder.findRequiredView(source, 2131230784, "field 'hello', method 'sayHello', and method 'sayGetOffMe'");
        target.hello = (android.widget.Button) view;
        view.setOnClickListener(
                new butterknife.internal.DebouncingOnClickListener() {
                    @Override
                    public void doClick(
                            android.view.View p0
                    ) {
                        target.sayHello();
                    }
                });
        view.setOnLongClickListener(
                new android.view.View.OnLongClickListener() {
                    @Override
                    public boolean onLongClick(
                            android.view.View p0
                    ) {
                        return target.sayGetOffMe();
                    }
                });
        view = finder.findRequiredView(source, 2131230785, "field 'listOfThings' and method 'onItemClick'");
        target.listOfThings = (android.widget.ListView) view;
        ((android.widget.AdapterView<?>) view).setOnItemClickListener(
                new android.widget.AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(
                            android.widget.AdapterView<?> p0,
                            android.view.View p1,
                            int p2,
                            long p3
                    ) {
                        target.onItemClick(p2);
                    }
                });
        view = finder.findRequiredView(source, 2131230786, "field 'footer'");
        target.footer = (android.widget.TextView) view;
    }

    public static void reset(com.example.butterknife.SimpleActivity target) {
        target.title = null;
        target.subtitle = null;
        target.hello = null;
        target.listOfThings = null;
        target.footer = null;
    }
}

这个自动生成的代码里面,只有2个方法,一个是inject,一个是reset,顾名思义,一个是进行初始化操作,一个是释放操作。

发现原来findviewByid这些代码是自动生成的,不是程序在运行的时候,查找注解,然后动态执行findViewById操作,同理OnClick这些事件的绑定也是一样。


所以我们有理由相信在oncreate中执行了ButterKnife.inject(this);那么会再主动调用

SimpleActivity$$ViewInjecort.inject();

这个方法。


以上大致是butterknife的简单原理,至于编译器怎么生成的这些代码,可以参考Java Apt相关知识。在此不多做讲解。



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值