How to add a dynamic view in between items of RecyclerView in android?

Sid picture Sid · Dec 24, 2015 · Viewed 11.6k times · Source

I need to add a small strip in between items of a RecyclerView. This strip can come after different number of items in a list. This needs to be done dynamically. I need to implement something like what FitBit has done:enter image description here

I also need the first row i.e. the one saying "This Week" to stick on top even if the page scrolls down.

Answer

thiagolr picture thiagolr · Jan 13, 2016

You should use the concept of different view types using getItemViewType(int). Then on onCreateViewHolder(ViewGroup, int) you can check which type you should inflate/create.

Example:

@Override
public int getItemViewType(int position) {
    // you should return the view type, based on your own dynamic logic
}

@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
     switch (viewType) {
         // handle each view type accordingly
     }
}