美文网首页
关于RecyclerView下拉的时候马上上划崩溃的问题

关于RecyclerView下拉的时候马上上划崩溃的问题

作者: 二贝呀 | 来源:发表于2018-08-18 14:37 被阅读106次

之前用的一个万能adapter的三方库所以没发现这个问题,在一次写Demo的时候在我下拉刷新的时候recyclerView崩溃了,而且崩溃的地方出在RecyclerView本身的源码之中具体如下:

java.lang.IndexOutOfBoundsException:Inconsistency detected. Invalid item position150(offset:150).state:153at android.support.v7.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:5504) at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5440) at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5436) at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2224) at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1551) at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1511) at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:595) at android.support.v7.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:3583) at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:3312) at android.support.v7.widget.RecyclerView.consumePendingUpdateOperations(RecyclerView.java:1618) at android.support.v7.widget.RecyclerView$ViewFlinger.run(RecyclerView.java:4702) at android.view.Choreographer$CallbackRecord.run(Choreographer.java:911) at android.view.Choreographer.doCallbacks(Choreographer.java:686) at android.view.Choreographer.doFrame(Choreographer.java:619) at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:897) at android.os.Handler.handleCallback(Handler.java:739) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:148) at android.app.ActivityThread.main(ActivityThread.java:7409) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)

其实这是RecyclerView本身源码的原因,个人认为是在adapter还没得到数据源之前再次滑动时viewholder得到的数据源发生了变化,在尝试了很多解决办法之后最后解决了,先看下网上大多人的解决办法吧

publicclassWrapContentLinearLayoutManagerextendsLinearLayoutManager{

publicWrapContentLinearLayoutManager(Context context){

super(context);

    }

publicWrapContentLinearLayoutManager(Context context,intorientation,booleanreverseLayout){

super(context, orientation, reverseLayout);

    }

publicWrapContentLinearLayoutManager(Context context, AttributeSet attrs,intdefStyleAttr,intdefStyleRes){

super(context, attrs, defStyleAttr, defStyleRes);

    }

@Override

public void onLayoutChildren(RecyclerView.Recycler recycler, RecyclerView.State state){

try{

super.onLayoutChildren(recycler, state);

}catch(IndexOutOfBoundsException e) {

            e.printStackTrace();

        }

}

此方法是重写了一个LayoutManager布局管理器,然后自己在程序走到RecyclerView之前进行了捕获这个角标越界的异常防止程序crash,有很多人表示已经解决了问题,但是不包括所有人能解决,显然我说的就是我自己了,哈哈!!!

最后我在想能不能在recyclerview的滑动事件上做手脚,在进行上下滑动的时候将boolean设置为true,在拿到数据源之后将boolben设置为false,默认为false,具体写法如下:

private boolean mIsRefreshing = false;

mRecyclerView.setOnTouchListener(

         newView.OnTouchListener() {

        @Override

        public boolean onTouch(View v, MotionEvent event) {

                  if(mIsRefreshing) {

                        return true;

                  }else{

                       return false;

                  }

          }

         } );

       //当刷新时设置

      //mIsRefreshing=true;

     //刷新完毕后还原为false

     //mIsRefreshing=false;

OK,解决了!为此做个笔记方便以后查看

相关文章

网友评论

      本文标题:关于RecyclerView下拉的时候马上上划崩溃的问题

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