Scroll to a specific view in scroll view

前端 未结 15 1481
醉梦人生
醉梦人生 2020-12-04 14:08

I have added a scrollview and the subchilds inside the scrollview. At some point i need to scroll to a specific view.



1. 

        
15条回答
  •  天涯浪人
    2020-12-04 14:39

    Use :

    final  ScrollView scrollView= (ScrollView) 
    int speed=1000;
    findViewById(R.id.main_scrollView);
    final View view = findViewById(R.id.your_view); 
    ViewTreeObserver vto = view.getViewTreeObserver();
    vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
                @Override
                public void onGlobalLayout() { view.getViewTreeObserver().removeOnGlobalLayoutListener(this);   
                    ObjectAnimator.ofInt(scrollView, "scrollY",  (int) view.getY()).setDuration(speed).start();
                }
            }); 
    

提交回复
热议问题