Positioning of ListView in android: use setSelectionFromTop

From http://www.jcodecraeer.com/a/anzhuokaifa/androidkaifa/2014/0924/1709.html

If a ListView is too long, sometimes we want the ListView to restore the last viewed position when returning from other interfaces, this It involves the positioning of ListView:

the solution is as follows:

// Save the index and offset of the current first visible item
int index = mList.getFirstVisiblePosition();
View v = mList.getChildAt(0);
int top = (v == null) ? 0 : v.getTop();
// ...
//Restore the last position according to the last saved index and offset
mList.setSelectionFromTop(index, top); used here setSelectionFromTop to position the ListView. In fact, you can also use setSelection to locate, but setSelectionFromTop is more accurate than setSelection. Because part of the first item obtained through getFirstVisiblePosition may already be invisible, if setSelection cannot reflect this invisible part.



Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=327053099&siteId=291194637