簡體   English   中英

ItemDecoration 在 NestedScrollView 中不起作用

[英]ItemDecoration doesn't work inside 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 不起作用。

我發現了下一個信息:

  • 滾動時不會調用 onDrawOver,因為也不會調用 RecyclerView.draw()。

  • 所有項目都是同時創建的(因此適配器為數據中的所有項目創建視圖持有者)。 這很糟糕,但這不是我的主要問題。

  • 我試圖在滾動時強制調用 recyclerview 的重繪,但它不起作用

你對如何解決有什么建議嗎?

更新 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 中制作粘性標題? (沒有外部庫)

難道不應該使用addDecoration(ItemDecoration decoration)方法將ItemDecoration添加到RecyclerView嗎?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM