我正在尝试删除RecyclerView
中的第一项。使用livedata和diffutil监视列表的更改。仅当RecyclerView
中的第一项被删除(第0个索引)时,崩溃才会发生。这似乎与其他相关问题类似。快速滚动等时不会发生此问题。只有在删除第0个位置的项目时才会发生Ot。该项目本身已从数据库中删除。但是应用程序崩溃了。我尝试从RecyclerView
类中恢复错误:.tryGetViewHolderForPositionByDeadline(RecyclerView.java:5923)
从RecyclerView.java:
if (offsetPosition < 0 || offsetPosition >= mAdapter.getItemCount()) {
throw new IndexOutOfBoundsException("Inconsistency detected. Invalid item "
+ "position " + position + "(offset:" + offsetPosition + ")."
+ "state:" + mState.getItemCount());
}
偏移位置小于0(-1)
完成错误堆栈:
03-23 08:55:38.739 25087-25087/com.application.essentials E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.application.essentials, PID: 25087
java.lang.IndexOutOfBoundsException: Inconsistency detected. Invalid item position 0(offset:-1).state:3 android.support.v7.widget.RecyclerView{41df14f0 VFED.V.. ......ID 0,0-480,606 #7f080084 app:id/recycler_view}, adapter:com.application.essentials.model.AlarmsAdapter@41dd7d80, layout:android.support.v7.widget.LinearLayoutManager@41e10b80, context:com.application.essentials.MainActivity@41d37840
at android.support.v7.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:5923)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5858)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5854)
at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2230)
at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1557)
at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1517)
at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:622)
at android.support.v7.widget.RecyclerView.dispatchLayoutStep1(RecyclerView.java:3875)
at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:3639)
at android.support.v7.widget.RecyclerView.onLayout(RecyclerView.java:4194)
at android.view.View.layout(View.java:15819)
at android.view.ViewGroup.layout(ViewGroup.java:4884)
at android.support.design.widget.CoordinatorLayout.layoutChild(CoordinatorLayout.java:1183)
at android.support.design.widget.CoordinatorLayout.onLayoutChild(CoordinatorLayout.java:870)
at android.support.design.widget.CoordinatorLayout.onLayout(CoordinatorLayout.java:889)
at android.view.View.layout(View.java:15819)
at android.view.ViewGroup.layout(ViewGroup.java:4884)
at android.support.v4.view.ViewPager.onLayout(ViewPager.java:1775)
at android.view.View.layout(View.java:15819)
at android.view.ViewGroup.layout(ViewGroup.java:4884)
at android.support.design.widget.HeaderScrollingViewBehavior.layoutChild(HeaderScrollingViewBehavior.java:142)
at android.support.design.widget.ViewOffsetBehavior.onLayoutChild(ViewOffsetBehavior.java:41)
at android.support.design.widget.AppBarLayout$ScrollingViewBehavior.onLayoutChild(AppBarLayout.java:1556)
at android.support.design.widget.CoordinatorLayout.onLayout(CoordinatorLayout.java:888)
at android.view.View.layout(View.java:15819)
at android.view.ViewGroup.layout(ViewGroup.java:4884)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
at android.view.View.layout(View.java:15819)
at android.view.ViewGroup.layout(ViewGroup.java:4884)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1677)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1531)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1440)
at android.view.View.layout(View.java:15819)
at android.view.ViewGroup.layout(ViewGroup.java:4884)...
答案 0 :(得分:1)
问题出在适配器上。我在Adapter Constructor中使用 setHasStableIds(true); ,以避免“更改时项目闪烁”。我已经覆盖了2种方法: public long getItemId(int position){...}和public int getItemViewType(int position){...}
当我删除以下行后,崩溃消失了,并且删除了第0个项目:setHasStableIds(true);在构造函数中。但是物品在闪烁。 问题出在我用覆盖的方法返回的 什么 。下面的代码有问题:
@Override
public long getItemId(int position) {
return position;
}
@Override
public int getItemViewType(int position) {
return position;
}
我要返回的职位不是唯一的。我将商品的ID作为唯一ID返回,问题消失了。
正确的代码:
@Override
public long getItemId(int position) {
return mAlarmEntityList.get(position).getAlarmId();
}
@Override
public int getItemViewType(int position) {
return position;
}
我花了数小时来找出问题所在。这些答案对我有帮助:Inconsistency detected in RecyclerView, How to change contents of RecyclerView while scrolling
How to prevent RecyclerView item from blinking after notifyItemChanged(pos)?
希望这对某人有帮助!