Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 574
Sid Avatar asked Dec 24 '15 01:12

Sid


2 Answers

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
     }
}
like image 108
thiagolr Avatar answered Nov 09 '22 18:11

thiagolr


Use StickyHeaderRecyclerView library

It is very easy to use

like image 30
Bhawna Raheja Avatar answered Nov 09 '22 17:11

Bhawna Raheja