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:
The solution is as follows:
1234567 |
// 保存当前第一个可见的item的索引和偏移量 int index = mList.getFirstVisiblePosition(); View v = mList.getChildAt(0); int top = (v == null ) ? 0 : v.getTop(); // ... //根据上次保存的index和偏移量恢复上次的位置 mList.setSelectionFromTop(index, top); |
The setselectionfromtop is used here to locate the ListView. In fact, can also use setselection can also locate, just setselectionfromtop than setselection more accurate. Because the getFirstVisiblePosition
first item that is obtained may already be partially invisible, if setselection cannot reflect this invisible part.
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:
12345678910111213141516171819202122232425262728293031 |
/**
* 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 the
* 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();
}
}
|
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:
1234567891011 |
/**
* 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);
}
|
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 ().
Original link: http://www.jcodecraeer.com/a/anzhuokaifa/androidkaifa/2014/0924/1709.html
"Go" targeting of the ListView in Android: Using Setselectionfromtop to achieve position retention of the ListView