自定义适配器中的黄油刀视图支架模式

Butter Knife view holder pattern in custom adapter

我正在尝试在自定义适配器中使用 Butter Knife,如文档所宣传的那样:http://jakewharton.github.io/butterknife/ 我粘贴了示例代码并使用我的 RelativeLayout 文件插入了布局:

  @Override public View getView(int position, View view, ViewGroup parent) {
    ViewHolder holder;
    ButterKnife.setDebug(true);
    if (view != null) {
      holder = (ViewHolder) view.getTag();
    } else {
      view = inflater.inflate(R.layout.list_row_people, parent, false);
      holder = new ViewHolder(view);
      view.setTag(holder);
    }

    holder.name.setText("John Doe");
    // etc...

    return view;
  }

这是 ViewHolder 的代码(内联 class of PeopleAdapter):

static class ViewHolder {
    @BindView(R.id.name)
    TextView name;

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

不幸的是,holder.name.setText("John Doe"); 抛出 NullPointer 异常,因为 name 为空。调试输出显示这些行:

13:11:32.816 11613-11613/com.myproject.debug D/ButterKnife: Looking up view binder for com.myproject.controller.adapters.PeopleAdapter$ViewHolder
13:11:32.827 11613-11613/com.myproject.debug D/ButterKnife: Not found. Trying superclass java.lang.Object
13:11:32.827 11613-11613/com.myproject.debug D/ButterKnife: MISS: Reached framework class. Abandoning search.

list_row_people.xml 目前看起来是这样的:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <TextView
        android:id="@+id/name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:gravity="center_vertical"
        android:padding="4dp"
        android:text="@string/example_name"
        android:textAppearance="?android:attr/textAppearanceLarge" />
</RelativeLayout>

阅读 https://github.com/JakeWharton/butterknife/issues/285,我将以下行添加到我的 proguard-rules.pro:

-keep class **$$ViewBinder { *; }

编辑:

Android Studio 2.1 与 com.jakewharton:butterknife:8.0.1。这是我的 build.gradle:

apply plugin: 'com.android.application'
apply plugin: 'android-apt'
apply plugin: 'kotlin-android'

repositories {
    mavenCentral()
}

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.3"
    defaultConfig {
        applicationId "com.myproject"
        minSdkVersion 18
        targetSdkVersion 23
        versionCode 18
        versionName "0.0.16"
        multiDexEnabled true
    }
    packagingOptions {
        exclude 'main/AndroidManifest.xml'
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
        debug {
            applicationIdSuffix ".debug"
        }
    }
    lintOptions {
        abortOnError false
        warning 'InvalidPackage'
    }
    sourceSets {
        main.java.srcDirs += 'src/main/kotlin'
    }
    compileOptions {
        targetCompatibility JavaVersion.VERSION_1_7
        sourceCompatibility JavaVersion.VERSION_1_7
    }
}

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    wearApp project(':wear')
    testCompile 'junit:junit:4.12'
    apt 'com.github.hotchemi:permissionsdispatcher-processor:2.1.2'
    releaseCompile project(path: ':common', configuration: 'release')
    debugCompile project(path: ':common', configuration: 'debug')
    compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
    compile 'com.google.android.gms:play-services-maps:8.4.0'
    compile 'com.google.maps.android:android-maps-utils:0.4.3'
    compile 'com.google.android.gms:play-services-wearable:8.4.0'
    compile 'com.android.support:appcompat-v7:23.3.0'
    compile 'com.android.support:support-v4:23.3.0'
    compile 'com.android.support:design:23.3.0'
    compile 'com.android.support:recyclerview-v7:23.3.0'
    compile 'com.google.code.gson:gson:2.6.2'
    compile 'com.jakewharton.timber:timber:2.5.0'
    compile 'com.jakewharton:butterknife:8.0.1'
    compile 'com.github.hotchemi:permissionsdispatcher:2.1.2'
    compile 'com.squareup.picasso:picasso:2.5.2'
}
buildscript {
    ext.kotlin_version = '1.0.1-2'
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
    }
}

在版本 8.0.0 的黄油刀中。

The Runtime and compiler are now split into two artifacts.

compile 'com.jakewharton:butterknife:8.0.0'
apt 'com.jakewharton:butterknife-compiler:8.0.0'

也看看这个Link