【发布时间】: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 我已经试过了,但没有任何区别。
-
添加recyclerView.setNestedScrollingEnabled(false);并使用 NestedScrollView。
-
我不能使用 recyclerView.setNestedScrollingEnabled(false);因为那时我必须将 RecyclerView 扩展到它的整个长度并失去 RecyclerViews 的基本功能。我的清单真的很长。
标签: android android-recyclerview android-scrollview