Harshil Pansare
Harshil Pansare

Reputation: 1147

Android: Scroll in a NestedScrollView to align a View to visible screen top

I have expandable views in my layout like this. enter image description here

Suppose I click on education, I need my NestedScrollView to move such that Education CardView should align to visible top of layout/screen.

I tried scrollView.smoothScrollTo(view.getTop()); It's as if the function is not even getting called.

Code where I am calling smoothScrollTo()

@OnClick(R.id.ip_expand_experience)
void expandExperiences(){
    if(!experiencesExpanded){
        AnimationUtils.expand(experiencesCard,context,CARD_HEIGHT_COLLAPSED);
        experiencesExpanded=true;
        focusOnView(experiencesCard);
    }else{
        AnimationUtils.collapse(experiencesCard,context,CARD_HEIGHT_COLLAPSED);
        experiencesExpanded=false;
    }

}

private void focusOnView(final View view){
    scrollView.post(new Runnable() {
        @Override
        public void run() {
            scrollView.smoothScrollTo(0, view.getTop());
        }
    });
}

Upvotes: 1

Views: 851

Answers (1)

AskNilesh
AskNilesh

Reputation: 69671

try this

 your_scrollview.post(new Runnable() {
        @Override
        public void run() {
            your_scrollview.scrollTo(0, yourview.getBottom());
        }
    });

Upvotes: 1

Related Questions