Android ListView Locator

Source: Internet
Author: User

If a ListView is too long, sometimes we want the ListView to revert to the last view when it returns from another interface, which involves the location of the ListView:
Smoothscrolltoposition need more than 2.2, smoothscrollbyoffset need more than 3.0. Smoothscrolltoposition allows for smooth scrolling
The solution is as follows:
Saves the index and offset of the currently first visible item
int index = mlist.getfirstvisibleposition ();
View v = mlist.getchildat (0);
int top = (v = = null)? 0:v.gettop ();
// ...
Note: listview.getchildat (int position), this position refers to the index in the visible item, which is very different from the position in the cursor.
You can see that the Listview.getchildcount () function Gets the number that is less than or equal to the number in the cursor (regardless of the header).
Although there may be 20 data, but the interface can only see 8, then the ChildCount is about 8.
On the other hand, Firstvisibleposition takes out the index in the total number of bars and then considers the missing header, so the firstvisibleposition is 0 to set to 1, which is greater than 0 o'clock and set to 0.
Restores the last position based on the last saved index and offset
Mlist.setselectionfromtop (index, top);
To illustrate the significance of the Setselectionfromtop parameter values, and the difference from the SetSelection, the following is analyzed from the source:

Take a look at the specific implementation of Setselectionfromtop (), the code is as follows:
/**
* Sets the selected item and positions the selection y pixels from the top edge
* of the ListView. (If in touch mode, the item is not being selected but it'll
* Still be positioned appropriately.)
*
* @param position Index (starting at 0) of the that data item to be selected.
* @param y the distance from the top edge of the The ListView (plus padding)
* Item would be positioned.
*/
public void setselectionfromtop (int position, int y) {
if (Madapter = = null) {
Return
}
if (!isintouchmode ()) {
Position = Lookforselectableposition (position, true);
if (position >= 0) {
Setnextselectedpositionint (position);
}
} else {
Mresurrecttoposition = position;
}
if (position >= 0) {
Mlayoutmode = layout_specific;
Mspecifictop = Mlistpadding.top + y;
if (Mneedsync) {
Msyncposition = position;
Msyncrowid = Madapter.getitemid (position);
}
Requestlayout ();
}
}

As you can tell from the code above, the function of Setselectionfromtop () is to set the position of the ListView selected and set an offset on the Y axis.
The SetSelection () method, passing in an index integer value, allows the ListView to navigate to the location of the specified item.
What is the difference between these two methods? Take a look at the specific implementation of setselection (), the code is as follows:

/**
* Sets the currently selected item. If in touch mode, the item won't be selected
* But it'll still be positioned appropriately. If the specified selection position
* is less than 0 and then the item at position 0 would be selected.
*
* @param position Index (starting at 0) of the that data item to be selected.
*/
@Override
public void setselection (int position) {
Setselectionfromtop (position, 0);
}
Originally, the setselection () is called the Setselectionfromtop (), but the y-axis offset is 0. It is time to have a deeper understanding of setselection () and Setselectionfromtop ().

In fact, can also use setselection can also locate, just setselectionfromtop than setselection more accurate.
Because the first item obtained through getfirstvisibleposition may already be partially invisible, if setselection cannot reflect this invisible part.


Then when the cursor is updated, the first index of the original is changed. Want to keep it in place. The steps are as follows:

(1) Get the position of this piece in the new cursor (posiition)

(2) Gets the position of this bar in the ListView after replacing the cursor.

(4) Due to the scrollable properties of the ListView, we need to record the index of the first item that is visible before the cursor is replaced (listview.getfirstvisibleposition ())

(3) The case of distinguishing firstvisibleposition is 0 and greater than 0. The header, which is the loading in the figure, disappears after the new data comes out.

(4) When firstvisibleposition is 0 o'clock the actual point is the header, we want to keep the position unchanged is the position of the first (R) under the header. Then set Firstvisibleposition to 1 at this time

(5) When Firstvisibleposition is more than 0 o'clock, the actual point is item, but we need to set Firstvisibleposition to 0. *

(6) We obtain the corresponding item view according to Firstvisibleposition with the listview.getchildat (int position) function, and then according to the View.gettop () The function gets the distance to the top of the ListView Y.

So the two parameters required for listview.setselectionfromtop (int position, int y) position and y are all available.

Android ListView Locator

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.