RecycleView异常Added View has RecyclerView as parent but view is not a real child. Unfiltered index:0

Android RecycleView  异常 

java.lang.IllegalStateException:Added View has RecyclerView as parent but view is not a real child. Unfiltered index:0


log日志:

07-16 14:54:12.075: E/AndroidRuntime(20475): java.lang.IllegalStateException:
Added View has RecyclerView as parent but view is not a real child. Unfiltered
index:0
07-16 14:54:12.075: E/AndroidRuntime(20475):     at
android.support.v7.widget.RecyclerView$LayoutManager.addViewInt(RecyclerView.java:6720)
07-16 14:54:12.075: E/AndroidRuntime(20475):     at
android.support.v7.widget.RecyclerView$LayoutManager.addView(RecyclerView.java:6686)
07-16 14:54:12.075: E/AndroidRuntime(20475):     at
android.support.v7.widget.RecyclerView$LayoutManager.addView(RecyclerView.java:6674)


在使用RecycleView中 偶尔出现了该异常 

追踪该异常发现出自

android.support.v7.widget.RecyclerView$LayoutManager.addViewInt(RecyclerView.java:6720)


if (child.getParent() == mRecyclerView) { // it was not a scrap but a valid child
                // ensure in correct position
                int currentIndex = mChildHelper.indexOfChild(child);
                if (index == -1) {
                    index = mChildHelper.getChildCount();
                }
                if (currentIndex == -1) {
                    throw new IllegalStateException("Added View has RecyclerView as parent but"
                            + " view is not a real child. Unfiltered index:"
                            + mRecyclerView.indexOfChild(child));
                }
                if (currentIndex != index) {
                    mRecyclerView.mLayout.moveView(currentIndex, index);
                }
            }

发现异常原因是  :

recycleView同时进行 "下拉刷新" 和 "加载更多" 而产生冲突   .

recycleView  执行异步的"加载更多"操作后 , 当调用recycleView   的addviewInt方法填充数据时,   发现列表已经被"下拉刷新"删除了. 找不到常规的child.


解决异常:

禁止 RecycleView  "下拉刷新" 和 "加载更多"  同时执行  . 

同一时间只允许用户使用一种动作 (即: 刷新不加载  加载不刷新 )


//  ┏┓   ┏┓
//┏┛┻━━━┛┻┓
//┃       ┃
//┃   ━   ┃
//┃ ┳┛ ┗┳ ┃
//┃       ┃
//┃   ┻   ┃
//┃       ┃
//┗━┓   ┏━┛
//   ┃   ┃   神兽保佑
//   ┃   ┃   代码无BUG!
//   ┃   ┗━━━┓
//   ┃       ┣┓
//   ┃       ┏┛
//   ┗┓┓┏━┳┓┏┛
//     ┃┫┫ ┃┫┫
//     ┗┻┛ ┗┻┛


谢谢

你可能感兴趣的:(android)