I have RecyclerView
, some kind of chat. Items are TextViews
The layout is reversed. Input field below RecyclerView
. When I scroll to the bottom of RecyclerView
and click to the bottom item it gets focused (only when it gets focused, not every click) and RecyclerView
scrolls automatically to the top of that item (when the text in item is larger than screen height).
Also when the keyboard is visible and I click to RecyclerView
item - keyboard hides and recycler view scrolls to the top of that item, how to disable such behavior?
I want it to stay at the same position when it was when the keyboard was opened or where it was when I clicked on some item.
Keyboard is hiding with this method
* * * *
inputView.setOnFocusChangeListener((v, hasFocus) -> {
if (!hasFocus){
hideKeyboard();
}
});
* * * *
public void hideKeyboard() {
InputMethodManager keyboard = (InputMethodManager)
getSystemService(Context.INPUT_METHOD_SERVICE);
keyboard.hideSoftInputFromWindow(inputView.getWindowToken(), 0);
}
And RecyclerView
configured in such way:
RecyclerView recyclerView = (RecyclerView) view.findViewById(R.id.messageList);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext());
linearLayoutManager.setReverseLayout(true);
recyclerView.setLayoutManager(linearLayoutManager);
recyclerView.setAdapter(chatAdapter = new chatAdapter(getContext()));
If I make a button and will call hideKeyBoard()
by clicking that button - its ok, it does not scroll, but when I click on RecyclerView item - it gets focused and scroll to top of that item
I found a solution, I extended LinearLayoutManager and override some methods. Now it not scrolling to focused items. Maybe someone knows better solution?
This answer might be your solution as well: RecyclerView scrolls to top after keyboard hides
Android Developer Site link