我有一些圖像排列在水平網格中。我想使用關鍵事件從左到右和從右到左移動我的網格。當我通過網格移動時,發生了兩件事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)
[findViewHolderForPosition](https://developer.android.com/reference/android/support/v7/widget/RecyclerView.html#findViewHolderForPosition(INT))已被棄用,你試圖與[findViewHolderForAdapterPosition替換它](https://developer.android.com/reference/android/support/v7/widget/RecyclerView.html#findViewHolderForAdapterPosition(INT))? – Rehan