János János - 1 month ago 8
iOS Question

Possible do define sections in RecyclerView in Android?

In iOS

could have 'sections' and inside a section 'rows'. Is this possible in Android with

has only a
delegate method. But nothing for sections. What alternatives you can suggest?

I thought to put the section headers into a row, and melt the sections into one global section, and so no section will need. Any better idea?


RecyclerView.Adapter supports multiple view types out of the box. It's up to you which item type you want to display at any particular position. You can easily use it to create sections, for example if you define two item types TYPE_HEADER and TYPE_ITEM:

public int getItemViewType(int position) {
    if (isHeader(position)) {
        return TYPE_HEADER;
    } else {
        return TYPE_ITEM;

Then, you can use onCreateViewHolder to inflate proper layouts and onBindViewHolder to make use of it.

public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    if (viewType == TYPE_HEADER) {
        View v = ... ; // inflate here
        return new HeaderViewHolder(v);
    } else {
        View v = ... ; // inflate here
        return new ItemViewHolder(v);

public void onBindViewHolder(ViewHolder holder, int position) {
    if (holder instanceof HeaderViewHolder) {
        // handle header
    } else if (holder instanceof ItemViewHolder {
        // handle item

The only thing left is data structure to keep stuff. There are many ways to implement it and I think it's a little bit out of the scope of your question. If data structure is very deep I prefer using custom tree-like structure I made some time ago but sometimes I use flat List and check item types using instanceof.