Butterknife无法在我的Adapter类中绑定

时间:2015-07-16 07:32:04

标签: android butterknife

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

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类创建一个新对象,我在其中“尝试”根据viewType绑定xml中的元素。我错过了什么,或做错了什么?

2 个答案:

答案 0 :(得分:24)

通过执行以下操作,我能够解决此问题:

首先,api文档声明如下

  

默认情况下,视图需要存在于字段和方法绑定的布局中。如果视图是可选的,则添加@Nullable注释,例如support-annotations库中的注释。

@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阻止之外。

答案 1 :(得分:6)

你有两次约束力。您不能在同一视图上调用两次绑定。它只是在执行你的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);
    }
}