Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EditText in RecyclerView view is detached when keyboard opened

I am attempting to create a long form with a variable number of EditText elements inside a RecyclerView. The layout consists of a FrameLayout, a RecyclerView for the form sections, then a nested RecyclerView inside of a CardView to hold all of the form elements for that section (see animation below).

Normally when an element is focused the keyboard will appear, the window is resized, and the view is scrolled so the element appears above the keyboard. The issue I am having is when an element is selected at the bottom of the screen and top of a section the window is resized and the RecyclerView detaches the section's view, causing the EditText to not exist when it tries to focus. This manifests itself as the keyboard popping open momentarily then closing as focus is returned to the FrameLayout.

RecyclerView Focus Issue

I am using android:windowSoftInputMode="adjustResize" in my activity and do not want to use adjustPan because it does not provide the best user experience.

I've tried adding a click listener to the EditText then scrolling the RecyclerView up so that the view won't get destroyed when the window resizes but this feels hacky and it is hard to detect precisely how far up the RecyclerView should be scrolled. You also get some weird jumps. In order to do this you have to turn off the focusability of the EditText which isn't ideal either because it breaks navigation and accessibility.

Trying to do something like recyclerView.getRecycledViewPool().setMaxRecycledViews(0, SECTION_COUNT); doesn't work either because it doesn't prevent the views from being detached.

How do I keep the view from being detached when the window is resized for the keyboard so the EditText receives focus?

like image 736
Victor Rendina Avatar asked Nov 08 '22 21:11

Victor Rendina


1 Answers

I solve this with by

android.os.Handler().postDelayed({recycle.smoothScrollToPosition(0)},100)
like image 84
Ghost Jkeee Avatar answered Nov 15 '22 11:11

Ghost Jkeee