SoFunction
Updated on 2025-04-10

How to implement Android listview to locate the last displayed position

How to implement Android listview to locate the last displayed position

Overall idea: When sliding the lictview, record the position of the listview and position it to that position when positioning.

1. Add global variables

private int scrolledX = 0;
private int scrolledY = 0;

2. Oncreate() adds scroll event listening

(new OnScrollListener() {

/**
 * Called when the scroll state changes
 */

@Override
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {

}

@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
if (scrollState == OnScrollListener.SCROLL_STATE_IDLE) {
try {

scrolledX = ();
("scroll X", (scrolledX));
scrolledY = (0).getTop();
("scroll Y", (scrolledY));
} catch (Exception e) {
}
}
}

//

});

3. Activity  When restarting, restore the original positioning.

public void onBack() {
(scrolledX, scrolledY);
}

protected void onResume() {
// (scrolledX, scrolledY);
onBack();
// initDataFromNet();
isClick = 0;
();
}

If you have any questions, please leave a message or go to the community of this site to exchange and discuss. Thank you for reading. I hope it can help you. Thank you for your support for this site!