带有RecyclerView的标题TextView

时间:2015-06-18 07:25:05

标签: android android-recyclerview

我正在使用RecyclerView + CardView来显示文字列表现在我正在尝试将TextView标题添加到RecyclerView的顶部,所以我有适配器内的两个布局,但现在它在应用程序中显示如下:

enter image description here

它必须如下所示:

enter image description here

问题可能在于getItemCountgetItemViewType我必须更改的内容,因此标题只显示一次后跟CardViews

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

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

List<AdapterData> mItems;

public Adapter() {
    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, parent, false));

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

    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 :(得分:3)

在getItemViewType()中试试这个

@Override
public int getItemViewType(int position) {
   return (position==0) ? 0 : 1;   //try this line
}