我有一些图像排列在水平网格中.我想使用键事件从左到右和从右到左移动网格.当我穿过网格时,发生了两件事
>选择第二个位置的项目.例如,我在位置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 referenceat com.winettv.recylcervIEwwithdpad.GrIDVIEw_Recycler_Adapter.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); } }}
总结 以上是内存溢出为你收集整理的java – RecyclerView在滚动时崩溃全部内容,希望文章能够帮你解决java – RecyclerView在滚动时崩溃所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)