【问题标题】:Scrolltoposition/ smoothscrolltoposition for recyclerview under a nestedscrollview layout用于嵌套滚动视图布局下的回收器视图的滚动位置/平滑滚动位置
【发布时间】:2019-04-27 17:06:47
【问题描述】:

我在nestedsrcollview 下有一个recyclerview。我想为 recyclerview 实现滚动到特定位置,但我遇到了困难。 xml代码为:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    tools:context=".HomeFragment"
    android:background="#ffffff"

    android:fillViewport="true"
    android:id="@+id/nestedscrollview"
    >

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

       <<some other layouts>>

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

            android:layout_marginBottom="52dp"

            />





    </LinearLayout>




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

我想实现 home_blog_list recyclerview 的滚动定位到一个位置(比如 26)。怎么做? P.S.-我已将 home_blog_list 的 nestedscrollingenabled 设置为 false。请注意,我想将 nestedscrollview 滚动到 recyclerview 的特定行。我不想要只滚动recyclerview 的情况。提前致谢!

【问题讨论】:

  • 不,它没有用。我实际上正在滚动nestedscrollview。回收站视图就在里面。如何将nestedscrollview滚动到recyclerview里面的特定位置?
  • 为什么需要嵌套滚动视图。 recyclerview 是可滚动的。如果你同时实现嵌套的scrollview 和recyclerview,它会在滚动时导致很多问题。
  • 实际上我的布局中还有另一个recyclerview(这个是水平的)。我希望整个系统滚动。 (就像 instagram 中的状态 recyclerview 和帖子 recyclerview 一样)。

标签: android android-recyclerview android-nestedscrollview


【解决方案1】:

我偶然发现了同样的问题,我找到了一个简单的解决方案,不需要使用 asif-ali 建议的库进行重构。

在我当前的项目中,我有一个NestedScrollView,其中包含一个ConstraintLayout。 这个ConstraintLayout 包含一个由多个视图组成的复杂标题,然后是我的RecyclerView

和你一样,我需要整个东西都可以滚动。

也就是说,当用户希望查看来自特定 RecyclerView 的项目时,您通常会调用:

RecyclerView#smoothScrollToPosition(int position)

但由于RecyclerView 的高度设置为wrap_content,因此会显示完整列表,其中ViewHolders 的数量与其adapter 中的项目数量一样多。 诚然,我们并没有从回收中受益,但是为什么我们需要 ScrollView 呢?使用@asif-ali 解决方案肯定会带来回收优化,但这不是重点。

所以,我们有一个完全布局的RecyclerView。要滚动到特定项目 (ViewHolder#itemView) 的位置,您可以执行以下操作:

final void smoothScrollToPosition(final int position) {
    final ViewHolder itemViewHolder = this.recyclerView.findViewHolderForAdapterPosition(position);
    // at this point, the ViewHolder should NOT be null ! Or else, position is incorrect !

    final int scrollYTo = (int) itemViewHolder.itemView.getY();
    // FYI: in case of a horizontal scrollview, you may use getX();
    this.nestedScrollView.smoothScrollTo(
        0, // x - for horizontal
        scrollYTo
    );
}

就是这样! 这样做后(在我的测试用例中),孩子可能不会完全可见,所以我建议将 itemView 的一半高度添加到 scrollYTo 变量中,以确保 nestedScrollView 可以滚动得足够多。如果这样做,您可能还想检查nestedScrollView 必须滚动到哪个方向(向上,然后删除一半高度,或者向下,然后添加一半高度。


[编辑 1]

经过进一步的测试和研究,基于这个答案:https://stackoverflow.com/a/6831790/3535408 实际上定位itemView.getBottom 更好更简单。在我的应用程序上,它完美无缺。

所以更新后的代码如下:

final void smoothScrollToPosition(final int position) {
    final ViewHolder itemViewHolder = this.recyclerView.findViewHolderForAdapterPosition(position);
    // at this point, the ViewHolder should NOT be null ! Or else, position is incorrect !

    // FYI: in case of a horizontal scrollview, you may use getX();
    this.nestedScrollView.smoothScrollTo(
        0, // x - for horizontal
        itemViewHolder.itemView.getBottom()
    );
}

【讨论】:

    【解决方案2】:

    我想这就是你想要的,看看:link

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-13
    • 1970-01-01
    • 1970-01-01
    • 2020-03-09
    相关资源
    最近更新 更多