ListView的setSelectionFromTop()方法与setSelection()方法的联系
2016-03-18 20:02
375 查看
通常,app中的数据都是以ListView的形式展示的。默认地,把“新”数据添加到数据列表的尾部。
但是,如果是IM类型的app,比如查看历史消息这个模块。新数据并不是插到数据列表的尾部,而是插到数据列表的头部。
要实现比较好的用户体验,需要保持当前的ListView的位置。换句话说,如果我们能够随心所欲地指定ListView滚动的位置,那么这个问题就迎刃而解。
在ListView中,有一个setSelectionFromTop()方法,下面是一个使用范例。代码如下:
看一下setSelectionFromTop()的具体实现,代码如下:
从上面的代码可以得知,setSelectionFromTop()的作用是设置ListView选中的位置,同时在Y轴设置一个偏移量(padding值)。
ListView还有一个方法叫setSelection(),传入一个index整型数值,就可以让ListView定位到指定Item的位置。
这两个方法有什么区别呢?看一下setSelection()的具体实现,代码如下:
原来,setSelection()内部就是调用了setSelectionFromTop(),只不过是Y轴的偏移量是0而已。现在应该对setSelection()和setSelectionFromTop()有了更深刻的认识了。
但是,如果是IM类型的app,比如查看历史消息这个模块。新数据并不是插到数据列表的尾部,而是插到数据列表的头部。
要实现比较好的用户体验,需要保持当前的ListView的位置。换句话说,如果我们能够随心所欲地指定ListView滚动的位置,那么这个问题就迎刃而解。
在ListView中,有一个setSelectionFromTop()方法,下面是一个使用范例。代码如下:
@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()的具体实现,代码如下:
/** * 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()的具体实现,代码如下:
/** * 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()有了更深刻的认识了。
相关文章推荐
- Linux--进程间通信(管道及有名管道FIFO)(转)
- linux 从一台机器复制文件到另一台linux机器上去
- CentOS7.2共享文件夹_安装配置SAMBA服务器
- dpkg查看软件包和共享库信息
- linux基础指令(三)scp
- 免费的论文查重网站
- hadoop 2.6 伪分布式的安装
- linux 排序 文本统计
- 在VMware Workstation 12 PRO上安装CentOS 7 Minimal版
- 《Linux内核分析》第四周学习总结 扒开系统调用的三成皮(上)
- Openfire实现QQ群功能
- docker入门
- 关于成为架构师的几个思考?
- 解决HP ProLiant DL380 G5的Centos 7安装与启动不能识别硬盘问题
- linux下查找文件包含关键字
- linux 常用的基本命令
- 【WEB】Tomcat基础使用知识
- shell--linux系统脚本
- nginx做域名转发和uri转发
- PVS高可用的第二种方法——BDM启动