【问题标题】:NestedScrollView smooth scroll to the top and to a view insideNestedScrollView 平滑滚动到顶部和内部的视图
【发布时间】:2018-02-17 13:34:04
【问题描述】:

首先我需要将NestedScrollView 滚动到顶部,但smoothScrollTo(0, 0) 对我不起作用(页面只是跳了一下)。其次,我想知道如何滚动到NestedScrollView 中的某个视图。 API 27,支持 27.0.2。

<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools">

    <data/>

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

        <android.support.v4.widget.NestedScrollView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@color/PageBackground"
            android:fillViewport="true">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:descendantFocusability="blocksDescendants"
                android:orientation="vertical"
                android:paddingBottom="@dimen/indent_page_bottom">

                ...

                <android.support.v7.widget.RecyclerView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="vertical" />

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

</layout>

【问题讨论】:

标签: android android-layout android-nestedscrollview


【解决方案1】:

我有一个nestedscrollview,里面有recyclerview。我试过smoothScrollTo(0, nsv_main.top)。没用。我给nestedscrollview 提供了一个ID,如下所示

<android.support.v4.widget.NestedScrollView
    android:id="@+id/nsv_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/PageBackground"
    android:fillViewport="true">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:descendantFocusability="blocksDescendants"
        android:orientation="vertical"
        android:paddingBottom="@dimen/indent_page_bottom">

        ...

        <android.support.v7.widget.RecyclerView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical" />

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

并在我的点击事件中提供nsv_main.scrollY = 0。它工作,即使它不是一个平滑的滚动。

【讨论】:

  • nsv_main.scrollY = 0 :对我不起作用。我在 NestedScrollView 中有一个 ConstraintLayout,它的 recyclerView 高度设置为 wrap_content。
【解决方案2】:

尝试使用fling实现平滑滚动:

nestedScrollView.fling(0);
nestedScrollView.smoothScrollTo(0, 0);

【讨论】:

    猜你喜欢
    • 2020-03-29
    • 1970-01-01
    • 1970-01-01
    • 2018-07-13
    • 1970-01-01
    • 2013-05-26
    • 2021-10-20
    • 2018-07-19
    • 1970-01-01
    相关资源
    最近更新 更多