如果一个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

最新文章

  1. 掌握SortSet接口和Set接口的关系,以及常用方法。
  2. Mybatis3.x与Spring4.x整合(转)
  3. web前端开发工程师,你了解吗?
  4. dll的编写和使用
  5. 一致性hash介绍
  6. SQL Server 2005 中的同义词
  7. 故障模块名称: mso.dll
  8. Freezable 对象(WPF)
  9. HDU 5266 pog loves szh III (LCA)
  10. 数学概念——J - 数论,质因数分解
  11. unity4.x for mac破解(含Unity全版本号破解)
  12. 手把手教你在Windows端搭建Redmine项目管理软件
  13. HDU 2952 Counting Sheep(DFS)
  14. React的组件用法
  15. setInterval()与setTimeout()的区别
  16. django的templatetags
  17. GitHub 设置首页显示 404 There isn't a GitHub Pages site here.
  18. 自动化测试-15.selenium单选框与复选框状态判断
  19. PHP 闭包获取外部变量和global关键字声明变量的区别
  20. 33. Search in Rotated Sorted Array & 81. Search in Rotated Sorted Array II

热门文章

  1. LeetCode 笔记系列13 Jump Game II [去掉不必要的计算]
  2. FPGA与simulink联合实时环路系列——实验一 测试
  3. bootstrap-markdown编辑器引入
  4. 为什么print在python3中变成了函数?
  5. 在canvas中使用html元素
  6. base64和图片的转换
  7. TYVJ P1080 N皇后
  8. 移动端设置-----rem
  9. Photon服务器进阶&一个新游戏的出产(二)
  10. Django学习笔记(现学现写,实时更新)