【问题标题】:ItemDecoration doesn't work inside NestedScrollViewItemDecoration 在 NestedScrollView 中不起作用
【发布时间】: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


【解决方案1】:

您不应该使用addDecoration(ItemDecoration decoration) 方法将ItemDecoration 添加到RecyclerView 吗?

【讨论】:

  • 我已经添加了。在第一个描述的情况下,它运行良好。
  • ItemDecoration 是否适用于第一个 RecyclerView,即使它实际上不应该存在?你试过吗?
  • 您能否提供更多详细信息,例如视图和视图组的属性?我会尝试复制该问题,但如果没有更多上下文信息,这会更困难?
  • 第一个 RecyclerView 具有水平滚动功能,因此 ItemDecoration 无法使用它。但是,如果将 2nd RecyclerView 移到顶部,它将无法正常工作。此外,如果删除 NestedScrollView 它会按预期工作
  • 我在我的主要问题中添加了更多细节
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-03-17
  • 2015-09-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多