How to set the starting position of a ScrollView?

后端 未结 6 1633
野趣味
野趣味 2021-01-07 23:36

I have been trying to set the initial position of a scroll View but have not found a way. Does anyone have any ideas? Also I have a GoogleMaps fragment as a children of the

6条回答
  •  青春惊慌失措
    2021-01-08 00:08

    Yes, that is possible:

    ScrollView.scrollTo(int x, int y);
    ScrollView.smoothScrollTo(int x, int y);
    ScrollView.smoothScrollBy(int x, int y);
    

    can be used for that. The x and y parameters are the coordinates to scroll to on the horizontal and vertical axis.

    Example in code:

        @Override   
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.your_layout);
    
            ScrollView sv = (ScrollView) findViewById(R.id.scrollView);
            sv.scrollTo(0, 100);
         }
    

    In that example, once the Activity is started, your ScrollView will be scrolled down 100 pixels.

    You can also try to delay the scrolling process:

    final ScrollView sv = (ScrollView) findViewById(R.id.scrollView);
    
    Handler h = new Handler();
    
    h.postDelayed(new Runnable() {
    
        @Override
        public void run() {
            sv.scrollTo(0, 100);            
        }
    }, 250); // 250 ms delay
    

提交回复
热议问题