RecyclerView IndexOutOfBoundsException异常

RecyclerView IndexOutOfBoundsException

为什么当我使用循环删除 RecyclerView 中的某些项目时会执行异常? 我在适配器中使用了 Collentions.synchronizedMap 并且 'deleteItem method' 也使用了同步(片段中的方法)。

public void elementController(JsonObject jsonObject , String type)   {
    if ( jsonObject == null || type == null )    {
        return;
    }

    int position =0 , resultPosition =0;
    if ( type.equals("update") || type.equals("delete"))    {

        String id = jsonObject.get(ELEMENT_ID).getAsString();
        Map<String , Element> map = gridFragment.getMap();

        synchronized (map) {  
            for (String s : map.keySet()) {
                if (s.equals(id)) {
                    resultPosition = position;
                } else {
                    position++;
                }
            }
        }
    }

    if(position-1 > gridFragment.getmAdapter().getData().size() || position <0)   {
        return;
    }
    switch (type)   {
        case "add":
            if (gridFragment.addElement(MyJsonParser.ElementParse(jsonObject),0)){
                LogUtils.logDebug(TAG,"add end");
            }
            break;
        case "update":
            if(gridFragment.updateElement( updateParser(jsonObject),resultPosition)){
                LogUtils.logDebug(TAG,"update end");
            }
            break;
        case "delete":
            if(gridFragment.deleteElement(jsonObject.get(ELEMENT_ID).getAsString(),resultPosition)){
                LogUtils.logDebug(TAG,"delete end");
            }
            break;
    }
}

public boolean deleteElement(final String id , final int position){
    new Thread(new Runnable() {
        @Override
        public void run() {
            getActivity().runOnUiThread(new Runnable(){
                @Override
                public void run() {
                    synchronized (map) {
                        map.remove(id);
                        mAdapter.setData(map);
                        mAdapter.notifyItemRemoved(position);
                    }
                }
            });
        }
    }).start();

    return true;
} 

我的错误日志:

java.lang.IndexOutOfBoundsException: Inconsistency detected. Invalid item position 0(offset:0).state:4
        at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:3382)
        at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:3340)
        at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:1810)
        at android.support.v7.widget.GridLayoutManager.layoutChunk(GridLayoutManager.java:356)
        at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1269)
        at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:523)
        at android.support.v7.widget.GridLayoutManager.onLayoutChildren(GridLayoutManager.java:151)
        at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:1942)
        at android.support.v7.widget.RecyclerView.resumeRequestLayout(RecyclerView.java:1171)
        at android.support.v7.widget.RecyclerView.run(RecyclerView.java:167)
        at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)
        at android.view.Choreographer.doCallbacks(Choreographer.java:574)
        at android.view.Choreographer.doFrame(Choreographer.java:543)
        at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747)
        at android.os.Handler.handleCallback(Handler.java:733)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:212)
        at android.app.ActivityThread.main(ActivityThread.java:5137)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:902)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:718)
        at dalvik.system.NativeStart.main(Native Method)

未找到设备

 mAdapter.notifyItemRemoved(position); 

这只会通知正在侦听布局更改的观察者。

改为尝试调用:

mAdpater.nofityDataChanged()

是的,这是因为并发访问。我遇到过。使用 Iterator 或一个不错的技巧很容易修复:你从最后一项开始删除。但是 Z-A 顺序 Iterator 更好。

最近我改进并创建了一个使用此解决方案的 FlexibleAdapter。 请同时查看说明和完整的工作示例:https://github.com/davideas/FlexibleAdapter

没有阅读您的代码,因为我不熟悉您使用的 类,但我知道这个问题的解决方法。当删除的行元素不是最后一个时出现问题,因为dataList索引(你的ArrayList存储数据)和方法onBindViewHolder中的参数final int position之间的索引差异。让我以图形方式解释这个问题:

假设我们有一个三行的 RecyclerView,我们删除了第 3 行,在 dataList 索引 2,(注意删除的不是最后一个元素)删除后,dataList最后一个元素的索引从 2 减少 3 但是,final int position 仍然将其位置检索为 3,这导致了问题。因此,删除后,您不能使用 final int position 作为要从 dataList.

中删除的元素的索引

要解决此问题,请引入 int shift=0 并在 while 循环中,try 删除 (position-shift) 处的第三项,如果失败(在我的情况下,它将)增加班次并再次尝试删除它,直到没有异常发生。

 public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
    
    ...
    
        holder.removeButton.setOnClickListener(new View.OnClickListener(){ //button used to remove rows
                    @Override
                    public void onClick(View view) {
                        if (position == dataList.size() - 1) { // if last element is deleted, no need to shift
                            dataList.remove(position);
                            notifyItemRemoved(position);
                        } else { // if the element deleted is not the last one
                            int shift=1; // not zero, shift=0 is the case where position == dataList.size() - 1, which is already checked above
                            while (true) {
                                try {
                                    dataList.remove(position-shift);
                                    notifyItemRemoved(position);
                                    break;
                                } catch (IndexOutOfBoundsException e) { // if fails, increment the shift and try again
                                    shift++;
                                }
                            }
                        }
                    }
                });
   ...
    }

为了动画不消失,使用:

holder.deleteItem.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                itemList.remove(position);
                notifyItemRemoved(position);
                notifyItemRangeChanged(position, itemList.size());
            }
        });

可以这样解决:

dataList.remove(item)
notifyItemRemoved(adapterPosition)

其实,比你想象的要简单,把map.notifyItemRemoved(position)移到这个

...
public void run() {
                    synchronized (map) {
                        map.remove(id);
                        mAdapter.setData(map);
                        mAdapter.notifyItemRemoved(position);
                    }
                }

至此

public void run() {
                    synchronized (map) {
                        mAdapter.notifyItemRemoved(position);
                        map.remove(id);
                        mAdapter.setData(map);
                    }
                }

它对我有用,像这样:

fun removeData(position: Int) {
    this.notifyItemRemoved(position)
    this.notifyItemRangeChanged(position, imgList.size)
    this.imgList.removeAt(position)
}