【问题标题】:RecyclerView inside ScrollView - AndroidScrollView 内的 RecyclerView - Android
【发布时间】:2021-08-30 16:33:40
【问题描述】:

这是我的 xml 代码:

<ScrollView
    android:id="@+id/scrollView"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">


        <!-- Header -->
        <RelativeLayout
            android:id="@+id/header"
            android:layout_width="match_parent"
            android:layout_height="100dp">

            <!-- Some views here -->

        </RelativeLayout>
        
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="1000dp"
            android:layout_marginTop="150dp">

            <androidx.recyclerview.widget.RecyclerView
                android:id="@+id/recyclerView"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                />

        </RelativeLayout>

    </RelativeLayout>

</ScrollView>

当用户触摸 RecyclerView 并滚动时,我会随着用户使用 RecyclerView 上的滚动监听器滚动的速度逐渐隐藏标题,如下所示:

@Override
public void onScroll(int dy)
{ 
    if (dy < headerHeight)
    {
        scrollView.setScrollY(dy);
    } 
}

问题是,当标题隐藏并且我以缓慢的方式滚动时,存在一些严重的闪烁问题。景色像疯了一样上下起伏。当 dy > headerHeight 时,此行为停止。

我猜测是因为整个 RecyclerView 的位置在 ScrollView 滚动时发生了变化,这就是问题的根本原因。如何解决此问题并实现平滑滚动?

【问题讨论】:

  • 当您有两个滚动元素时,您可能应该使用 NestedScrollView
  • @tyczj 我已经试过了,但没有任何区别。
  • 也许你应该看看这里stackoverflow.com/a/68967796/12660050
  • 添加recyclerView.setNestedScrollingEnabled(false);并使用 NestedScrollView。
  • 我不能使用 recyclerView.setNestedScrollingEnabled(false);因为那时我必须将 RecyclerView 扩展到它的整个长度并失去 RecyclerViews 的基本功能。我的清单真的很长。

标签: android android-recyclerview android-scrollview


【解决方案1】:

你的布局很糟糕。您不需要所有这些嵌套视图。例如阅读这篇文章:https://droidbyme.medium.com/android-recyclerview-with-multiple-view-type-multiple-view-holder-af798458763b。例如,删除所有那些不必要的视图并仅保留具有根相对布局的回收器视图,您的问题应该会消失。

【讨论】:

  • 我已经考虑过这种方法。具有不同高度的项目的 RecyclerView 存在性能问题。
  • 实际上这种方法真的很有帮助,我想谢谢你。这是迄今为止我找到的最接近我正在寻找的解决方案的东西。
猜你喜欢
  • 2014-10-20
  • 1970-01-01
  • 1970-01-01
  • 2015-11-30
  • 2016-01-13
  • 2020-09-07
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多