public void onAdapterDataChanged() { final Adapter adapter = mRecyclerView.getAdapter(); if (mChoiceMode == ChoiceMode.NONE || adapter == null || !adapter.hasStableIds()) { return; } final int itemCount = adapter.getItemCount(); // Clear out the positional check states, we'll rebuild it below from IDs. mCheckedStates.clear(); for (int checkedIndex = 0; checkedIndex < mCheckedIdStates.size(); checkedIndex++) { final long currentId = mCheckedIdStates.keyAt(checkedIndex); final int currentPosition = mCheckedIdStates.valueAt(checkedIndex); final long newPositionId = adapter.getItemId(currentPosition); if (currentId != newPositionId) { // Look around to see if the ID is nearby. If not, uncheck it. final int start = Math.max(0, currentPosition - CHECK_POSITION_SEARCH_DISTANCE); final int end = Math.min(currentPosition + CHECK_POSITION_SEARCH_DISTANCE, itemCount); boolean found = false; for (int searchPos = start; searchPos < end; searchPos++) { final long searchId = adapter.getItemId(searchPos); if (currentId == searchId) { found = true; mCheckedStates.put(searchPos, true); mCheckedIdStates.setValueAt(checkedIndex, searchPos); break; } } if (!found) { mCheckedIdStates.delete(currentId); mCheckedCount--; checkedIndex--; } } else { mCheckedStates.put(currentPosition, true); } } }
@Override public boolean handleKeyboardShortcutRepeat( @NonNull final KeyboardShortcutsHandler handler, final int keyCode, final int repeatCount, @NonNull final KeyEvent event, int metaState) { final String action = handler.getKeyAction(CONTEXT_TAG_NAVIGATION, keyCode, event, metaState); if (action == null) return false; final int direction; switch (action) { case ACTION_NAVIGATION_PREVIOUS: { direction = -1; break; } case ACTION_NAVIGATION_NEXT: { direction = 1; break; } case ACTION_NAVIGATION_PAGE_DOWN: { RecyclerViewUtils.pageScroll(view, manager, 1); return true; } case ACTION_NAVIGATION_PAGE_UP: { RecyclerViewUtils.pageScroll(view, manager, -1); return true; } default: { return false; } } final View focusedChild = RecyclerViewUtils.findRecyclerViewChild(view, manager.getFocusedChild()); final int position; final int firstVisibleItemPosition = manager.findFirstVisibleItemPosition(); final int lastVisibleItemPosition = manager.findLastVisibleItemPosition(); final int itemCount = adapter.getItemCount(); final boolean backupOutsideRange = positionBackup > lastVisibleItemPosition || positionBackup < firstVisibleItemPosition; if (focusedChild != null) { position = view.getChildLayoutPosition(focusedChild); } else if (firstVisibleItemPosition == 0) { position = -1; } else if (lastVisibleItemPosition == itemCount - 1) { position = itemCount; } else if (direction > 0 && backupOutsideRange) { position = firstVisibleItemPosition; } else if (direction < 0 && backupOutsideRange) { position = lastVisibleItemPosition; } else { position = positionBackup; } positionBackup = position; RecyclerViewUtils.focusNavigate(view, manager, position, direction); return true; }