Плавная прокрутка NestedScrollView вверх и к виду внутри

Сначала мне нужно прокрутить 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>

person Konstantin Konopko    schedule 17.02.2018    source источник
comment


Ответы (2)


У меня был nestedscrollview, а внутри него recyclerview. Я пробовал smoothScrollTo(0, nsv_main.top). Не работает. Я дал идентификатор nestedscrollview, как показано ниже.

<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 в моем событии клика. Это сработало, хотя это не плавная прокрутка.

person ArjunA    schedule 26.09.2018
comment
nsv_main.scrollY = 0 : у меня не сработало. У меня есть ConstraintLayout внутри NestedScrollView, для высоты recyclerView которого установлено значение wrap_content. - person Abhi Muktheeswarar; 26.09.2019

Попробуйте использовать fling для плавной прокрутки:

nestedScrollView.fling(0);
nestedScrollView.smoothScrollTo(0, 0);
person Swetha Reddy    schedule 18.10.2018