如果 'setHasStableIds(true)' 在适配器中,则删除第一项时 RecyclerView 会崩溃

RecyclerView Crashes when delete first item if 'setHasStableIds(true)' is in adapter

如果我设置

,我的 RecyclerView 会崩溃
init{
   setHasStableIds(true)
}

在我的 ListAdapter 中。如果我删除此代码,它可以正常工作。 Logcat 下面

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.starsolutions.starfilemanager, PID: 10919
    java.lang.IndexOutOfBoundsException: Inconsistency detected. Invalid item position 0(offset:-1).state:2 androidx.recyclerview.widget.RecyclerView{4a24664 VFED..... ......ID 0,147-1080,1878 #7f080181 app:id/rvExplorer}, adapter:com.starsolutions.starfilemanager.ui.ExplorerAdapter@efe3a61, layout:androidx.recyclerview.widget.LinearLayoutManager@e9986, context:com.starsolutions.starfilemanager.MainActivity@a4f9b5f
        at androidx.recyclerview.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:6183)

我的 Id 是项目在列表中的位置。

override fun getItemId(position: Int): Long = position.toLong()

现在我用其他唯一编号替换了 'position.toLong()',问题就解决了。