Android Development record ListView scroll position

Source: Internet
Author: User

The problem itself is not difficult, but because the requirements in the project are too complex, the result is that the problem becomes less easily realized. On the Internet a search, the results are as follows:

I don't know who's copy who? Anyway introduced by the so-called three methods, the first is not able to run, very obvious bug, the 23rd method is the entry-level knowledge, these big Brother blogging themselves do not first run a bit?

Alas, do not spit groove, talk about my solution:

Perform the following actions where you want to record the scroll position:

int scrollPos = refreshListView.getFirstVisiblePosition();                    View v1 = refreshListView.getChildAt(0);                    intnull0 : v1.getTop();

The Getfirstvisibleposition method is used to record the index of the currently first visible item.
int scrolltop = (V1 = = null)? 0:v1.gettop (); The offset that is used to record the current first visible item.

Attention

In your project, you may need to View v1 = refreshListView.getChildAt(0); add a non-null judgment to determine if the data set that is appropriate to the ListView is empty.

Perform the following actions where you need to restore the scroll position:

.setSelectionFromTop(scrollPos, scrollTop);

Some people may choose to record the current position of the first visible item, and then use the SetSelection method to recover the location, so it is not impossible, but not accurate enough, because in many cases the top item in our ListView is not necessarily just shown, May show half of the hidden half. So personally think the method described above is more appropriate.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced. If there is a wrong place, I would appreciate it if I could criticize it.

Android Development record ListView scroll position

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.