代码之家  ›  专栏  ›  技术社区  ›  Ali

旋转设备时将NestedScrollView滚动到顶部

  •  0
  • Ali  · 技术社区  · 7 年前

    我有以下布局包括一个NestedScrollView(它内部有一个RecyclerView):

    <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>
    

    当我旋转设备时(例如当我在RecyclerView列表的中间时),我想滚动到NestedScrollView的顶部。我试过:

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

    但在我的案子里他们都没用。对我来说有什么解决办法吗?

    源代码位于: https://github.com/Ali-Rezaei/TMDb-Paging

    1 回复  |  直到 7 年前
        1
  •  0
  •   Dmytro Ivanov    7 年前

    我下载了你的应用程序,问题是你正在尝试设置 scrollTo(0,0) 或通过 fullScroll(View.FOCUS_UP) ,您的列表尚未初始化/呈现,调用操作滚动到顶部时,列表为空。简单的解决方法是延迟您的滚动操作(不是那么完美的方式):

    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)
    }
    

    在搜索解决方案时发现了很多很棒的动画。你是motionlayout怪物:)

    推荐文章