ListView with EditText Keyboard Issues

2019-07-21 20:09发布

In my app, I have an Activity, which contains a Fragment, which contains a ListView with various EditTexts, like so:

Activity -> Fragment -> ListView -> EditTexts

When an EditText is tapped, the soft keyboard is displayed and one of two things happens:

  • If the Activity has not yet lost focus (via visiting the Android home screen, for example), the entire activity slides up slightly so that the EditText is not obscured by the keyboard.

  • If the Activity has lost focus at some point (again, such as visiting the Android home screen), the ListView scrolls the EditText above the keyboard (the activity remains stationary), BUT the EditText loses focus!

Neither of these results is ideal. Ideally, I always want the ListView to scroll the EditText above the keyboard without the EditText losing focus. Can anybody point me in the right direction and/or explain why I am seeing two different behaviors for the same action right now?

Edit: For reference, I'm building for Android 4.2 and testing on Nexus 7 tablet.

2条回答
该账号已被封号
2楼-- · 2019-07-21 20:31

The problem is that when you use EditText in a ListView row, your ListView might be redrawn to make space for the keyboard. And when your ListView is redrawn, the system does not remember where the cursor was before.

The same issue does not seem to happen if you use a LinearLayout inside a ScrollView instead of a ListView. It sounds stupid, but it works.

查看更多
Evening l夕情丶
3楼-- · 2019-07-21 20:36

Recently I have encountered this problem either, and i sloved this problem by adding android:windowSoftInputMode="adjustPan" in AndroidManifest.xml.This will not cause the listview to redraw, so the edittext will not lose its focus.

查看更多
登录 后发表回答