RecyclerView behavior - Goes empty when keyboard is opened/closed
Asked Answered
D

2

10

I have implemented a RecyclerView with SearchView and Filterable; all classes from v7. Now there is this behavior that is annoying. Whenever the keyboard is brought up or closed the contents of the RecyclerView goes blank. The count is still correct but the view is empty. My guess, it has something to do with the Layout size change. Is this behavior normal or something is wrong? How to deal with it? I can show the code but don't quite know which part will be relevant so tell me what can I add here?

Daveen answered 21/8, 2016 at 22:16 Comment(0)
D
20

While typing in the question, found this from the similar questions.

Please add following line to your activity in manifest. Hope it works. android:windowSoftInputMode="adjustPan"

More precisely, add android:windowSoftInputMode="adjustPan" in the activity tag in AndroidMenifest.xml where the keyboard is going to be opened.

Example:

<activity
        android:name=".FManagerActivity"
        android:label="@string/app_name"
        android:windowSoftInputMode="adjustPan"
        android:theme="@style/AppTheme.Light.NoActionBar">
    <intent-filter>
        <action android:name="android.intent.action.MAIN"/>
        <category android:name="android.intent.category.LAUNCHER"/>
    </intent-filter>
</activity>

It is basically the behavior in which the activity reacts when the keyboard is opened or closed. adjustPan tells the keyboard to overlay the view of the activity not disturbing it's content. Without that when the keyboard is opened the size of the activity also changes which makes the content disappear as notifyDatasetChanged() is not called during and after the implicit actions.

Daveen answered 21/8, 2016 at 22:16 Comment(0)
C
0

Not really sure why but setting it as SOFT_INPUT_ADJUST_RESIZE solved the issue for me:

activity?.window?.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE)
Colloid answered 30/9, 2018 at 6:54 Comment(1)
Setting SOFT_INPUT_ADJUST_RESIZE will resize the activity which will lead to recreation of activity (unless handling manually using android:configChanges="screenSize"). You must save activity state and retain in onCreate.Daveen

© 2022 - 2024 — McMap. All rights reserved.