编程语言
首页 > 编程语言> > java – RecyclerView在滚动时崩溃

java – RecyclerView在滚动时崩溃

作者:互联网

我有一些图像排列在水平网格中.我想使用键事件从左到右和从右到左移动网格.当我穿过网格时,发生了两件事

>选择第二个位置的项目.例如,我在位置0并且想要选择位置1处的下一个项目,但我的网格选择位置2项目并跳过位置1项目.
>在滚动时我得到空指针异常

if(keyCode == KeyEvent.KEYCODE_DPAD_RIGHT)
  {
     if (mCurrentSelectedPosition + 1 < getItemCount()) {
    Log.e("current position1",Integer.toString(mCurrentSelectedPosition));

    gridHolder = (GridView_Holder) mRecyclerView.findViewHolderForPosition(mCurrentSelectedPosition);
    gridHolder.itemView.setSelected(false);
    notifyItemChanged(mCurrentSelectedPosition);
    notifyDataSetChanged();

    int lastItem = lm.findLastCompletelyVisibleItemPosition();
    Log.e("last item",Integer.toString(lastItem));

    if ((mCurrentSelectedPosition + 1) > lastItem) {
        //  Log.e("Is less", "true");
        recyclerView.smoothScrollToPosition(mCurrentSelectedPosition + 1);
        Log.e("current position2",Integer.toString(mCurrentSelectedPosition));

    } else {
        gridHolder = (GridView_Holder) mRecyclerView.findViewHolderForPosition(mCurrentSelectedPosition + 1);
        gridHolder.itemView.setSelected(true);
        Log.e("current position3",Integer.toString(mCurrentSelectedPosition));
    }

    mCurrentSelectedPosition += 1;
    Log.e("current position4",Integer.toString(mCurrentSelectedPosition));
}
return true;
}

滚动监听器

gridRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener(){
        @Override
        public void onScrolled(RecyclerView recyclerView, int x, int y)
        {
            super.onScrolled(recyclerView, x, y);
            try {
                GridView_Holder gridHolder;
                gridHolder= (GridView_Holder) recyclerView.findViewHolderForPosition(adapter.mCurrentSelectedPosition);

                gridHolder.itemView.setSelected(true);
            }catch(Exception e){
               Log.e("exception",e.getMessage());
           }

        }

这是我的logcat

java.lang.NullPointerException: Attempt to read from field 'android.view.View android.support.v7.widget.RecyclerView$ViewHolder.itemView' on a null object reference
at com.winettv.recylcerviewwithdpad.GridView_Recycler_Adapter$1.onKey(GridView_Recycler_Adapter.java:193)
at android.view.View.dispatchKeyEvent(View.java:9230)
at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1635)
at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1640)
at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1640)
at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1640)
at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1640)
at android.widget.ScrollView.dispatchKeyEvent(ScrollView.java:379)
at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1640)
at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1640)
at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1640)
at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1640)
at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1640)
at com.android.internal.policy.PhoneWindow$DecorView.superDispatchKeyEvent(PhoneWindow.java:2395)
at com.android.internal.policy.PhoneWindow.superDispatchKeyEvent(PhoneWindow.java:1727)
at android.app.Activity.dispatchKeyEvent(Activity.java:2725)
at android.support.v7.app.AppCompatActivity.dispatchKeyEvent(AppCompatActivity.java:543)
at android.support.v7.view.WindowCallbackWrapper.dispatchKeyEvent(WindowCallbackWrapper.java:53)
at android.support.v7.app.AppCompatDelegateImplBase$AppCompatWindowCallbackBase.dispatchKeyEvent(AppCompatDelegateImplBase.java:315)

解决方法:

当我在滚动期间开发带有自定义翻译的库时,我遇到了同样的问题.只有在快速滚动时,当我尝试从List访问View元素时,这是null,这里是NPE.

所以有一个非常简单的解决方案只是跳过元素,这是空的,并继续新的元素.就我而言,它是这样的:

// Other init....
for (int i = 0; i < MainListView.getChildCount; i++) {
   if (MainListView.getChildAt(i) == null)
      continue;
   // Continue base behavior
}

发生这种情况,基于ScrollView(它是RecycleView,ListView等的父级)当我们尝试访问正在重绘的View时.无论如何只需简单的Null检查就可以解决您的问题.

更新1:

同样基于缓存查看RecyclerView的布局,您还需要另一个选项.见下文.

gridRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener(){
        @Override
        public void onScrolled(RecyclerView recyclerView, int x, int y)
        {
            super.onScrolled(recyclerView, x, y);
            GridView_Holder gridHolder;
            gridHolder= (GridView_Holder) recyclerView.findViewHolderForPosition(adapter.mCurrentSelectedPosition);
            if (gridHolder != null && gridHolder.itemView != null) {
                   gridHolder.itemView.setSelected(true);
            }
        }
}

标签:key-events,scroller,android,java,android-recyclerview
来源: https://codeday.me/bug/20190823/1702648.html