paetztm/recycler_view_headers

Push header rather than slide over

blkxltng opened this issue · 3 comments

Hello,

This isn't really an issue, per say, but I was just wondering if you had any advice for how to get the "new" header to push the "old" header up out of the way rather than simply sliding over it, kind of like this:
https://i.stack.imgur.com/ghBKV.gif

Thanks,
Max

Sevastyan explains how he does this here: https://stackoverflow.com/a/44327350/1236327

Are you asking me to explain his code?

Not at all. I was wanted to know if you knew what to look at to get it working with your code. I was able to figure it out though. I'll post another comment with the changes a bit later :).

My implementation was definitely assisted by Sevastyan's code, but here's what I did to get things pushing :D. In between the final int position = parent.getChildAdapterPosition(child); and CharSequence title = sectionCallback.getSectionHeader(position); lines, I placed this code:

//This makes the top header get "pushed" by the one below it
if(sticky) {
    int contactPoint = headerView.getBottom();
    if(sectionCallback.isSection(parent.getChildAdapterPosition(child))) {
        if(child.getTop() <= (contactPoint*2) && child.getTop() > contactPoint) {
                moveHeader(c, child, headerView);
        }
    }
}

Here's the code for the moveHeader method that I used, which was inspired by Sevastyan:

private void moveHeader(Canvas c, View nextHeader, View headerView) {
    c.save();
    c.translate(0, nextHeader.getTop() - headerView.getHeight()*2); //Pushes the top header out
    headerView.draw(c);
    c.restore();
}

After adding these things, the headers were able to "push" the top header out of the way instead of simply sliding over. I hope this helps anyone who's trying to do the same thing.