Android ListView setSelection()方法的介绍
2015-11-15 20:50
393 查看
一般来讲app中的数据都是以ListView的形式展示的。默认地,把“新”数据添加到数据列表的尾部。
但是,如果是IM类型的app,比如查看历史消息这个模块。新数据并不是插到数据列表的尾部,而是插到数据列表的头部。
要实现比较好的用户体验,需要保持当前的ListView的位置。换句话说,如果我们能够随心所欲地指定ListView滚动的位置,那么这个问题就迎刃而解。
在ListView中,有一个setSelectionFromTop()方法,下面是一个使用范例。代码如下:
[java] view
plaincopy
![](https://oscdn.geek-share.com/Uploads/Images/Content/201611/a7c8e286f463007e2a900848b93dd72c.png)
![](https://oscdn.geek-share.com/Uploads/Images/Content/201611/9e12f1d3e499fc949c886e7c9e0484f9)
@Override
public void loaded(Long loadTime, int thisPageNumber, boolean isFromZero, boolean isHasMoreToLoad, List data) {
refreshComplete();
checkIfHasMoreToLoad(isHasMoreToLoad);
if (thisPageNumber != 1) {// 不是第一页
mListView.setSelectionFromTop(5+2, mIMPullToRefreshListView.getHeaderHeight());
mIMPullToRefreshListView.getHeaderView().setVisibility(View.GONE);
}
}
看一下setSelectionFromTop()的具体实现,代码如下:
[java] view
plaincopy
![](https://oscdn.geek-share.com/Uploads/Images/Content/201611/a7c8e286f463007e2a900848b93dd72c.png)
![](https://oscdn.geek-share.com/Uploads/Images/Content/201611/9e12f1d3e499fc949c886e7c9e0484f9)
/**
* 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();
}
}
从上面的代码可以得知,setSelectionFromTop()的作用是设置ListView选中的位置,同时在Y轴设置一个偏移量(padding值)。
ListView还有一个方法叫setSelection(),传入一个index整型数值,就可以让ListView定位到指定Item的位置。
这两个方法有什么区别呢?看一下setSelection()的具体实现,代码如下:
[java] view
plaincopy
![](https://oscdn.geek-share.com/Uploads/Images/Content/201611/a7c8e286f463007e2a900848b93dd72c.png)
![](https://oscdn.geek-share.com/Uploads/Images/Content/201611/9e12f1d3e499fc949c886e7c9e0484f9)
/**
* 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);
}
原来,setSelection()内部就是调用了setSelectionFromTop(),只不过是Y轴的偏移量是0而已。
现在应该对setSelection()和setSelectionFromTop()有了更深刻的认识了。
但是,如果是IM类型的app,比如查看历史消息这个模块。新数据并不是插到数据列表的尾部,而是插到数据列表的头部。
要实现比较好的用户体验,需要保持当前的ListView的位置。换句话说,如果我们能够随心所欲地指定ListView滚动的位置,那么这个问题就迎刃而解。
在ListView中,有一个setSelectionFromTop()方法,下面是一个使用范例。代码如下:
[java] view
plaincopy
![](https://oscdn.geek-share.com/Uploads/Images/Content/201611/a7c8e286f463007e2a900848b93dd72c.png)
@Override
public void loaded(Long loadTime, int thisPageNumber, boolean isFromZero, boolean isHasMoreToLoad, List data) {
refreshComplete();
checkIfHasMoreToLoad(isHasMoreToLoad);
if (thisPageNumber != 1) {// 不是第一页
mListView.setSelectionFromTop(5+2, mIMPullToRefreshListView.getHeaderHeight());
mIMPullToRefreshListView.getHeaderView().setVisibility(View.GONE);
}
}
看一下setSelectionFromTop()的具体实现,代码如下:
[java] view
plaincopy
![](https://oscdn.geek-share.com/Uploads/Images/Content/201611/a7c8e286f463007e2a900848b93dd72c.png)
/**
* 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();
}
}
从上面的代码可以得知,setSelectionFromTop()的作用是设置ListView选中的位置,同时在Y轴设置一个偏移量(padding值)。
ListView还有一个方法叫setSelection(),传入一个index整型数值,就可以让ListView定位到指定Item的位置。
这两个方法有什么区别呢?看一下setSelection()的具体实现,代码如下:
[java] view
plaincopy
![](https://oscdn.geek-share.com/Uploads/Images/Content/201611/a7c8e286f463007e2a900848b93dd72c.png)
/**
* 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);
}
原来,setSelection()内部就是调用了setSelectionFromTop(),只不过是Y轴的偏移量是0而已。
现在应该对setSelection()和setSelectionFromTop()有了更深刻的认识了。
相关文章推荐
- Android Theme and style
- Android联机或者模拟器单独测试Activity的辅助Activity示例开发
- Android读书笔记----Service的用法
- 《第一行代码Android》学习日记1 4000 3
- Android中通过泛型解决findViewById需要强制类型转换的问题
- 初学Android项目:开发电子市场<第三天>
- Android 总结:Manifest文件中,application和activity标签属性详解
- Android Studio的使用(九)--设置IDE编码格式
- 理清contactsprovider
- 安卓开发之CheckedBox和RadioGroup
- Android Intent使用总结
- Android(4)——Property Animation属性动画
- Android驱动调试利器Busybox之初体验
- Gradlle 全解析(Android官方技术文档翻译)
- android(仿QQ向右滑动退出)在viewpager中onTouchEvent无法监听到ACTION_DOWN的getX的值
- 【Android】Scheme详解
- bsh for android : Socket Test
- 【Android开发—电商系列】(一):ListView,就这么美
- Found 4 versions of android-support-v4.jar in the dependency list 解决方案
- android studio导出lib和使用第三方lib