【发布时间】:2020-02-23 15:58:41
【问题描述】:
我已经实现了带有粘性标题的 RecyclerView,我通过 ItemDecoration 实现了它。 在独立回收站视图的情况下,它可以按预期工作。
<LinearLayout ...>
<androidx.recyclerview.widget.RecyclerView .../>
<androidx.recyclerview.widget.RecyclerView ... /> // ItemDecoration is supposed to be here and it works excellent
</LinearLayout>
但是我有两个列表,我需要使用 NestedScrollView
<androidx.core.widget.NestedScrollView ...>
<LinearLayout ...>
<androidx.recyclerview.widget.RecyclerView .../>
<androidx.recyclerview.widget.RecyclerView ... /> // It doesn't work here
</LinearLayout>
</androidx.core.widget.NestedScrollView ...>
在这种情况下,ItemDecoration 不起作用。
我发现了下一条信息:
onDrawOver 在滚动时不会被调用,因为 RecyclerView.draw() 也不会被调用。
同时创建所有项目(因此适配器为数据中的所有项目创建视图持有者)。这很糟糕,但这不是我的主要问题。
我试图在滚动时强制调用 recyclerview 的重绘,但它不起作用
你有什么建议可以解决吗?
UPD xml 文件:
<?xml version="1.0" encoding="utf-8"?>
<androidx.core.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
android:scrollbars="none">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/black_background_color"
android:focusableInTouchMode="true"
android:orientation="vertical">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/menuNewsList"
android:layout_width="match_parent"
android:layout_height="170dp" />
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/listMenu"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager" />
</LinearLayout>
</androidx.core.widget.NestedScrollView>
代码初始化:
listMenu.apply {
adapter = dishAdapter
isNestedScrollingEnabled = false
}
listMenu.addItemDecoration(HeaderItemDecoration(listMenu, isHeader = isHeader()))
HeaderItemDecoration 是从那里复制的 How can I make sticky headers in RecyclerView? (Without external lib)
【问题讨论】:
-
你能清除一件事吗,“ItemDecoration 不起作用”是指装饰标头不呈现还是不呈现但不按要求滚动。
-
ItemDecoration 不呈现。实际上 onDrawOver 甚至没有被调用
-
您找到解决方案了吗?在这里遇到同样的问题。
标签: android android-recyclerview item-decoration