ListView的setSelection()和setSelectionFromTop()联系
2016-06-14 11:19
495 查看
转自http://blog.csdn.net/manoel/article/details/39183025
通常,app中的数据都是以ListView的形式展示的。默认地,把“新”数据添加到数据列表的尾部。但是,如果是IM类型的app,比如查看历史消息这个模块。新数据并不是插到数据列表的尾部,而是插到数据列表的头部。要实现比较好的用户体验,需要保持当前的ListView的位置。换句话说,如果我们能够随心所欲地指定ListView滚动的位置,那么这个问题就迎刃而解。在ListView中,有一个setSelectionFromTop()方法,下面是一个使用范例。代码如下:
[java] view plain copy
@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 plain copy
/**
* 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 plain copy
/**
* 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()有了更深刻的认识了。
让ListView回到Header位置或者Footer位置,还有第一项元素位置,这个要是使用
通常,app中的数据都是以ListView的形式展示的。默认地,把“新”数据添加到数据列表的尾部。但是,如果是IM类型的app,比如查看历史消息这个模块。新数据并不是插到数据列表的尾部,而是插到数据列表的头部。要实现比较好的用户体验,需要保持当前的ListView的位置。换句话说,如果我们能够随心所欲地指定ListView滚动的位置,那么这个问题就迎刃而解。在ListView中,有一个setSelectionFromTop()方法,下面是一个使用范例。代码如下:
[java] view plain copy
@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 plain copy
/**
* 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 plain copy
/**
* 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()有了更深刻的认识了。
让ListView回到Header位置或者Footer位置,还有第一项元素位置,这个要是使用
ListView.setSelection(pos);
ListView.setSelectionFromTop(0, 0);//回到ListView的header
ListView.setSelectionFromTop(1, 0);//回到第一项元素回到footer根据具体情况而定,因为header会占一个索引值。
int postion=ListView.getAdapter().getCount();//有header的情况下 ListView.setSelectionFromTop(postion,0);
相关文章推荐
- 【1】搭建HA高可用hadoop-2.3(规划+环境准备)
- Linux下移植和配置wpa_supplicant
- Linux里设置环境变量的方法(export PATH)
- Nginx配置文件nginx.conf中文详解
- linux常用的查看设备的命令
- nginx 返回json
- apache日志分析简介
- 漫谈shell脚本
- 在Linux CentOS上编译并安装Clang 3.5.0,适用于Redhat和Clang3.8
- linux yum命令详解
- opencv类型转换
- Spring+Mybatis+Tomcat下多数据源与 atomikos 分布式事务配置
- ecshop2.6.1 Empty delimiter
- Linux各发行版本及其软件包管理方法
- 【linux】windows和linux编码相互转换
- linux编程之mprotect
- 更新CentOS 6.7源为阿里源
- 搭建memcached的docker集群
- BrnShop开源网上商城第三讲:插件的工作机制
- 基于KVM、Xen、OpenVZ等虚拟化技术的WEB在线管理工具