Android development records the scroll position of ListView, androidlistview
This problem is not difficult, but it is not so easy to implement because the project requirements are too complicated. Search on the Internet and the results are as follows:
I don't know. who copy who? The first three methods described in this article cannot be run, and there are obvious bugs. The second three methods are basic-level knowledge, don't these big brother write a blog and run it first?
Alas, let's talk about my solution:
Perform the following operations to record the scroll position:
int scrollPos = refreshListView.getFirstVisiblePosition(); View v1 = refreshListView.getChildAt(0); int scrollTop = (v1 == null) ? 0 : v1.getTop();
The getFirstVisiblePosition method is used to record the index of the first visible item.
Int scrollTop = (v1 = null )? 0: v1.getTop (); used to record the offset of the first visible item.
Note:
In your project, you may needView v1 = refreshListView.getChildAt(0);
Add a non-empty judgment to determine whether the dataset adapted to ListView is empty.
To restore the scroll position, perform the following operations:
refreshListView .setSelectionFromTop(scrollPos, scrollTop);
Some may choose to record the current position of the first visible item, and then use the setSelection method to restore the position. This is not impossible, but not accurate enough, in many cases, the top items in the ListView are not necessarily just displayed, but may be half hidden. Therefore, I personally think the method described above is more appropriate.
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger. I am very grateful if you have any mistakes.