Android ListView positioning, androidlistview

Source: Internet
Author: User

Android ListView positioning, androidlistview

If a ListView is too long, sometimes we want the ListView to restore the last viewed position returned from other interfaces, which involves the positioning problem of the ListView:
SmoothScrollToPosition requires more than 2.2, and smoothScrollByOffset requires more than 3.0. SmoothScrollToPosition for smooth scrolling
The solution is as follows:
// Save the index and offset of the first visible item
Int index = mList. getFirstVisiblePosition ();
View v = mList. getChildAt (0 );
Int top = (v = null )? 0: v. getTop ();
//...
Annotation: ListView. getChildAt (int position), which refers to the index in the visible item, which is different from the position in the cursor.
Let's take a look at the ListView. getChildCount () function to get a number that is less than or equal to the number in the Cursor (if header is not considered ).
Although there may be a total of 20 data records, but the interface can only see 8, then this ChildCount is about 8.
On the other hand, FirstVisiblePosition takes out the index in the total number of items and then considers the missing header. Therefore, when FirstVisiblePosition is 0, it must be set to 1, and when it is greater than 0, it must be set to 0.
// Restore the last location based on the last saved index and offset
MList. setSelectionFromTop (index, top );
To illustrate the significance of the setSelectionFromTop parameter value and the difference with setSelection, the following is an analysis from the source code:

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 will not be selected but it will
* Still be positioned appropriately .)
*
* @ Param position Index (starting at 0) of the data item to be selected.
* @ Param y The distance from the top edge of the ListView (plus padding) that
* Item will 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 ();
}
}
 

From the code above, we can know that setSelectionFromTop () is used to set the position selected by ListView and an offset on the Y axis.
In the setSelection () method, an index integer value is input, so that the ListView can locate the specified Item.
What are the differences between the 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 will not be selected
* But it will still be positioned appropriately. If the specified selection position
* Is less than 0, then the item at position 0 will be selected.
*
* @ Param position Index (starting at 0) of the data item to be selected.
*/
@ Override
Public void setSelection (int position ){
SetSelectionFromTop (position, 0 );
}
It turns out that setSelection () calls setSelectionFromTop () internally, except that the offset of the Y axis is 0. Now we should have a deeper understanding of setSelection () and setSelectionFromTop.

In fact, you can also use setSelection or location, but setSelectionFromTop is more accurate than setSelection.
Because part of the first item obtained through getFirstVisiblePosition may be invisible, and this is invisible if setSelection is used.


When the Cursor is updated, the first index will change. To maintain its position. The procedure is as follows:

(1) obtain the position of this entry in the new Cursor (posiition)

(2) obtain the position of this entry in the ListView after changing the Cursor.

(4) because of the scrolling attribute of ListView, we need to record the index of the first item visible before Cursor (ListView. getFirstVisiblePosition ())

(3) differentiate whether FirstVisiblePosition is 0 or greater than 0. Because the header, that is, the Loading in the figure, disappears after the new data is generated.

(4) When FirstVisiblePosition is 0, it actually points to the header. What we want to keep unchanged is the position of the first (R) below the header. Set FirstVisiblePosition to 1.

(5) When FirstVisiblePosition is greater than 0, it actually points to the item, but we need to set FirstVisiblePosition to 0. *

(6) we use the ListView. getChildAt (int position) function to obtain the View of the corresponding item based on FirstVisiblePosition, and then obtain the distance Y from the top of the ListView Based on The View. getTop () function.

In this way, both the position and y parameters required by ListView. setSelectionFromTop (int position, int y) are available.

 

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.