Bui Quang Huy
Bui Quang Huy

Reputation: 1794

Custom item of RecyclerView

I want to sort my item of RecyclerView by alphabet like this:

enter image description here

So could anybody let me know how to make the row has SECTION smaller than the other. Will we custom it in class Decoration?

Upvotes: 1

Views: 1240

Answers (2)

Tom
Tom

Reputation: 7834

Use getItemViewType to determine what type of view a position should hold.

public int getItemViewType(final int position) {
    if (dataset.get(position).isHeader()) {
        return TYPE_HEADER;
    } else {
        return TYPE_ITEM;
    }
}

Then create the correct ViewHolder in onCreateViewHolder.

public RecyclerView.ViewHolder onCreateViewHolder(final ViewGroup parent, final int viewType) {
    if (viewType == TYPE_HEADER) {
        return HeaderViewHolder.create(parent);
    }
    else {
        return ItemViewHolder.create(parent);
    }
}

Make sure that you bind the correct data in onBindViewHolder.

public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int position) {
    if (getItemViewType(position) == TYPE_HEADER) {
        ((HeaderViewHolder) holder).bind(/* your heading data for this position */);
    } else {
        ((ItemViewHolder) holder).bind(/* your item data for this position */);
    }
}

If it's not clear - create your ViewHolders to inflate separate layouts. Your header view holder will inflate the thinner layout with different background etc.

Upvotes: 2

bhumika rijiya
bhumika rijiya

Reputation: 440

you should use this library,

https://github.com/timehop/sticky-headers-recyclerview

I hope you get what you want from this link.

Upvotes: 1

Related Questions