Annotation注解APT(五):注入框架ButterKnife

引言
作为一个程序员,特别是一个app程序员,在写代码的时候,很大部分时间都花在布局的编写及控件的初始化和事件监听方法上了,这样就没有时间来处理代码逻辑和结构了,现在ButterKnife框架可以为你节省很多时间,真的很节省的.

Android Studio中使用ButterKnife
1.可以直接在build.gradle中配置
top build.gradle

classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'

app build.gradle

apply plugin: 'com.neenbedankt.android-apt'
dependencies {
    ...
    compile 'com.jakewharton:butterknife:8.4.0'
    apt 'com.jakewharton:butterknife-compiler:8.4.0'
}

1.也可以通过File->Project Structure -> app -> Dependencides-> + 搜索butterknife的最新版本.

然后就可以使用了:

package com.example.fishmov.daggerdemo;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.Button;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;


public class MainActivity extends AppCompatActivity {
    private static final String TAG = "fish---";
    protected Unbinder unbinder;

    @BindView(R.id.btn)
    Button button;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        unbinder = ButterKnife.bind(this);

        Log.i(TAG, "" +  button);
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        unbinder.unbind();
    }
}

注意:一定要在setContentView(R.layout.activity_main)后进行ButterKnife.bind(this);否则将报异常,当然我们会在onDestroy中进行unbinder.unbind().

对于这种处理,我们在app中可以采用一个基类的做法,避免重复编码,比如:

Activity

package com.example.fishmov.daggerdemo;

import android.os.Bundle;
import android.os.PersistableBundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;

import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * Created by fishmov on 17-6-22.
 */

public abstract class BaseActivity extends AppCompatActivity {

    protected abstract int getContentViewId();
    protected abstract void initView();
    protected Unbinder unbinder;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(getContentViewId());
        unbinder = ButterKnife.bind(this);
        initView();
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (null != unbinder) {
            unbinder.unbind();
        }
    }
}

Fragment

package com.example.fishmov.daggerdemo;

import android.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * Created by fishmov on 17-6-22.
 */

public abstract class BaseFragment extends Fragment {

    protected abstract int getContentViewId();
    protected abstract void initView();
    protected Unbinder unbinder;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(getContentViewId(), container, false);
        unbinder = ButterKnife.bind(this, view);
        return view;
    }


    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initView();
    }

    @Override
    public void onDestroyView() {
        if (null != unbinder) {
            unbinder.unbind();
        }
        super.onDestroyView();
    }
}

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

由于butterknife的用法比较简单,也很好理解,后面将不再以实例说明,只提供相关用法:

@BindView(R.id.title) TextView title;
@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

一篇很好的文章:http://blog.csdn.net/itjianghuxiaoxiong/article/details/50177549
butterknife官方说明:http://jakewharton.github.io/butterknife/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

fishmov

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值