美文网首页
悬案解决: java.lang.IndexOutOfBounds

悬案解决: java.lang.IndexOutOfBounds

作者: 朱_c713 | 来源:发表于2020-05-08 17:07 被阅读0次

    monkey经常会出现这个问题:
    java.lang.IndexOutOfBoundsException: Inconsistency detected. Invalid item position 6(offset:6).state:11 androidx.recyclerview.widget.RecyclerView错误
    不幸的是新项目,手点,也能点出此问题

    第一种:解决方案:包装LinearLayoutManager

    public class RecyclerViewNoBugLinearLayoutManager extends LinearLayoutManager {
        public RecyclerViewNoBugLinearLayoutManager(Context context) {
            super( context );
        }
    
        public RecyclerViewNoBugLinearLayoutManager(Context context, int orientation, boolean reverseLayout) {
            super( context, orientation, reverseLayout );
        }
    
        public RecyclerViewNoBugLinearLayoutManager(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
            super( context, attrs, defStyleAttr, defStyleRes );
        }
    
        @Override
        public void onLayoutChildren(RecyclerView.Recycler recycler, RecyclerView.State state) {
            try {
                //try catch一下
                super.onLayoutChildren( recycler, state );
            } catch (IndexOutOfBoundsException e) {
                e.printStackTrace();
            }
    
        }
    }
    
    

    问题存在。

    相关链接:https://www.jianshu.com/p/7c675d5d2074

    最终问题解决:

     private fun beginSearch() {
          mSearchResultListData.clear()
            val keyWords = etSearchTrim
            OkGoUtils.getRequest(
                ConstantUtils.getUrlConstant().video_searcher + "?keywords=" + keyWords + "&page=1",
                object : JsonCallBack<SeacherListBean>(SeacherListBean::class.java, this) {
                    override fun onCacheSuccess(response: Response<SeacherListBean>?) {
                        //                super.onCacheSuccess(response);
                    }
    
                    override fun onSuccess(response: Response<SeacherListBean>) {
                        //获取搜索内容
                  
                        val body = response.body()
                        et_seacher!!.setText(keyWords)
                        type = 0
                        mSearchResultListData.addAll(body.data)
                        if (mSearchResultListData.size > 0) {
                            mSearchResultAdapter!!.setNewData(mSearchResultListData)
                            refreshSearchResult(body.total)
                        } else {
                            showNoData()
                        }
                    }
                })
        }
    
     private fun beginSearch() {
    
            val keyWords = etSearchTrim
            OkGoUtils.getRequest(
                ConstantUtils.getUrlConstant().video_searcher + "?keywords=" + keyWords + "&page=1",
                object : JsonCallBack<SeacherListBean>(SeacherListBean::class.java, this) {
                    override fun onCacheSuccess(response: Response<SeacherListBean>?) {
                        //                super.onCacheSuccess(response);
                    }
    
                    override fun onSuccess(response: Response<SeacherListBean>) {
                        //获取搜索内容
                        mSearchResultListData.clear()
                        val body = response.body()
                        et_seacher!!.setText(keyWords)
                        type = 0
                        mSearchResultListData.addAll(body.data)
                        if (mSearchResultListData.size > 0) {
                            mSearchResultAdapter!!.setNewData(mSearchResultListData)
                            refreshSearchResult(body.total)
                        } else {
                            showNoData()
                        }
                    }
                })
        }
    

    移动了这一行的代码位置,相比大家也都清楚为甚么。原来的垃圾代码导致。

    原来的下拉加载实在是不知所云:
    期间还牵涉到两个方法:

      private fun refreshSearchResult(total: Int) {
    
            if (mSearchResultListData.size == 0) {
                mSearchResultAdapter!!.loadMoreEnd()
            }
            //如果总页数=1不用刷新
            if (total > 1) {
                mSearchResultAdapter!!.setOnLoadMoreListener {
                    page++
                    getLoadData()
                }
            } else {
                mSearchResultAdapter!!.loadMoreEnd(false)
            }
    
    
        }
    
       private fun getLoadData() {
            OkGoUtils.getRequest(
                ConstantUtils.getUrlConstant().video_searcher + "?keywords=" + etSearchTrim + "&page=" + page,
                object : JsonCallBack<SeacherListBean>(SeacherListBean::class.java, this) {
    
                    override fun onStart(request: Request<SeacherListBean, out Request<*, *>>?) {
                        //                super.onStart(request);
                    }
    
                    override fun onCacheSuccess(response: Response<SeacherListBean>?) {
                        //                super.onCacheSuccess(response);
                    }
    
                    override fun onError(response: Response<SeacherListBean>) {
                        super.onError(response)
                        //触发搜索延迟
                        mSearchResultAdapter!!.loadMoreFail()
                    }
    
                    override fun onSuccess(response: Response<SeacherListBean>) {
                        if (response.body().data.size > 0) {
                            mSearchResultAdapter!!.addData(response.body().data)
                        }
                        //如果不够一页就显示更多布局
                        if (response.body().data.size < response.body().per_page) {
                            mSearchResultAdapter!!.loadMoreEnd(false)
                        } else {
                            mSearchResultAdapter!!.loadMoreComplete()
                        }
    
                    }
                })
        }
    

    可以看到他为了下载加载,写了三个方法,并且getLoadData和beginSearch,两个方法高度类似。只是传page的地方不一致。

    解决方案:全部换成一个方法,beginSearch,将page变成活的参数,类似于getLoadData,调用轮子自带的下拉刷新api,重新写onSucess中方法。

        private fun beginSearch() {
    
            val keyWords = etSearchTrim
            OkGoUtils.getRequest(
                ConstantUtils.getUrlConstant().video_searcher + "?keywords=" + keyWords + "&page="+page,
                object : JsonCallBack<SeacherListBean>(SeacherListBean::class.java, this) {
                    override fun onCacheSuccess(response: Response<SeacherListBean>?) {
                        //                super.onCacheSuccess(response);
                    }
    
                    override fun onSuccess(response: Response<SeacherListBean>) {
    
                        val body = response.body()
                        mSearchResultAdapter?.addData(body.data)
                        if (body.next_page_url==null) {
                            mSearchResultAdapter?.loadMoreEnd(false)
                        } else {
                            page++
                            mSearchResultAdapter?.loadMoreComplete()
                        }
                    }
    
                    override fun onError(response: Response<SeacherListBean>?) {
                        super.onError(response)
                        mSearchResultAdapter?.loadMoreFail()
                    }
                })
        }
    

    相关文章

      网友评论

          本文标题:悬案解决: java.lang.IndexOutOfBounds

          本文链接:https://www.haomeiwen.com/subject/pidfnhtx.html