android中ListView的定位:使用setSelectionFromTop

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

时间: 2024-08-09 22:01:05

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

【转】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(); // ... //根据上次保存

Android中ListView分类

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android = "http://schemas.android.com/apk/res/android" android:orientation = "vertical" android:layout_width = "fill_parent" android:layo

android中使用百度定位sdk实时的计算移动距离

前段时间因为项目需求,通过百度定位adk写了一个实时更新距离的程序(类似大家坐的士时,车上的里程表),遇到很多技术点,总结了一下发表出来和大家相互学习.直接要求定位具体的位置应该是不难的,只需要引入百度定位adk,并配置相关参数就可以完成,显示百度地图也类似,但是如果需要不断的实时显示移动距离,GPS定位从一个点,到第二个点,从第二个点,到第三个点,从第三个点......,移动距离是多少呢?不得不说,要实现这种需求的确存在一定的难度. 目标:使用百度定位sdk开发实时移动距离计算功能,根据经纬度

Android中ListView字母排序,实现字母挤压效果以及右侧快速选中字母,搜索关键字功能

Android中ListView字母排序,实现字母挤压效果以及右侧快速选中字母,搜索关键字功能 本文中阐述如何自定义EditText实现搜索框自定义的样式以及挤压字母的思路等 自定义EditText 相关的drawable文件 主界面以及相关的适配器 结果展示 定义要呈现的EditText的样式 public class ClearEditText extends EditText implements OnFocusChangeListener, TextWatcher { /** * 定义删

android中listview点击事件失效的灵异事件

首先说明一下我想实现的功能: 点击某个item之后,让其颜色发生变化.如果变化网上有很多例子,我就不班门弄斧了.Listview之所以点击没有反应是因为上图中绿色部分(自己定义的一个继承BaseAdapter的adapter来适应listview)将listview的item覆盖了.现在点击的只是自定义的adapter中的convertView. 其次,自定义的adapter中包含一个ImageView和二个TextView.代码如下: <?xml version="1.0" e

Android中ListView的addFooterView不显示的问题

mListView.addFooterView(btn_more, null, false); mListView.setAdapter(mBlogListAdapter); 把addFootView放在setAdapter之前就可解决. Android中ListView的addFooterView不显示的问题

Android中ListView选中时的黄色底色

Android的ListView中默认选中时底色为黄色,如何去掉呢 其中会用到一个属性: android:listSelector="#00000000" 这样就行了 Android中ListView选中时的黄色底色

Android中ListView异步加载图片错位、重复、闪烁问题分析及解决方案

Android中ListView异步加载图片错位.重复.闪烁问题分析及解决方案 我们在使用ListView异步加载图片的时候,在快速滑动或者网络不好的情况下,会出现图片错位.重复.闪烁等问题,其实这些问题总结起来就是一个问题,我们需要对这些问题进行ListView的优化. 比如ListView上有100个Item,一屏只显示10个Item,我们知道getView()中convertView是用来复用View对象的,因为一个Item的对应一个View对象,而ImageView控件就是View对象通

Android中ListView分页加载数据

熟悉Android的朋友们都知道,不管是微博客户端还是新闻客户端,都离不开列表组件,可以说列表组件是Android数据展现方面最重要的组件,我们今天就要讲一讲列表组件ListView加载数据的相关内容.通常来说,一个应用在展现大量数据时,不会将全部的可用数据都呈现给用户,因为这不管对于服务端还是客户端来说都是不小的压力,因此,很多应用都是采用分批次加载的形式来获取用户所需的数据.比如:微博客户端可能会在用户滑动至列表底端时自动加载下一页数据,也可能在底部放置一个“加载更多”按钮,用户点击后,加载