Search code examples
androidscrollviewmaterial-designgoogleio

Fix an item in a ObservableScrollView Android


I'm trying to fix an item in a ScrollView when it hit the ActionBar. It stop correctly but when i return to main scroll position, i get a random margin above the layout (Screenshot 3). (The margin in Screenshot 2 is not a bug)

Problem screenshot

My ObservableScrollView listener

if(t > stackHeight && !animationActionBarstackedBg){
                animationActionBarstackedBg = true;
            } else if (t < stackHeight && animationActionBarstackedBg){
                animationActionBarstackedBg = false;
            }

            frameImg.setPadding(frameImg.getPaddingLeft(), t/2, frameImg.getPaddingRight(), frameImg.getPaddingBottom());

            if(animationActionBarstackedBg){
                panelTitleContainer.setPadding(panelTitleContainer.getPaddingLeft(), t-stackHeight, panelTitleContainer.getPaddingRight(), panelTitleContainer.getPaddingBottom());
            }

Where t is the ScrollY, stackHeight is the image height, frameImg is a FrameLayout containing an ImageView (to make a Google I/O 2014 like animation) and panelTitleContainer is a layout which contains the brown layout.

There is a way to fix an item in a scroll view?


Solution

  • Solved by using setTranslationY()

    if(t > stackHeight && !animationActionBarstackedBg){
        animationActionBarstackedBg = true;
    } else if (t < stackHeight && animationActionBarstackedBg){
        animationActionBarstackedBg = false;
    }
    
    frameImg.setTranslationY(svMain.getScrollY() * 0.5f);
    
    if(!animationActionBarstackedBg){
        panelTitleContainer.setTranslationY(0);
    } else {
        panelTitleContainer.setTranslationY(svMain.getScrollY() - stackHeight);
    }