Butterknife 无法在我的适配器内绑定 Class

Butterknife is unable to bind inside my Adapter Class

我有一个适配器可以为我的导航抽屉绘制布局。我的导航抽屉包含两个内部 xml 文件:一个是 Header,另一个是 Row。我在单个适配器中绘制这些,但是当我尝试在我的 header 上 setText() 时,我无法绑定。这是我的适配器 class:

public class DrawerAdapter extends RecyclerView.Adapter<DrawerAdapter.ViewHolder> {

private static final int HEADER_TYPE = 0;
private static final int ROW_TYPE = 1;
private static Context context;
private static DatabaseHelper databaseHelper;

private List<String> rows;
private List<Integer> icons;
private String driverName;

public DrawerAdapter(Context context, List<String> rows, List<Integer> icons, String driverName, DatabaseHelper databaseHelper) {
    this.icons = icons;
    this.rows = rows;
    this.context = context;
    this.driverName = driverName;
    this.databaseHelper = databaseHelper;
}

@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    if (viewType == HEADER_TYPE) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.drawer_header, parent, false);
        return new ViewHolder(view, viewType);

    } else if (viewType == ROW_TYPE) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.drawer_row, parent, false);
        return new ViewHolder(view, viewType);
    }
    return null;
}

@Override
public void onBindViewHolder(ViewHolder holder, int position) {
    if (holder.viewType == ROW_TYPE) {
        String rowText = rows.get(position - 1);
        int imageView = icons.get(position - 1);
        holder.textView.setText(rowText);
        holder.imageView.setImageResource(imageView);
    } else if (holder.viewType == HEADER_TYPE) {
        holder.driverNameText.setText(driverName);
    }
}

@Override
public int getItemCount() {
    return rows.size() + 1;
}

@Override
public int getItemViewType(int position) {
    if (position == 0)
        return HEADER_TYPE;
    return ROW_TYPE;
}

public static class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
    protected int viewType;
    @Bind(R.id.drawer_row_icon)
    ImageView imageView;
    @Bind(R.id.drawer_row_text)
    TextView textView;
    @Bind(R.id.drawer_row_id)
    FrameLayout listRow;
    @Bind(R.id.driverName)
    TextView driverNameText;

    public ViewHolder(View itemView, int viewType) {
        super(itemView);

        this.viewType = viewType;

        if (viewType == ROW_TYPE) {
            ButterKnife.bind(this, itemView);
            imageView.setOnClickListener(this);
            textView.setOnClickListener(this);
            listRow.setOnClickListener(this);
        } else {
            ButterKnife.bind(this, itemView);
        }
    }


 }

正如您在我的 onCreateViewHolder 方法中看到的那样,我正在检查两个 viewType,以便我知道要绘制哪个布局。这反过来将为 ViewHolder class 创建一个新的 object,其中我 "TRY" 根据 [=16= 绑定我的 xml 中的元素].我是漏掉了什么,还是做错了什么?

您绑定了两次。您不能在同一视图上两次调用绑定。它只是在进行您的 findviewbyid 调用。在您的 if 块上方调用 bind 一次。

public ViewHolder(View itemView, int viewType) {
    super(itemView);

    this.viewType = viewType;

    ButterKnife.bind(this, itemView);
    if (viewType == ROW_TYPE) {
        imageView.setOnClickListener(this);
        textView.setOnClickListener(this);
        listRow.setOnClickListener(this);
    }
}

我通过执行以下操作解决了这个问题:

首先,api 文档说明如下

Be default, views are required to be present in the layout for both field and method bindings. If a view is optional add a @Nullable annotation such as the one in the support-annotations library.

@Nullable @Bind(R.id.title) TextView subtitleView;

http://jakewharton.github.io/butterknife/javadoc/

现在因为我要绑定来自不同 xml 文件的不同元素,我不得不将它们标记为 @Nullable,因为它们可能一开始就不可绑定。我更改了我的代码以执行以下操作:

@Nullable @Bind(R.id.drawer_row_icon)
ImageView imageView;
@Nullable @Bind(R.id.drawer_row_text)
TextView textView;
@Nullable @Bind(R.id.drawer_row_id)
FrameLayout listRow;
@Nullable @Bind(R.id.driverName)
TextView driverNameText;

同时将 ButterKnife.bind() 移出我的 IF-ELSE 街区。