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.
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