Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android RecyclerView Edittext issue

I'm creating a list of editable items (received from backend). I'm using the "new" recyclerview for this. There a couple of possible viewTypes in my recyclerview:

  • checkbox
  • spinner
  • edittext

The problem I'm having is with the EditText gaining focus. AdjustResize kicks in fine and my keyboard is shown. But the EditText that has gained focus isn't visible anymore in the list. (position in the list in the now resized portion is below the visible positions). I don't want to be using AdjustPan in this case because on top there is a pager & stuff I would like to keep fixed there..

like image 691
Jordy Avatar asked May 05 '15 10:05

Jordy


2 Answers

Using this in layout worked : android:descendantFocusability="beforeDescendants"

<android.support.v7.widget.RecyclerView
        android:id="@+id/recyclerview"
        android:descendantFocusability="beforeDescendants"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@+id/extrasLayout"
        android:layout_below="@+id/anchorView"
        android:layout_marginTop="@dimen/margin5"
        android:fastScrollEnabled="false"
        />

Manifest file : Add this to the activity section android:windowSoftInputMode="stateHidden|adjustPan"

<activity
            android:name=".activites.CartActivity"
            android:label="@string/title_activity_cart"
            android:exported="true"
            android:windowSoftInputMode="stateHidden|adjustPan"
            android:parentActivityName=".activites.HomeActivity"
            android:screenOrientation="portrait">
            <meta-data
                android:name="android.support.PARENT_ACTIVITY"
                android:value=".activites.HomeActivity"/>
        </activity>
like image 155
Alok Omkar Avatar answered Sep 28 '22 23:09

Alok Omkar


I've had the same issue. Long story short - there is no right and elegant solution. Focus handling was always a big pain in Android.

There multiple reasons why you are loosing focus:

  • Keyboard hides the descendant EditText views
  • Next EditText is not rendered, because RecycleView hasn't even created a ViewHolder for it. Example: you have 10 views, and 5 of them are on screen and others aren't visible, because those are below.
  • Some other view consumes focus, in example CheckBox or some DatePicker
  • RecyclerView due to scroll events consumes focus
  • Bunch of other hidden under-the-hood stuff

Few words in terms of architecture and structural approach in my solution:

  • Everything works with Data binding
  • RecyclerView works with Adapter that supports AdapterDelegate approach
  • Each EditText, Spinner, DatePicker, CheckBox or RadioButton group is a separate ViewModel which is a separate unit and handles a lot of stuff on its own.

I've used a lot of tricks and mixed them together. As someone already mentioned, the first step would be to add those params to your RecyclerView:

            android:descendantFocusability="beforeDescendants"
            android:focusable="true"
            android:focusableInTouchMode="true"

Base view model for all possible inputs has defined interface, let's call it SingleInputViewModel, among that interface you can have defined next functions/methods:

void onFocusGained();

void onFocusLost();

boolean isFocusable();

In each particular input item implementation you are able to control focus, for example you are able to implement CheckBox as non-focusable and focus will jump to next isFocusable() == true item. Also you will be able to control the state and action depending on consuming/gaining focus on particular view.

Next step for fixing some of the focus passing issues - was scrolling RecyclerView when IME_ACTION_NEXT occurs. In such case you need to delegate your scroll logic to LayoutManager.scrollHorizontallyBy() or LayoutManager.scrollToPosition() with calculating appropriate offset or position.

Hard and elegant approach is to override logic inside LayoutManager, which is also responsible for focus handling. LinearLayoutManager has a lot of hidden logic, which you won't be able to override, so probably you will need to write a lot of code from scratch.

And the last and the most complex way to fix that is to extend RecyclerView and override focus search related funs/methods:

  • RecyclerView.focusSearch()
  • RecyclerView.isPreferredNextFocus()
  • RecyclerView.onRequestFocusInDescendants()
  • RecyclerView.onFocusSearchFailed()
  • RecyclerView.onInterceptFocusSearch()
  • RecyclerView.onRequestChildFocus()

P.S. Have a look at the FocusFinder and it's usage, just for general knowledge. Now you have few option to choose. I hope you will find something helpful. Good luck!

like image 39
Yurii Tsap Avatar answered Sep 28 '22 23:09

Yurii Tsap