Recording the scroll position of ListView in android Development

Source: Internet
Author: User

Recording the scroll position of ListView in android Development

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 is the cZ operator? Http://www.bkjia.com/kf/ware/vc/ "target =" _ blank "class =" keylink "> vcHkgd2hvo7 + 3tNX9venJ3LXEy/keys + keys/Cw + a1xLLZ1/keys" brush: java; ">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.

 

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.