android - 在 recyclerView 中使用 ItemTouchHelper 时出现 java.lang.IndexOutOfBoundsException 错误?
问题描述
到目前为止,我已经阅读了这个主题的所有 stackoverflows,但问题没有解决。我有一个 ItemTouchHelper :
ItemTouchHelper helper = new ItemTouchHelper(new ItemTouchHelper.SimpleCallback(
0,
ItemTouchHelper.LEFT|ItemTouchHelper.RIGHT)
{
@Override
public boolean onMove(@NonNull RecyclerView recyclerView, @NonNull ViewHolder viewHolder, @NonNull ViewHolder target) {
return false;
}
@Override
public void onSwiped(@NonNull ViewHolder viewHolder, int direction) {
mNotes.remove(viewHolder.getAdapterPosition());
mAdapter.notifyItemChanged(viewHolder.getAdapterPosition());
}
});`enter code here`
使用此适配器附加到 recyclerView :
public class NotesAdapter extends RecyclerView.Adapter<NotesAdapter.MyViewHolder> {
List<Note> notes;
Context context;
public NotesAdapter(List<Note> notes, Context context) {
this.notes = notes;
this.context = context;
}
public void setNotes(List<Note> notes) {
this.notes = notes;
}
@NonNull
@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.note_list_item, parent, false);
return new MyViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
holder.bind(notes.get(position));
}
@Override
public int getItemCount() {
return notes.size();
}
public class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
TextView title, text, date;
Note mNote;
public MyViewHolder(@NonNull View itemView) {
super(itemView);
itemView.setOnClickListener(this);
title = itemView.findViewById(R.id.note_item_title);
text = itemView.findViewById(R.id.note_item_text);
date = itemView.findViewById(R.id.note_item_date);
}
@Override
public void onClick(View v) {
Intent intent = NoteEditActivity.newIntent(context, mNote);
context.startActivity(intent);
}
public void bind(Note note) {
mNote = note;
text.setText(note.getText());
title.setText(note.getTitle());
date.setText(note.getDate().toString());
}
}
}
并且recyclerView 有LinearLayoutManager。当我尝试刷卡时,我得到了这个错误:
java.lang.IndexOutOfBoundsException: Inconsistency detected. Invalid view holder adapter positionViewHolder{ac354a70 position=10 id=-1, oldPos=10, pLpos:-1 scrap [attachedScrap] tmpDetached no parent} androidx.recyclerview.widget.RecyclerView{aba0d698 VFED.... .F....I. 0,0-1080,1581 #7f08008b app:id/recycler_view}, adapter:com.example.notebook_v2.NotesAdapter@ac317680, layout:androidx.recyclerview.widget.LinearLayoutManager@aba66738, context:com.example.notebook_v2.NoteListActivity@ab9fd968
at androidx.recyclerview.widget.RecyclerView$Recycler.validateViewHolderForOffsetPosition(RecyclerView.java:5715)
at androidx.recyclerview.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:5898)
at androidx.recyclerview.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5858)
at androidx.recyclerview.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5854)
at androidx.recyclerview.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2230)
at androidx.recyclerview.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1557)
at androidx.recyclerview.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1517)
at androidx.recyclerview.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:612)
at androidx.recyclerview.widget.RecyclerView.dispatchLayoutStep1(RecyclerView.java:3875)
at androidx.recyclerview.widget.RecyclerView.dispatchLayout(RecyclerView.java:3639)
at androidx.recyclerview.widget.RecyclerView.onLayout(RecyclerView.java:4194)
at android.view.View.layout(View.java:14817)
at android.view.ViewGroup.layout(ViewGroup.java:4631)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
at android.view.View.layout(View.java:14817)
at android.view.ViewGroup.layout(ViewGroup.java:4631)
at androidx.drawerlayout.widget.DrawerLayout.onLayout(DrawerLayout.java:1231)
at android.view.View.layout(View.java:14817)
at android.view.ViewGroup.layout(ViewGroup.java:4631)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
at android.view.View.layout(View.java:14817)
at android.view.ViewGroup.layout(ViewGroup.java:4631)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1671)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1525)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1434)
at android.view.View.layout(View.java:14817)
at android.view.ViewGroup.layout(ViewGroup.java:4631)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
at android.view.View.layout(View.java:14817)
at android.view.ViewGroup.layout(ViewGroup.java:4631)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1671)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1525)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1434)
at android.view.View.layout(View.java:14817)
at android.view.ViewGroup.layout(ViewGroup.java:4631)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
at android.view.View.layout(View.java:14817)
at android.view.ViewGroup.layout(ViewGroup.java:4631)
at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:1987)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1744)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1000)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5670)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)
at android.view.Choreographer.doCallbacks(Choreographer.java:574)
at android.view.Choreographer.doFrame(Choreographer.java:544)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747)
at android.os.Handler.handleCallback(
我不知道该怎么做,到目前为止,互联网也没有帮助我。enter code here
解决方案
您收到此错误的原因是因为您正在调用notifyItemChanged
而不是notifyItemRemoved
. 您正在删除一个项目,mNotes
因此您的列表比以前少了一个项目。当您调用 时notifyItemChanged
,它会告诉适配器您刚刚更改了一个项目,因此没有删除任何内容。适配器查找要加载的最后一个项目,但那里没有项目!
尝试使用这个:
ItemTouchHelper helper = new ItemTouchHelper(new ItemTouchHelper.SimpleCallback(
0,
ItemTouchHelper.LEFT|ItemTouchHelper.RIGHT)
{
@Override
public boolean onMove(@NonNull RecyclerView recyclerView, @NonNull ViewHolder
viewHolder, @NonNull ViewHolder target) {
return false;
}
@Override
public void onSwiped(@NonNull ViewHolder viewHolder, int direction) {
mNotes.remove(viewHolder.getAdapterPosition());
mAdapter.notifyItemRemoved(viewHolder.getAdapterPosition());
}
});`enter code here`
推荐阅读
- excel - Word 和 Excel 与宏
- azure-sql-database - 如何强制 Azure SQL(无服务器)暂停?
- reactjs - React-persist 不是持久状态
- reactjs - 为什么在使用 Jest 测试 App 组件时重定向到登录页面?
- python - 有没有办法根据二分网络中的有限可能性来规范节点中心性?
- tomcat - 为什么Tomcat在启动后会系统地重新加载我的webapp?
- javascript - Node.js:有什么方法可以在 line-reader 的 eachLine 函数中停止读取文件和整个过程?
- inria-spoon - 如何为狙击模式打印实现 OutputType 过滤器
- python - 如何让程序将所有这些信息写入文本文件
- puppet - Puppet7 代理无法从服务器找到目录