注入類型的Android框架Butterknife示例和原理

MarWhitcomb 10年前發布 | 29K 次閱讀 Android開發 移動開發

來自: http://blog.csdn.net//guijiaoba/article/details/43020059


最近使用了一個注入類型的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相關知識。在此不多做講解。



 本文由用戶 MarWhitcomb 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!