Get listview item position after soft keyboard shown

青春壹個敷衍的年華 提交于 2019-12-25 04:52:28

问题


I have a ListView, it is under action bar and above an EditText. See picture:

The list view items have random height according to its content.

When click list view item, I need to focus on EditText and open soft keyboard, meanwhile I want the one being clicked to be at top of the list view, for example if I click item2, I want to see:

For putting the clicked item to the top of listview, when clicking an item, I will get the clicked item View by its position, and call view.getLocationOnScreen(locations) to get its y location, and use this y minus actionbar's height, then I get the distance to scroll, at last call listview.smoothScrollBy(distance, 100);.

This approach works well after the soft keyboard being shown, as the list view's height will be fixed then; but it doesn't work well the first time I click a list view item, because at this point, the listview's height will be changed as the soft keyboard takes up some spaces, which causes when I use view.getLocationOnScreen(locations), it returns the old position of the view, old means the position before the soft keyboard shown, I also tried to use view.postDelayed(new Runnable() {//Gets view position here!}, 1000);, but got the same old position.

So the question should be how to get the latest position after the keyboard shown?

I use android:transcriptMode="normal" for ListView and adjustResize for windowSoftInputMode.

EDIT:

Also tried int firstVisiblePos = commentsListView.getFirstVisiblePosition(); int offset = firstVisiblePos - clickedPosition; commentsListView.smoothScrollByOffset(offset);, but still no helps.


回答1:


You dont have to use the screen height to scroll the listView. You could use ListView's smoothScrollToPosition(int position) to scroll to a particular location in the list.




回答2:


commentsListView.setSelection(clickedPosition + 1);, got it, simply this works for me, although it has no animation (not smooth), it is good enough for me, for making the smooth scroll to position, use this answer [link]smoothScrollToPositionFromTop() is not always working like it should



来源:https://stackoverflow.com/questions/30802348/get-listview-item-position-after-soft-keyboard-shown

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