Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add a sticky header to parallax scrolling - android

I want to have a parallax scroll in my application much like the spotify app with the 'sticky' header. This means the header will be pinned to the top of the screen. I've found plenty of ScrollView libraries which do these functions separately, I can't find any libraries with do both.

I am using the ParallaxScroll library for the parallex scroll and StickyScrollViewItems to stick the item to the top of the screen.

Any help is much appreciated.

like image 695
Brendan Henry Avatar asked Jan 20 '15 10:01

Brendan Henry


1 Answers

visit https://github.com/ksoichiro/Android-ObservableScrollView

If you don't want to use library, you can just get logic of making header sticky from here :-

@Override 
 public void onScrollChanged(int scrollY, boolean firstScroll, boolean dragging) {
        if (dragging) {
            int toolbarHeight = mToolbarView.getHeight();
            if (firstScroll) {
                float currentHeaderTranslationY = ViewHelper.getTranslationY(mHeaderView);
                if (-toolbarHeight < currentHeaderTranslationY) {
                    mBaseTranslationY = scrollY;
                } 
            } 
            float headerTranslationY = ScrollUtils.getFloat(-(scrollY - mBaseTranslationY), -toolbarHeight, 0);
            ViewPropertyAnimator.animate(mHeaderView).cancel();
            ViewHelper.setTranslationY(mHeaderView, headerTranslationY);
        } 
    } 

/// this is the key method to make view sticky.

setTranslationY(float translationY)

Sets the vertical location of this view relative to its top position.

like image 173
Kavis Avatar answered Sep 24 '22 00:09

Kavis