【问题标题】:SmoothScrollTo item in RecyclerView inside NestedScrollViewNestedScrollView 内 RecyclerView 中的 SmoothScrollTo 项
【发布时间】:2019-06-14 17:47:12
【问题描述】:

这是我的布局:

<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
android:id="@+id/nsvRoot"
app:layout_behavior="@string/appbar_scrolling_view_behavior">

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


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

        <android.support.v7.widget.CardView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="8dp"
            app:cardCornerRadius="0dp"
            app:cardElevation="4dp">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical">
                <More_Child_Views/>
            </LinearLayout>
        </android.support.v7.widget.CardView>

        <android.support.v7.widget.CardView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="8dp"
            android:layout_marginBottom="8dp"
            app:cardCornerRadius="0dp"
            app:cardElevation="4dp">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical">
                <More_Child_Views/>
            </LinearLayout>
        </android.support.v7.widget.CardView>

        <android.support.v7.widget.CardView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="8dp"
            android:layout_marginBottom="8dp"
            app:cardElevation="4dp">

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


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

                    <android.support.v7.widget.RecyclerView
                        android:id="@+id/rvItems"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content" />

                </LinearLayout>
            </LinearLayout>
        </android.support.v7.widget.CardView>
    </LinearLayout>

</LinearLayout>

在此布局的底部有一个RecyclerView,ID 为rvItems

我希望根NestedScrollView 滚动到此Recyclerview 中特定项目的位置。

我试过了:

rvItems.getChildAt(itemIndex).getY();

childAt(itemIndex) 返回 nullrvItems.getItemCount() 返回 0,即使回收站视图中显示了 15 多个项目。

我认为这是由于NestedScrollViewRecyclerView 的父级。 如何滚动到 recyclerView 中项目的位置?

【问题讨论】:

  • 发布您的适配器代码

标签: android android-layout android-recyclerview smooth-scrolling android-nestedscrollview


【解决方案1】:

几点说明

请向我们展示您如何声明/设置您的适配器和回收站视图。还要检查您的方法调用的顺序。 (例如,在将适配器传递给 recyclerview 之前先设置适配器)。

什么可以解决这个问题

recyclerView.getLayoutManager().scrollToPosition(10); // 10 being the position of the item

了解更多

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-02-15
    • 2017-05-28
    • 1970-01-01
    • 2019-03-21
    • 1970-01-01
    • 2021-12-10
    • 2019-04-26
    相关资源
    最近更新 更多