Android sticky header RecyclerView/ Section Header RecyclerView
public void onScrolled(int dx, int dy)
those are the two arguments that you're receiving onScrolled
, that means, the number of pixels that the RecyclerView changed on the X and Y axis... so probably all you want to do is:
@Override
public void onScrolled(int dx, int dy) {
if(dx < 0) // going up
showSitckyHeader();
}
you can probably further improve this implementation by adding a minimum scroll amount. Something like:
int totalScrolled = 0;
@Override
public void onScrolled(int dx, int dy) {
totalScrolled += dx;
if(totalScrolled < MIN_SCROLL)
showSitckyHeader();
if(dx > 0)
totalScrolled = 0;
}
@Override
public void onScrollStateChanged(int newState) {
if(newState == SCROLL_STATE_IDLE || newState = SCROLL_STATE_SETTLING)
totalScrolled = 0;
}
or even go further and implement speed, counting time, but those types of implementation are more tricky, and you have to test it yourself.
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.