Capture Keyevent On Recyclerview Views
Following the directions on this blog post I am able to track the selected item on a vertical list Adapter, but I cannot click or long click any item by pressing some key. I've cr
Solution 1:
I have edited the code from the post, by adding regular and long button presses:
@OverridepublicvoidonAttachedToRecyclerView(final RecyclerView recyclerView) {
super.onAttachedToRecyclerView(recyclerView);
mRecyclerView = recyclerView;
// Handle key up and key down and attempt to move selection
recyclerView.setOnKeyListener(newView.OnKeyListener() {
@OverridepublicbooleanonKey(View v, int keyCode, KeyEvent event) {
RecyclerView.LayoutManagerlm= recyclerView.getLayoutManager();
// Return false if scrolled to the bounds and allow focus to move off the listif (event.getAction() == KeyEvent.ACTION_DOWN) {
if (keyCode == KeyEvent.KEYCODE_DPAD_DOWN) {
return tryMoveSelection(lm, 1);
} elseif (keyCode == KeyEvent.KEYCODE_DPAD_UP) {
return tryMoveSelection(lm, -1);
} elseif(KeyEventUtils.isConfirmButton(event)){
Viewview= mRecyclerView.findViewHolderForAdapterPosition(mSelectedItem).itemView;
if((event.getFlags() & KeyEvent.FLAG_LONG_PRESS)==KeyEvent.FLAG_LONG_PRESS) {
view.performLongClick();
}
else{
view.performClick();
}
returntrue;
}
}
returnfalse;
}
});
}
Now it works properly, I don't know if it's the most elegant way.
Post a Comment for "Capture Keyevent On Recyclerview Views"