Save the position of scrollview when the orientation changes

后端 未结 6 1579
星月不相逢
星月不相逢 2020-12-02 23:57

These is my layout:

\"detail

I need to save the scrolling position when the orientation change

相关标签:
6条回答
  • 2020-12-03 00:19

    You If your activity is not changing anything on rotation you can use android:configChanges="orientation|keyboardHidden|screenSize" in manifest file within this particular activity. But this is clearly workaround and not a proper solution.

    Note:

    If you need to use different views for landscape and portraits or if you need to use different views for different devices, One should use bundle and save data onsaveinstancestate and retrieve the data in oncreate. This method stops to recreate the activity and activity assumes that things will be handled by the user himself/herself.

    0 讨论(0)
  • 2020-12-03 00:19

    Do this in manifest..

      <activity
            android:name=".YourActivity"
            android:configChanges="keyboardHidden|orientation|screenSize"
            android:label="@string/app_name" >
      </activity>
    
    0 讨论(0)
  • 2020-12-03 00:25

    refer these to persist the scroll position and scroll after orientation change.

    https://asishinwp.wordpress.com/2013/04/15/save-scrollview-position-resume-scrollview-from-that-position/

    https://eliasbland.wordpress.com/2011/07/28/how-to-save-the-position-of-a-scrollview-when-the-orientation-changes-in-android/

    0 讨论(0)
  • 2020-12-03 00:29

    Just set android:id on your scrolling element. Your view will save its scrolling position automatically.

    Code from View.java:15554

    protected void dispatchSaveInstanceState(SparseArray<Parcelable> container) {
        if (mID != NO_ID && (mViewFlags & SAVE_DISABLED_MASK) == 0) {
            mPrivateFlags &= ~PFLAG_SAVE_STATE_CALLED;
            Parcelable state = onSaveInstanceState();
            if ((mPrivateFlags & PFLAG_SAVE_STATE_CALLED) == 0) {
                throw new IllegalStateException(
                        "Derived class did not call super.onSaveInstanceState()");
            }
            if (state != null) {
                // Log.i("View", "Freezing #" + Integer.toHexString(mID)
                // + ": " + state);
                container.put(mID, state);
            }
        }
    }
    
    0 讨论(0)
  • 2020-12-03 00:33

    To save and restore the scroll position of a ScrollView when the phone orientation changes you can do the following: Save the current position in the onSaveInstanceState method:

    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putIntArray("ARTICLE_SCROLL_POSITION",
                new int[]{ mScrollView.getScrollX(), mScrollView.getScrollY()});
    }
    

    Then restore the position in the onRestoreInstanceState method. Note that we need to post a Runnable to the ScrollView to get this to work:

    protected void onRestoreInstanceState(Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);
        final int[] position = savedInstanceState.getIntArray("ARTICLE_SCROLL_POSITION");
        if(position != null)
            mScrollView.post(new Runnable() {
                public void run() {
                    mScrollView.scrollTo(position[0], position[1]);
                }
            });
    }
    

    Found this solution on google. Credit goes to Original Coder. :)

    0 讨论(0)
  • 2020-12-03 00:40

    On my Samsung tablet, I didn't have to add as much code (just the call to the superclass). Also, I already had a name on the ListView.

    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
    }
    
    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <EditText
            android:id="@+id/search_text"
            android:maxLines="1"
            android:inputType="text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:hint="Enter search string here" />
    
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="doSearch"
            android:clickable="true"
            android:text="Search"
            android:layout_toRightOf="@id/search_text"/>
    
        <ListView xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/list_of_books"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_below="@id/search_text"
            android:divider="@null"
            android:orientation="vertical" />
    
    </RelativeLayout>
    
    0 讨论(0)
提交回复
热议问题