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