Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Saving scroll state of NestedScrollView

My application revolves around a HomeActivity which contains 4 tabs at the bottom. Each of these tabs is a fragment, all of them are added (not replaced) from the start, and they are hidden/shown upon tapping the appropriate tab.

My problem is that whenever I change tab, the state of my scroll is lost. Each fragment which exhibits that issue uses a android.support.v4.widget.NestedScrollView (see below for an example).

Note: My fragments that use a RecyclerView or ListView keep their scroll state for some reason.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <include layout="@layout/include_appbar_title" />

    <android.support.v4.widget.NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <!-- Content -->

    </android.support.v4.widget.NestedScrollView>

</LinearLayout>

I read several posts regarding saving the instance state (this one, that one for example), and their solution either don't work in my scenario, or are not practical to implement given I have 4-12 different fragments I'd need to modify to make it work.

What is the best way to have a Nested Scroll View keep its scroll position on fragment changes ?

like image 608
Julian Honma Avatar asked Jun 07 '17 08:06

Julian Honma


4 Answers

One solution I found on inthecheesefactory is that fragments, by default, have their state saved (from the input in a EditText, to the scroll position), but ONLY if an ID is given to the xml element.

In my case, just adding an ID to my NestedScrollView fixed the problem:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <include layout="@layout/include_appbar_title" />

    <android.support.v4.widget.NestedScrollView
        android:id="@+id/NestedScrollView"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <!-- Content -->

    </android.support.v4.widget.NestedScrollView>

</LinearLayout>
like image 128
Julian Honma Avatar answered Nov 11 '22 23:11

Julian Honma


Looking at the implementation of the NestedScrollView, we see that the scrollY property of the NestedScrollView is stored in its SavedState as its saved scroll position.

// Source: NestedScrollView.java

@Override
protected Parcelable onSaveInstanceState() {
    Parcelable superState = super.onSaveInstanceState();
    SavedState ss = new SavedState(superState);
    ss.scrollPosition = getScrollY();
    return ss;
}

Therefore I do agree with Ramiro G.M. in terms of the idea of retaining scroll position across configuration changes. I do not think a sub-class of NestedScrollView is necessary in this case.

If you are using a Fragment and MVVM, then I would save the scroll position of the NestedScrollView to my ViewModel in the fragments onViewDestroyed method. You can later observe the state via a LiveData object when the fragments view has been created.

override fun onViewCreated(...) {
    mViewModel.scrollState.observe(viewLifecycleOwner, { scrollState ->
         binding.myNestedScrollView.scrollY = scrollState
    })
}

override fun onDestroyView() {
    val scrollState = binding.myNestedScrollView.scrollY
    mViewModel.setScrollState(scrollState)
    super.onDestroyView()
}

This is just a simple example but the concept holds true.

like image 40
Kaylen Travis Pillay Avatar answered Nov 11 '22 23:11

Kaylen Travis Pillay


You can manage the instance state (which includes the scroll state) by yourself by first making the corresponding methods public:

class SaveScrollNestedScrollViewer : NestedScrollView {
    constructor(context: Context) : super(context)

    constructor(context: Context, attributes: AttributeSet) : super(context, attributes)

    constructor(context: Context, attributes: AttributeSet, defStyleAttr: Int) : super(context, attributes, defStyleAttr)


    public override fun onSaveInstanceState(): Parcelable? {
        return super.onSaveInstanceState()
    }

    public override fun onRestoreInstanceState(state: Parcelable?) {
        super.onRestoreInstanceState(state)
    }
}

Then use it in your view with (YOUR_NAMESPACE is the namespace of the SaveScrollNestedScrollViewer class):

<YOUR_NAMESPACE.SaveScrollNestedScrollViewer
     android:id="@+id/my_scroll_viewer"
     android:layout_width="match_parent"
     android:layout_height="match_parent">
</YOUR_NAMESPACE.SaveScrollNestedScrollViewer>

and then in the activity which displays it, save / recover the state as needed. For example, if you want to recover the scroll position after navigating away use the following:

class MyActivity : AppCompatActivity() {

    companion object {
        var myScrollViewerInstanceState: Parcelable? = null
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.my_activity)

        if (myScrollViewerInstanceState != null) {
            my_scroll_viewer.onRestoreInstanceState(myScrollViewerInstanceState)
        }
    }

    public override fun onPause() {
        super.onPause()
        myScrollViewerInstanceState = my_scroll_viewer.onSaveInstanceState()
    }
}
like image 1
Florian Moser Avatar answered Nov 11 '22 23:11

Florian Moser


Since all answers are now deprecated, I'll give y'all a new option.

  1. Create a variable to hold the nested scroll view on your view model:
class DummyViewModel : ViewModel() {
var estadoNestedSV:Int?=null
}
  1. Override onStop on your Fragment to save the state before the nested scroll view gets destroyed:
override fun onStop() {
        try {
            super.onStop()
            viewModel.estadoNestedSV = binding.nestedSV.scrollY
        } catch (e: Exception) {
            Log.i((activity as MainActivity).constantes.TAG_GENERAL, e.message!!)
        }
    }
  1. Restore the state after the view is created on your fragment by overriding onViewCreated:
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        try {
           //Check first if data exists to know if this load is a first time or if the device was rotated.
           if(viewModel.data.value != null)
           binding.nestedSVPelisDetalles.scrollY = viewModel.estadoNestedSV!!
            } catch (e: Exception) {
            Log.i((activity as MainActivity).constantes.TAG_GENERAL, e.message!!)
            }
    }

Happy coding!

like image 1
Ramiro G.M. Avatar answered Nov 12 '22 00:11

Ramiro G.M.