繁体   English   中英

NestedScrollView平滑滚动到顶部和内部视图

[英]NestedScrollView smooth scroll to the top and to a view inside

首先,我需要将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>

我有一个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>

并在我的click事件中给了nsv_main.scrollY = 0 即使滚动不顺畅,它仍然有效。

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM