RecyclerView behavior - Goes empty when keyboard is opened/closed

给你一囗甜甜゛ 提交于 2021-02-18 21:53:32

问题


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?


回答1:


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.




回答2:


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)


来源:https://stackoverflow.com/questions/39069334/recyclerview-behavior-goes-empty-when-keyboard-is-opened-closed

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