9

For Example, I have a layout like this:

<ScrollView>
  <LinearLayout>
    <LinearLayout>
         ...
    </LinearLayout>
    <LinearLayout>
         ...
    </LinearLayout>
    <LinearLayout>
         <TextView>
         </TextView>
    </LinearLayout>
  </LinearLayout>
</ScrollView> 

My TextView have long content. Now I can scroll the ScrollView manually to the end of TextView content. In the code, I write: scrview.scrollTo(0, 800);, with scrview is my ScrollView. It supposed to scroll to the middle of the TextView content. But when running, it only scrolled to the start of the TextView and can't scroll through the TextView content.

Does anybody know what causing this problems?

EDIT: I found it. Seems like I call scrollTo too soon. Using

scrview.post(new Runnable() {
                public void run() {
                    scrview.scrollTo(0, 800);
                }
            }); 

instead and it works. Posted the answer for anyone getting same problem.

user1417127
  • 1,525
  • 1
  • 21
  • 29

3 Answers3

22

I found it. Seems like I call scrollTo too soon. Using

scrview.post(new Runnable() {
                public void run() {
                    scrview.scrollTo(0, 800);
                }
            }); 

instead and it works. Posted the answer for anyone getting same problem.

user1417127
  • 1,525
  • 1
  • 21
  • 29
  • 1
    800 milliseconds is a long time to wait. Is it really necessary and is there a way to do this without picking an arbitrary time delay that may be too long or short? – Edwin Evans Dec 03 '14 at 02:29
  • @EdwinEvans [this](http://stackoverflow.com/a/18930335/4348328) answer solved a related issue for me without the need for arbitrary delay. – mpkuth Jul 21 '15 at 21:20
2

First of all, if all your LinearLayouts have the same orientation, why you don't simply create it only once? If I'm not wrong, ScrollView can only have 1 child (1 direct child). Maybe that's probably the reason of your problems. However, if you still want those 3 LinearLayouts, you can create a FrameLayout as a parent to hold them and make that FrameLayout the child of the ScrollView

gian1200
  • 3,670
  • 2
  • 30
  • 59
  • My bad. My question is not clear enough. This is just the sample code for everyone easy to understand so I forgot some detail. I've already have a parent view hold all the view inside ScrollView. And this problems still occurred. I edited my question – user1417127 Jun 28 '12 at 08:06
0

You have to call to scrollTo inside a runnable because the scrollview has not beed layouted yet, so when using a runnable you can experiment screen blinking. To solve this issue, you can listen for lauyout changes on scrollview like this:

scrollview.addOnLayoutChangeListener(new View.OnLayoutChangeListener() {
            @Override
            public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) {
                scrollview.scrollTo(x, y)
            }
        });
mgonzalez
  • 107
  • 2