【转】android中ListView的定位:使用setSelectionFromTop实现ListView的position的保持
如果一个ListView太长,有时我们希望ListView在从其他界面返回的时候能够恢复上次查看的位置,这就涉及到ListView的定位问题:
解决的办法如下:
1
2
3
4
5
6
7
|
// 保存当前第一个可见的item的索引和偏移量 int index = mList.getFirstVisiblePosition(); View v = mList.getChildAt(0); int top = (v == null ) ? 0 : v.getTop(); // ... //根据上次保存的index和偏移量恢复上次的位置 mList.setSelectionFromTop(index, top); |
这里使用了setSelectionFromTop来定位ListView。其实还可以使用setSelection也可以定位,只是setSelectionFromTop要比setSelection更精准。因为通过getFirstVisiblePosition
得到的第一个item可能已经有一部分是不可见的了,如果用setSelection无法反映出这不可见的部分。
为了说明setSelectionFromTop的参数值的意义,以及与setSelection的区别,下面从源码上来分析:
看一下setSelectionFromTop()的具体实现,代码如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
|
/** * 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轴设置一个偏移量。
而setSelection()方法,传入一个index整型数值,就可以让ListView定位到指定Item的位置。
这两个方法有什么区别呢?看一下setSelection()的具体实现,代码如下:
1
2
3
4
5
6
7
8
9
10
11
|
/** * 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://www.jcodecraeer.com/a/anzhuokaifa/androidkaifa/2014/0924/1709.html
【转】android中ListView的定位:使用setSelectionFromTop实现ListView的position的保持的更多相关文章
- android中使用百度定位sdk实时的计算移动距离
; //5秒刷新一次 private Handler refreshHandler = new Handler(){ //刷新界面的Handler public void handleMessag ...
- android中百度地图定位的实现方法(仅适用于真机+WIFI联网环境)
注意:此代码的环境是:真机(系统版本为Android4.2.2)+WIFI联网(才能实现最后的运行结果):使用虚拟机调试会出现各种问题. 第一步:下载SDK和申请Key 到百度的网站http://de ...
- android中与Adapter相关的控件----ListView
ListView讲解: 一.ListView这个控件是一个使用非常广泛的控件,值得深入的学习和研究.基本使用已经在Adapter中使用过了 二.常用的属性和方法 footerDividersEnabl ...
- Android中几种定位 方式
介绍的几种定位方式 http://www.cnblogs.com/cuihongyu3503319/p/3863867.html 百度地图api: http://lbsyun.baidu.com/in ...
- Android中 ListView 详解(二)
本文版权归 csdn noTice501 所有,转载请详细标明原作者及出处,以示尊重! 作者:noTice501 原文:http://blog.csdn.net/notice520/article/d ...
- Android中使用ListView实现自适应表格
GridView比ListView更容易实现自适应的表格,但是GridView每个格单元的大小固定,而ListView实现的表格可以自定义每个格单元的大小,但因此实现自适应表格也会复杂些(格单元大小不 ...
- Android中ListView实现图文并列并且自定义分割线(完善仿微信APP)
昨天的(今天凌晨)的博文<Android中Fragment和ViewPager那点事儿>中,我们通过使用Fragment和ViewPager模仿实现了微信的布局框架.今天我们来通过使用Li ...
- Android中ListView的几种常见的优化方法
Android中的ListView应该算是布局中几种最常用的组件之一了,使用也十分方便,下面将介绍ListView几种比较常见的优化方法: 首先我们给出一个没有任何优化的Listview的Adapte ...
- Android中Listview点击item不变颜色以及设置listselector 无效
Android中Listview点击item不变颜色以及设置listselector 无效 这是同一个问题,Listview中点击item是会变颜色的,因为listview设置了默认的listsele ...
- Android中ListView控件的使用
Android中ListView控件的使用 ListView展示数据的原理 在Android中,其实ListView就相当于web中的jsp,Adapter是适配器,它就相当于web中的Servlet ...
随机推荐
- JQUERY获取当前页面的URL信息
以前在做网站的时候,经常会遇到当前页的分类高亮显示,以便让用户了解当前处于哪个页面.之前一直是在每个不 同页面写方法.工程量大,也不便于修改.一直在想有什么简便的方法实现.后来在网上查到可以用获取当前 ...
- IoC 与 AOP (谈谈你对 Spring 的理解)
一.Spring 实现了工厂模式的工厂类,这个类名为BeanFactory(实际上是一个接口),在程序中通常 BeanFactory 的子类 ApplicationContext. Spring相当于 ...
- 【SQL】sql server 2008R2 评估期已过,
参考1:http://www.cnblogs.com 参考2:http://www.wang1314.com 个人认为:升级+秘钥,,买正版才是最终的解决方法.
- 小程序实现sql插入语句转换成Laravel迁移语句
sql的插入语句长这样: INSERT INTO `media` (`MediaID`, `type`, `filename`, `title`) VALUES (1, 'word', 'word1. ...
- Ubuntu16.04编译Android6.0/cm13.0教程及相关错误解决办法
一.必备工作 1.安装依赖库 sudo apt--dev libesd0-dev git-core gnupg flex bison gperf build-essential zip curl zl ...
- iis虚拟目录名称“ReportServer”的巧合
今天测试一个Crystal Report网站的报表服务,建立一个虚拟目录,名为ReportServer,结果无论怎样访问浏览器都返回 localhost/ReportServer - / Micr ...
- CSS命名规范
DIV+CSS规范命名大全集合 前端人员必看CSS命名规范 整理: 文件名必须由小写字母.数字.中划线组成 ).所有的命名最好都小写,一律采用小写加中划线的方式,不允许使用大写字母或 _2).属性的值 ...
- wpf converter converterparameter 绑定多参数
1. converterparameter不是依赖属性,所以不能用binding. 2. 可以把converter 的接口 IValueConverter改为 IMultiValueConverter ...
- CIDR详解和ip最长地址前缀匹配
1.CIDR是什么 无类域间路由(CIDR)编址方案 摒弃传统的基于类的地址分配方式,允许使用任意长度的地址前缀,有效提高地址空间的利用率. 就是一个ip加一个网络掩码,不过这个掩码不是之前只有3个值 ...
- 动手实验iptables的NAT功能实现流量穿透
1.NAT和iptables理论见: http://lustlost.blog.51cto.com/2600869/943110 2.引子 近期,有同事抱怨说数据入库时,由于数据库所在的服务器只有内网 ...