How to Make Sticky Headers in Recyclerview? (Without External Lib)

How can I make sticky headers in RecyclerView? (with or without external lib)

You don't need to use any third party libraries. You can make use of ExpandableListView without actually making it "expand and collapse" to do the exact same thing which you need. See my answer for this post. The advantage here is that you can deal with this as easily as you deal with an ExpandableListView, with no custom code. You only need to add one line to what is otherwise a standard ExpandableListView adapter.

@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
...
((ExpandableListView) parent).expandGroup(groupPosition);
...
}

Your section headers, you can set as a group view and the list items as children. Your data structure need to be updated before being passed on to the adapter. It need to be a grouped data, not a plain list which you have to pass to the expandable adapter(for example an array of classes, each instance that contain a String property for group header and an ArrayList of IncomeExpense objects). And when you update the data, make the update in the corresponding group, instead of the entire data.

Android sticky header RecyclerView/ Section Header RecyclerView

Since the previous answers don't provide a reliable solution, I propose my FlexibleAdapter library for RecyclerView, that is able to handle all following functionalities at once:

  • Sticky functionality for headers with Sections, works with all 3 LayoutManagers and with ViewPager too.
  • Selection Modes.
  • Multiple item types with auto-mapping.
  • Predefined ViewHolders.
  • Expandable items with Selection Coherence.
  • Draggable and Swipe-To-Dismiss.
  • Animated Async Filter with spannable text.
  • Scrolling Animations.
  • EndlessScroll with Adapter binding.
  • UndoHelper & ActionMode Helper.
  • FastScroller.
  • ...and more.

The idea behind is to avoid to create from scratch over again a custom Adapter for every project and to have more functionalities in one library, instead of relying on different libraries that support only 1 or 3 of them and that you cannot merge.



Related Topics



Leave a reply



Submit