Add a sticky header to parallax scrolling - android

Brendan Henry picture Brendan Henry · Jan 20, 2015 · Viewed 8.8k times · Source

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.

Answer

Kavis picture Kavis · Jan 20, 2015

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.