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

本文分析了Android RecyclerView出现java.lang.IllegalStateException异常的原因,并提供了解决方案。异常发生在RecyclerView同时执行下拉刷新和加载更多操作时。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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!
//   ┃   ┗━━━┓
//   ┃       ┣┓
//   ┃       ┏┛
//   ┗┓┓┏━┳┓┏┛
//     ┃┫┫ ┃┫┫
//     ┗┻┛ ┗┻┛


谢谢

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值