Search code examples
androidandroid-recyclerviewandroid-nestedscrollview

Scroll NestedScrollView to top when rotating device


I have following layout includes a NestedScrollView(it has a RecyclerView inside) :

<android.support.constraint.motion.MotionLayout
        android:id="@+id/details_motion"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layoutDescription="@xml/scene_show_details">

        <ImageView
            android:id="@+id/details_backdrop"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:scaleType="centerCrop"
            app:imageUrl="@{movie.backdropPath}"
            tools:ignore="ContentDescription" />

        <android.support.v7.widget.AppCompatImageView
            android:id="@+id/details_poster"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:background="@drawable/placeholder"
            android:scaleType="centerCrop"
            android:transformPivotX="0px"
            android:transformPivotY="0px"
            android:transitionName="@string/view_name_header_image"
            app:imageUrl="@{movie.posterPath}" />

        <android.support.v7.widget.Toolbar
            android:id="@+id/details_toolbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:minHeight="?attr/actionBarSize"
            app:theme="@style/Toolbar" />

        <android.support.v4.widget.NestedScrollView
            android:id="@+id/details_rv"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:background="@color/window_background"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@id/details_appbar_background">

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

                <TextView
                    style="@style/TmdbMargin.Small"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="@dimen/padding_normal"
                    android:text="@{@string/release_date(movie.releaseDate)}" />

                <TextView
                    style="@style/TmdbMargin.Small"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="@{@string/rating(movie.voteAverage)}" />

                <TextView
                    style="@style/TmdbMargin.Title"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="@string/summary" />

                <TextView
                    android:id="@+id/summary"
                    style="@style/TmdbMargin.Body"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="@{movie.overview}" />

                <include
                    layout="@layout/trailers"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="@dimen/padding_normal"
                    android:layout_marginTop="@dimen/padding_large"
                    app:vm="@{vm}"
                    tools:ignore="RtlHardcoded" />

                <TextView
                    style="@style/TmdbMargin.Title"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="@dimen/padding_normal"
                    android:text="@string/cast"
                    app:visibleGone="@{vm.isCastVisible}" />

                <android.support.v7.widget.RecyclerView
                    android:id="@+id/cast_list"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_marginTop="12dp" />

            </LinearLayout>

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

    </android.support.constraint.motion.MotionLayout>

When I rotate the device (for instance when I am in the middle of RecyclerView list), I want to scroll to top of NestedScrollView. I tried :

nestedScrollView.fullScroll(View.FOCUS_UP);
nestedScrollView.scrollTo(0,0);

But none of them worked in my case. Could be any solution for me?

Source code can be found at : https://github.com/Ali-Rezaei/TMDb-Paging


Solution

  • I downloaded your app, problem is a while you trying to set scrollTo(0,0) or through fullScroll(View.FOCUS_UP), your list has not been initialized / rendered, list is empty when you calling operation scroll to top. Easy workaround is delayed your scroll operation (not so perfect way):

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        // Your code ....
        // ....
        nestedScrollView = view.findViewById<NestedScrollView>(R.id.details_rv)
        nestedScrollView.postDelayed({
            nestedScrollView.scrollTo(0, 0)
        }, 100)
    }
    

    While searched solution found a lot of great animations. You are MotionLayout monster :)