Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EditText loses focus on Scroll in RecyclerView

I have a RecyclerView which have EditText as its list items. When i scroll the RecyclerView, the EditText loses focus when the item goes off screen. So the focus does not remain on the EditText when it comes back on the screen on scroll.

I want the focus to remain on the same item. For that i also tried storing position of the item on focus and reassigning the focus in onBindViewHolder. But that slow downs the scrolling.

I also tried this with ListView but there is another kind of focus related problem. Focus jumps there.

Have searched for this lot on SO and Googled a lot. but always found answers like android:focusableInTouchMode="true" and android:descendantFocusability="afterDescendants" which does not work.

Any help would be highly appreciated.

like image 930
kanudo Avatar asked Jul 21 '17 13:07

kanudo


1 Answers

The whole point of a RecyclerView is to reuse the views so that the phone doesn't have to keep all the rows in memory at once and doesn't have to be constantly destroying and creating views. When your EditText leaves the screen, the phone takes that view, resets its contents and moves it to the bottom of the incoming view stack.

RecyclerView diagram

This means that once your EditText leaves the screen it no longer exists. It cannot keep focus because it is removed from the layout.

The only way around this would be what you mentioned, which is storing the position, checking when that position comes onscreen, and manually restoring focus.

like image 193
Elan Hamburger Avatar answered Sep 19 '22 00:09

Elan Hamburger