0

I have a scrollView in my ViewPager fragment. I want to set the initial scrollView position to (0,100).

I tried setting scrollView.scrollTo(0,100) in onCreateView(). It didn't work.

Then I tried the same in Handler and it worked but it only scrolls to (0,100) after (0,0) causing little jerk in scrollView which is bad for user experience.

Is there any way to to make scrollview directly scroll to (0,100) instead of (0,0)? or Is there any method available in scrollView to detect initial scroll event?

P.S :I saw the question here which also the same as mine but the accepted answer is not working for me.

Any help would be appreciated. Thanks

Community
  • 1
  • 1
Vijay
  • 545
  • 1
  • 5
  • 15
  • Since your scrollview is inside a fragment you are probably calling the method on the wrong place since these method are supposed to work. Can't be much of help without a real example of what you are doing. – Ricardo Oct 18 '16 at 10:19
  • did you ever find a solution? I'm having the same problem – M. Smith Apr 07 '17 at 17:41

2 Answers2

0

Instead of scrollTo you can use requestFocus on first child of inner layout of scroll view.

Example:

  <ScrollView>

    <LinearLayout>

        <TextView
            android:focusable="true"
            android:focusableInTouchMode="true"
            android:tag="ScrollTopTag" />
        //***rest of your layout***//
    </LinearLayout>

</ScrollView>

In your activity where you want to scroll to top of scroll view add below line

View view = findViewByTag("ScrollTopTag); view.requestFocus();

  • Can you please comment your modified layout here. – WritingForAnroid Oct 18 '16 at 10:47
  • Hi Thanks for your answer. `View view1 = scrollView.getChildAt(scrollView.getChildCount()-1); view1.setFocusable(true); view1.setFocusableInTouchMode(true); view1.requestFocus();` I believe that is what you said somewhat but didn't worked for me. Like I said before scrollTo works fine in Handler and it scrolls to desired position. So there is no problem with scrollTo() method. The problem if you call scrollTo(0,100), it first scrolls to (0,0), then only it goest to (0,100). – Vijay Oct 18 '16 at 11:07
0

It feels like you are inflating views inside the adapter without recycling them. This would explain the choppy loading times. Other than that, from the example, try delaying it even more. In the example it uses 250ms, but if your listview it not done loading than its not going to scroll anywhere since it is not filled yet. I would start off but first increasing the duration to about 2000ms just to see if it works. If that works, I would go back to your adapter class and make sure each view is recycling instead of reinflated.

fsebek
  • 389
  • 2
  • 9
  • But the question is, it scrolls to desired position only after initial position(0,0). And I want to override that initial position. It causes small jerk. I didn't implemented the recycle part. I am only reinflating views – Vijay Oct 18 '16 at 10:48