【问题标题】:Nested recyclerviews inside nestedscrollview在nestedscrollview 内嵌套recyclerviews
【发布时间】:2018-02-22 15:36:46
【问题描述】:

我正在尝试实现嵌套的RecyclerView。我这样做了,但父 RecyclerView 滚动时滚动不流畅。我做了很多优化,但仍然没有成功滚动,直到我将父 recyclerview 放入NestedScrollView。 Scroll 现在完美无缺,但我有一个问题。

如果我滚动(哪怕是一点点)我的内部 RecyclerView(水平),我会立即回到 [垂直 recyclerview - 父级] 的开头

这种情况会发生一次。为什么会这样,是否有可能修复它?

<android.support.v4.widget.NestedScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fillViewport="true">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/parent_rv"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

    </android.support.v7.widget.RecyclerView>

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

【问题讨论】:

  • RecyclerView.setNestedScrollingEnabled(false);
  • @NileshRathod 我在父 recyclerview 上有 setNestedScrollingEnabled(false)
  • If I scroll till the end of my inner RecyclerView (horizontal), I immediately get back to the start. - 您的水平起点 RecyclerView 或父 RecyclerView?
  • 水平的RecyclerView在滚动结束时是否加载新数据?

标签: android android-recyclerview


【解决方案1】:

据我了解您的问题,当内部水平 RecyclerView 项目更新时,您的父级 RecyclerView 会更新并滚动到位置 0。您还说水平RecyclerView 保持在同一位置更新项目已更新。在这种情况下,您需要保存父 RecyclerView 的状态,然后在水平 RecyclerView 中的项目更新后将其放回原处。

// Save state
private Parcelable recyclerViewState;
recyclerViewState = parentRecyclerView.getLayoutManager().onSaveInstanceState();

// Restore state
parentRecyclerView.getLayoutManager().onRestoreInstanceState(recyclerViewState);

在调用 API 之前保存状态以更新水平 RecyclerView 中的数据,然后在更新完成时恢复父 RecyclerView 的位置。

【讨论】:

  • 是否可以选择通过 xml 属性或更优雅的方式实现结果?另外,我想指出,我真的不必滚动到最后一个元素(当加载新元素时),我可以做微小的滚动,然后开始跳跃。 ://
  • 不,它没有。
  • 哦,我明白了。请编辑您的问题并在您的问题中添加 cmets,这些是被要求澄清的,以便其他人也可以提供帮助。
【解决方案2】:

我有一个解决方案。 基本上,我所做的是将回收器视图包装在相关布局中并设置android:descendantFocusability="blocksDescendants",现在效果很好。

<android.support.v4.widget.NestedScrollView
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fillViewport="true"
    app:layout_behavior="@string/appbar_scrolling_view_behavior">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:descendantFocusability="blocksDescendants">

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

        </RelativeLayout>

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

【讨论】:

    猜你喜欢
    • 2016-08-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多