Implementing RecyclerView with Sectioned Header based on Category

I am trying to implement RecyclerView with custom sectioned header based on list’s category. I already have a list that’s sorted based on their category, so I want to inflate header if and only if one row’s category is different from preceding row’s category. I’ve been figuring out how to do it but something bothering me here. It worked, but does not display the right row. I think it’s because of the item count which is increased due to the header. Can anyone help me?

Here is my adapter :

List<Notes> notes;
OnItemClickListener mItemClickListener;
private static final int TYPE_CATEGOORY = 0;
private static final int TYPE_ITEM = 1;
String changing = "";

public OneNoteAdapter(List<Notes> logs) {
    this.notes = logs;
}

@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    if(viewType == TYPE_CATEGOORY){
        View itemView = LayoutInflater.
                from(parent.getContext()).
                inflate(R.layout.category_header, parent, false);
        return new HeaderViewHolder(itemView);
    }else{
        View itemView = LayoutInflater.
                from(parent.getContext()).
                inflate(R.layout.learn_card, parent, false);
        return new ItemViewHolder(itemView);
    }

}

@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
    if(holder instanceof HeaderViewHolder){
        Notes note = notes.get(position);
        ((HeaderViewHolder) holder).vCategoryText.setText(note.category);
    }else {
        ItemViewHolder item = (ItemViewHolder) holder;
        Notes note = notes.get(position);
        item.vNotesNote.setText(note.notes);
        item.vNotesCat.setText(note.category);
    }
}

@Override
public int getItemViewType(int position) {
    if((!(notes.get(position).category).equals(changing))){
        changing = notes.get(position).category;
        return TYPE_CATEGOORY;
    }else{
        return TYPE_ITEM;
    }
}

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



public class ItemViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{

    protected TextView vNotesNote, vNotesCat;

    private final Context context;

    public ItemViewHolder(View itemView) {
        super(itemView);
        vNotesNote = (TextView) itemView.findViewById(R.id.notes_notes);
        vNotesCat = (TextView) itemView.findViewById(R.id.notes_category);
        context = itemView.getContext();
        itemView.setOnClickListener(this);
    }


    @Override
    public void onClick(View v) {
        if(mItemClickListener != null){
            mItemClickListener.onItemClick(v, getPosition());
        }
    }
}

public class HeaderViewHolder extends RecyclerView.ViewHolder {

    protected TextView vCategoryText;

    public HeaderViewHolder(View itemView) {
        super(itemView);
        vCategoryText = (TextView) itemView.findViewById(R.id.category_header_text);
    }
}

public interface OnItemClickListener{
    public void onItemClick(View v, int poition);
}

public void setOnItemClickListener (final OnItemClickListener mItemClickListener){
    this.mItemClickListener = mItemClickListener;
}

}

Thank You

Answer

Try to replace your onBindViewHolder and getItemViewType methods with following:

@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
    if (holder instanceof HeaderViewHolder) {
        ((HeaderViewHolder) holder).vCategoryText.setText(notes.get(position + 1).category);
    } else {
        ItemViewHolder item = (ItemViewHolder) holder;
        Notes note = notes.get(position);
        item.vNotesNote.setText(note.notes);
        item.vNotesCat.setText(note.category);

        if (position + 1 < notes.size() &&
            !notes.get(position + 1).category.equals(note.category)) {
            notes.add(position + 1, null);
        }
    }
}

@Override
public int getItemViewType(int position) {
    if (notes.get(position) == null) {
        return TYPE_CATEGOORY;
    } else {
        return TYPE_ITEM;
    }
}

And you can delete String changing = "";.