ListView的setSelection()和setSelectionFromTop()联系
来源:互联网 发布:linux安装intel驱动 编辑:程序博客网 时间:2024/06/06 13:22
通常,app中的数据都是以ListView的形式展示的。默认地,把“新”数据添加到数据列表的尾部。
但是,如果是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()有了更深刻的认识了。
参考资料
http://developer.android.com/reference/android/widget/ListView.html#setSelection%28int%29
http://www.cnblogs.com/over140/archive/2013/05/20/2948239.html
http://blog.csdn.net/jdsjlzx/article/details/17794209
http://blog.csdn.net/a859522265/article/details/8154103
- ListView的setSelection()和setSelectionFromTop()联系
- ListView的setSelection()和setSelectionFromTop()联系
- ListView的setSelection()和setSelectionFromTop()联系
- 【移动开发】ListView的setSelection()和setSelectionFromTop()联系
- 【移动开发】ListView的setSelection()和setSelectionFromTop()联系
- Android——ListView的setSelection()和setSelectionFromTop()联系
- 【移动开发】ListView的setSelection()和setSelectionFromTop()联系
- 商品搜索跳转显示引出的setSelection()和setSelectionFromTop()的区别
- ListView的setSelection用法
- ListView的setSelection()方法
- android中ListView的定位:使用setSelectionFromTop
- android中ListView的定位:使用setSelectionFromTop
- android中ListView的定位:使用setSelectionFromTop
- listview.setSelection ()无效的问题
- ListView的setSelection()不起作用的原因
- ListView的setSelection()方法的作用
- 为什么ListView的setSelection无效了?
- ListView的setItemChecked()或者setSelection()无效是怎么回事?
- OpenGL--双缓冲
- 面试问答—软实时和硬实时操作系统的区别
- Android二维码功能实现,在程序内嵌入ZXing项目
- Python 使用nltk对序列元素(eg. 列表)的频谱计算小结
- 豪情-CSS解构系列之-新浪页面解构-01
- ListView的setSelection()和setSelectionFromTop()联系
- The superclass "javax.servlet.http.HttpServlet" was not found on the Java Build Path
- android application
- Django Web项目部署参考
- 在Eclipse中修改自动生成的注释里的作者信息
- 典型应用,企业网络vlan规划方案!
- Servlet路径跳转
- 微信公众号开发将war包导入新浪sae出现错误
- android 离线语言识别