Android development records the scroll position of ListView, androidlistview

Source: Internet
Author: User

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.