RecyclerView有两个布局

时间:2015-06-17 11:37:05

标签: android android-recyclerview

我正在尝试将一个Header TextView放在我的RecyclerView的顶部,所以我在RecyclerView中放了两个布局我已经按照一些教程尝试将代码修改到这里,但我无法正确使用它

在onCreateViewHolder下,它给了我一个无法解析inflate(int)所以我应该怎么称呼它

有人可以告诉我我做错了吗

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

private static final int LAYOUT_ONE  = 0;
private static final int LAYOUT_TWO = 1;

List<AdapterData> mItems;

public TheSimsAdapter() {
    super();
    mItems = new ArrayList<>();
    AdapterData data = new AdapterData();
    data.setCode("dummytext");
    data.setResult("dummytext");
    mItems.add(data);

    data = new AdapterData();
    data.setCode("dummytext");
    data.setResult("dummytext");
    mItems.add(data);

    data = new AdapterData();
    data.setCode("dummytext");
    data.setResult("dummytext");
    mItems.add(data);

    data = new AdapterData();
    data.setCode("dummytext");
    data.setResult("dummytext");
    mItems.add(data);

    data = new AdapterData();
    data.setCode("dummytext");
    data.setResult("dummytext");
    mItems.add(data);

}

@Override
public int getItemViewType(int position) {
    return position % 2;
}

@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    switch ( viewType ) {

        case LAYOUT_ONE: return new ViewHolderFirst(LayoutInflater.from(parent.getContext()).inflate(R.layout.top_textview));

        case LAYOUT_TWO: return new ViewHolderSecond(LayoutInflater.from(parent.getContext()).inflate(R.layout.recycler_view_card_item));
    }

    return null;
}

@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
    switch (getItemViewType(position)) {

        case LAYOUT_ONE:
            ViewHolderFirst viewHolderFirst = (ViewHolderFirst)holder;

            break;

        case LAYOUT_TWO:
            ViewHolderSecond viewHolderSecond = (ViewHolderSecond)holder;
            AdapterData data = mItems.get(position);
            viewHolderSecond.code.setText(data.getCode());
            viewHolderSecond.result.setText(data.getResult());

            break;
    }
}

@Override
public int getItemCount() {
    return mItems.size();
}



class ViewHolderFirst extends RecyclerView.ViewHolder {
    public TextView header;

    public ViewHolderFirst(View itemView) {
        super(itemView);
        header = (TextView)itemView.findViewById(R.id.header_textview);

    }
}

class ViewHolderSecond extends RecyclerView.ViewHolder{
    public TextView code;
    public TextView result;

    public ViewHolderSecond(View itemView) {
        super(itemView);
        code = (TextView)itemView.findViewById(R.id.sims_code);
        result = (TextView)itemView.findViewById(R.id.sims_result);

    }
}


}

1 个答案:

答案 0 :(得分:0)

尝试使用

LayoutInflater.from(parent.getContext()).inflate(R.layout.top_textview, parent, false)
在你的createViewHolder方法中