RecycleView加载更多完成后选item崩溃

问题

项目使用XRecyclerView进行替代掉ListView,然后在上拉加载更多,加载完之后,随意选择一个Item后便会崩溃掉。报错

java.lang.IllegalArgumentException: Scrapped or attached views may not be recycled. isScrap:false isAttached:true
at android.support.v7.widget.RecyclerView$Recycler.recycleViewHolderInternal(RecyclerView.java:5659)
    at android.support.v7.widget.RecyclerView$Recycler.recycleView(RecyclerView.java:5603)
    at android.support.v7.widget.GapWorker.prefetchPositionWithDeadline(GapWorker.java:277)
    at android.support.v7.widget.GapWorker.flushTaskWithDeadline(GapWorker.java:324)
    at android.support.v7.widget.GapWorker.flushTasksWithDeadline(GapWorker.java:337)
    at android.support.v7.widget.GapWorker.prefetch(GapWorker.java:344)
    at android.support.v7.widget.GapWorker.run(GapWorker.java:370)
    at android.os.Handler.handleCallback(Handler.java:743)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:150)
    at android.app.ActivityThread.main(ActivityThread.java:5665)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:822)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:712)

网上还有是刷新会报这个错误。从字面上意思是整个大的RecycleView还拥有着焦点,所以突然选择item就导致出错。

解决

通过对比几种情况下均报这种错误的相同点后发现,都是在列表数据发生变化后出现的错误。在更改了列表数据后进行展示处理,然后在这个过程为正确处理完成即操作其他操作比如选择item、切换不同列表等。
于是解决方案即及时准确提醒列表刷新数据即可。(研究了一圈后发现,其实这么简单。)

*刷新与加载更多

el.recycler.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                ru.times = 1;
                new Handler().postDelayed(new Runnable() {
                    public void run() {
                        // 刷新列表
                        mData.clear();
                        el.mAdapter.notifyDataSetChanged();
                        getNotiListDates(ru.times, ru.messageType);
                        el.recycler.refreshComplete();
                    }

                }, 1000); // refresh data here
            }

            @Override
            public void onLoadMore() {
                long countPages = (ru.counts - 1) / ru.pageSize + 1;
                if (ru.times < countPages) {
                    new Handler().postDelayed(new Runnable() {
                        public void run() {
                            el.recycler.loadMoreComplete();
                            el.mAdapter.notifyDataSetChanged();
                            getNotiListDates(ru.times, ru.messageType);
                            el.recycler.refreshComplete();
                        }
                    }, 1000);
                } else {
                    new Handler().postDelayed(new Runnable() {
                        public void run() {
                            el.recycler.loadMoreComplete();
                            el.mAdapter.notifyDataSetChanged();
                        }
                    }, 1000);
                }
                ru.times++;
            }
        });

*切换不同类型列表,(通知就是在清除数据后el.mAdapter.notifyDataSetChanged())

switch (v.getId()) {
        case R.id.notif_action:// 行动消息Button监听
            mData.clear();
            el.mAdapter.notifyDataSetChanged();
            ru.times = 1;
            ru.messageType = 1;
            getNotiListDates(ru.initPage, ru.messageType);
            el.actionBtn.setBackgroundResource(R.drawable.button_my_task);
            el.actionBtn.setTextColor(v.getResources().getColor(R.color.white));
            el.otherBtn.setBackgroundResource(R.drawable.button_unfinished);
            el.otherBtn.setTextColor(v.getResources().getColor(R.color.blue));
            break;
        case R.id.notif_normal:// 普通消息Button监听
            mData.clear();
            el.mAdapter.notifyDataSetChanged();
            ru.times = 1;
            ru.messageType = 2;
            getNotiListDates(ru.initPage, ru.messageType);
            el.actionBtn.setBackgroundResource(R.drawable.button_unfinished);
            el.actionBtn.setTextColor(v.getResources().getColor(R.color.blue));
            el.otherBtn.setBackgroundResource(R.drawable.button_my_task);
            el.otherBtn.setTextColor(v.getResources().getColor(R.color.white));
            break;

在数据clear后加上el.mAdapter.notifyDataSetChanged()。便解决崩溃问题。在数据发生更新后,列表加载新数据前,多刷新一次列表数据即可。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值