Android settings ListView currently displayed item

Source: Internet
Author: User

This may be required in the project: dynamically set the item displayed by the ListView

This kind of demand may appear in different situations, some of the open page will be displayed in a specific location, there is a browse list when the real-time update data and change the data in the collection, or a certain condition triggered and change the location of the display, I encountered is the second.

Let's talk about my situation first:

Displaying the data in a ListView increases the amount of data in the collection when the pull-up or drop-down refreshes, but considering that if the item is too much of a resource, I control the length of the collection, and I remove a portion of the data from the collection after each refresh if the size exceeds the maximum value. But when I refresh the adapter, the problem arises, and the item that the ListView displays jumps directly to the top or bottom instead of the position shown before the refresh, for reasons I don't have a specific study, Perhaps because adapter remembers position every time the refresh of adapter is called, the refresh will continue to show from the position that you remember, but I removed a portion of it, causing the display to go awry.

In fact, the solution is very simple, many people on the Internet have encountered this problem and gave a solution:

If you remove part of the data from the collection when you pull up or pull down, you can resolve the problem by calling the following method before refreshing

Listview.postdelayed (New Runnable () {
@Override
public void Run () {

Pull, setselection (gets the position of the first item that is currently displayed minus the number of data that you removed from the collection (a)) so that the previous position is displayed when the refresh is resumed
Listview.setselection (Listview.getfirstvisibleposition ()
-A + 1);
}

Drop-down, setselection (number of data you removed from the collection (a))

Listview.setselection (a);
}, 20);

Android settings ListView currently displayed item

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.