如果一个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()有了更深刻的认识了。

android中ListView的定位:使用setSelectionFromTop的更多相关文章

  1. 【转】android中ListView的定位:使用setSelectionFromTop实现ListView的position的保持

    如果一个ListView太长,有时我们希望ListView在从其他界面返回的时候能够恢复上次查看的位置,这就涉及到ListView的定位问题: 解决的办法如下: 1 2 3 4 5 6 7 // 保存 ...

  2. Android中Listview点击item不变颜色以及设置listselector 无效

    Android中Listview点击item不变颜色以及设置listselector 无效 这是同一个问题,Listview中点击item是会变颜色的,因为listview设置了默认的listsele ...

  3. Android中ListView控件的使用

    Android中ListView控件的使用 ListView展示数据的原理 在Android中,其实ListView就相当于web中的jsp,Adapter是适配器,它就相当于web中的Servlet ...

  4. android中ListView控件&&onItemClick事件中获取listView传递的数据

    http://blog.csdn.net/aben_2005/article/details/6592205 本文转载自:android中ListView控件&&onItemClick ...

  5. android中ListView点击和里边按钮点击不能同时生效问题解决

    今天遇到一个问题:android中ListView点击和里边button点击不能同时生效问题解决. 原因是: listView 在开始绘制的时候,系统首先调用getCount()函数,根据他的返回值得 ...

  6. Android中ListView无法点击

    Android中ListView无法点击 转自:http://xqjay19910131-yahoo-cn.iteye.com/blog/1319502   问题描述: ListView中Item加入 ...

  7. Android中 ListView 详解(二)

    本文版权归 csdn noTice501 所有,转载请详细标明原作者及出处,以示尊重! 作者:noTice501 原文:http://blog.csdn.net/notice520/article/d ...

  8. Android中ListView实现图文并列并且自定义分割线(完善仿微信APP)

    昨天的(今天凌晨)的博文<Android中Fragment和ViewPager那点事儿>中,我们通过使用Fragment和ViewPager模仿实现了微信的布局框架.今天我们来通过使用Li ...

  9. Android中ListView的几种常见的优化方法

    Android中的ListView应该算是布局中几种最常用的组件之一了,使用也十分方便,下面将介绍ListView几种比较常见的优化方法: 首先我们给出一个没有任何优化的Listview的Adapte ...

随机推荐

  1. CentOS 6.6实现永久修改DNS地址的方法

    本文实例讲述了CentOS 6.6实现永久修改DNS地址的方法. 百牛信息技术bainiu.ltd整理发布于博客园 分享给大家供大家参考,具体如下:1.配置ip地址文件 /etc/sysconfig/ ...

  2. js bind es5函数柯里化

    绑定函数 bind()最简单的用法是创建一个函数,使这个函数不论怎么调用都有同样的this值.常见的错误就像上面的例子一样,将方法从对象中拿出来,然后调用,并且希望this指向原来的对象. 如果不做特 ...

  3. 比Android更深远的改变世界——谷歌开源人工智能系统TensorFlow文档中文版

    OpenStack中国社区编者按:开源无处不在,特别在基础创新领域,未来系统软件都会是开源为主流:2015年11月9日,Google于开源了其第二代人工智能系统Tensorflow,如同6年前同样开源 ...

  4. cardboard sdk for unity 下载地址

    https://github.com/googlesamples/cardboard-unity

  5. 关于数学函数中的abs——————————————杭电2057——————————————————————————

    数学函数中的abs当你用abs之后括号之中的数字就转换成了int格式.可能会丢失一些数据造成误差而且还会有,    警告: #include<stdio.h> #include<ma ...

  6. 《windows核心编程系列》十五谈谈windows线程栈

    谈谈windows线程栈. 当系统创建线程时会为线程预订一块地址空间区域,注意仅仅是预订.默认情况下预定的这块区域的大小是1MB,虽然预订这么多,但是系统并不会给全部区域调拨物理存储器.默认情况下,仅 ...

  7. [Usaco2008 Dec]Patting Heads 轻拍牛头

    Description 今天是贝茜的生日,为了庆祝自己的生日,贝茜邀你来玩一个游戏. 贝茜让N(1≤N≤100000)头奶牛坐成一个圈.除了1号与N号奶牛外,i号奶牛与i-l号和i+l号奶牛相邻.N号 ...

  8. 51nod 1119 机器人走方格 V2

    1119 机器人走方格 V2  基准时间限制:1 秒 空间限制:131072 KB 分值: 10 难度:2级算法题  收藏  关注 M * N的方格,一个机器人从左上走到右下,只能向右或向下走.有多少 ...

  9. Stamps ans Envelope Sive UVA - 242

    ( ||{集合x}表示x中元素1||x中元素2||...||x的最后一个元素||(a,b)表示a||b) ans[i][j][k]表示考虑前i种邮票时取j个邮票能否得到面值kans[i][j][k]= ...

  10. c#如何使用replace函数将"\"替换成"\\"

    当我使用 String str="c:\aa.xls"; str=str.Replace("\","\\");时,括号为红色错误的,那么如何 ...