5

I currently have a ScrollView with a LinearLayout. I'm changing some content dynamically, e.g. setting a long text passage dynamically, and adding dynamic layouts (buttons etc.). This causes the linear layout's height to change, which causes the ScrollView to scroll down. How can I keep the current scroll position while the layout is being dynamically updated?

<ScrollView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fillViewport="true">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical
        android:gravity="center_horizontal"
        >

        <!-- Dynamic Content Here -->
    </LinearLayout>
</ScrollView>
lumi
  • 1,176
  • 1
  • 9
  • 6
  • Try this? or downvote me into oblivion... see if i care. http://stackoverflow.com/questions/25648526/manage-scroll-position-in-scrollview-during-orientation-change? – Frenchie Dec 31 '14 at 19:27
  • I could try storing the position and then restoring, but that would mean I would have to listen for each individual view's layout which will get pretty messy, especially if we take user touches into consideration. I'll save this if there is no alternative. – lumi Dec 31 '14 at 19:36
  • 1
    I had the same problem, i fix it with the solution posted by mjp66 http://stackoverflow.com/a/14632456/4712031 – Leo Apr 15 '16 at 13:48
  • @Leo's link to mjp66's answer worked for me. At the time of writing it is the correct answer and has 220 up-votes including mine. – Chucky Mar 08 '17 at 12:23

2 Answers2

-1

One option i would suggest is to save the scroll position in a bundle and restore it.

@Override
protected void onSaveInstanceState(Bundle outState) {
    outState.putInt("xPos", scrollView.getScrollX());
    outState.putInt("yPos", scrollView.getScrollY());
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    ...
    int scrollX = savedInstanceState.getInt("xPos"); // Default value 0
    int scrollY = savedInstanceState.getInt("yPos");
    scrollView.scrollTo(scrollX, scrollY);
}
Prem
  • 4,823
  • 4
  • 31
  • 63
-1

I think with this steps you can do it :

  • Create a custom scroll view that extends from scrollview
  • Define a boolean property for enabling and disabling scroll
  • Check the boolean variable on the onScollChanged method and return false if is true
  • Set the boolean variable true before adding dynamic views to it's container
  • Set it to false when adding done

I hope these help you

Morteza Soleimani
  • 2,652
  • 3
  • 25
  • 44
  • I'm not sure if this will work due to the fact that computeScroll is called some time after the view has been added to the layout, which would mean that I would again have to keep track of layout changes... – lumi Dec 31 '14 at 19:49
  • You want add the views dynamically after that activity loaded and content view set ? Or you are setting the content view after adding new views to root view ? – Morteza Soleimani Dec 31 '14 at 19:57
  • I am loading the activity, fetching some data then adding the data and adding dynamic views into the layout. – lumi Dec 31 '14 at 20:04