首页 > 解决方案 > ItemDecoration 在 NestedScrollView 中不起作用

问题描述

我已经使用粘性标题实现了 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 不起作用。

我发现了下一个信息:

你对如何解决它有什么建议吗?

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 是从那里复制的 如何在 RecyclerView 中制作粘性标题?(没有外部库)

标签: androidandroid-recyclerviewitem-decoration

解决方案


你不应该使用该方法添加ItemDecoration一个吗?RecyclerViewaddDecoration(ItemDecoration decoration)


推荐阅读