Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set the starting position of a ScrollView?

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 scroll View.

Thanks,

Ryan

like image 308
RyanCW Avatar asked Mar 10 '14 17:03

RyanCW


People also ask

What is the difference between NestedScrollView and ScrollView?

NestedScrollView is just like ScrollView , but it supports acting as both a nested scrolling parent and child on both new and old versions of Android. Nested scrolling is enabled by default.

Can ScrollView be horizontal?

In android, You can scroll the elements or views in both vertical and horizontal directions.

What is ScrollView layout?

In Android, a ScrollView is a view group that is used to make vertically scrollable views. A scroll view contains a single direct child only. In order to place multiple views in the scroll view, one needs to make a view group(like LinearLayout) as a direct child and then we can define many views inside it.

What is scrollEventThrottle?

scrollEventThrottle: It is used to control the firing of scroll events while scrolling.


1 Answers

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
like image 66
Philipp Jahoda Avatar answered Oct 06 '22 19:10

Philipp Jahoda