ScrollView 内的 RecyclerView - Android

RecyclerView inside ScrollView - Android

这是我的 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 上使用滚动侦听器滚动的速度逐渐隐藏 header,如下所示:

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

问题是,虽然 header 隐藏并且我以缓慢的方式滚动,但存在一些严重的闪烁问题。景色像疯了似的上下起伏。当 dy > headerHeight.

时,此行为停止

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

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