EditText loses focus on Scroll in RecyclerView

懵懂的女人 提交于 2019-12-22 08:31:31

问题


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.


回答1:


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.

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.



来源:https://stackoverflow.com/questions/45238973/edittext-loses-focus-on-scroll-in-recyclerview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!