Android入门 在ListView中如何进行精确的定位
在android的开发中,经常会遇到需要主动去设定某条ListItem的位置的需求。设置位置的函数有
ListView.setSelection(int position)
ListView.setSelectionFromTop(int position, int y);
其中
position指的是指定的item的在ListView中的索引,注意如果有Header存在的情况下,索引是从Header就开始算的。
y指的是到ListView可见范围内最上边边缘的距离。
函数有了,现在就是根据自身需求来进行设置。
这次遇到的需求,ListView要求是从下往上展示的,并且当Cursor更新时,要保持住原先的最上方的item(不包括header)的位置不变,然后新的历史数据在原先那条item上方继续向上展示。如图:
ListView从下往上展示,也就是
- android:stackFromBottom="true"
但是发现这一属性的设置不会影响索引的排序顺序,也就是item的索引都是从上往下递增的,不会变成从下往上递增。索引为0的item,都是在ListView的最上方的item(或header).
那么当Cursor更新时,原先第一条的索引便会发生变化。要想保持住它(图中的 R)的位置。步骤如下:
(1)获取这一条在新Cursor中的位置(posiition)
(2)获取这一条在更换Cursor后ListView中的位置。
(4)由于ListView的可滚动的属性,我们需要记录更换Cursor前可视的第一条item的索引(ListView.getFirstVisiblePosition())
(3)区分FirstVisiblePosition是0和大于0的情况。由于header,也就是图中的Loading那一条在新数据出来后是会消失的。
(4)当FirstVisiblePosition为0时实际指向的是header,我们要保持位置不变的是header下面第一条(R)的位置。那么此时要设置FirstVisiblePosition为1
(5)当FirstVisiblePosition大于0时实际指向的就是item,但是我们需要设置FirstVisiblePosition为0。*
(6)我们根据FirstVisiblePosition用ListView.getChildAt(int position)函数获取对应的item的View,再根据View.getTop()函数获取到ListView顶部的距离Y。
这样ListView.setSelectionFromTop(int position, int y)所需的两个参数 position 和 y就都有了。
*注解:ListView.getChildAt(int position), 这个position指的是在可视的item中的索引,跟cursor里的位置是大不一样的。可以看看ListView.getChildCount()函数得到个数是小于或等于Cursor里的个数的(不考虑header的话)。虽然一共可能有20条数据,但是界面只能看到8条,那么这个ChildCount大约就是8了。另一方面, FirstVisiblePosition取出的是在总的条数中的索引,再将会消失的header考虑进来,所以就是 FirstVisiblePosition为0时要设为1,大于0时又要设为0。
下面上代码:
调用的代码:
- int headerCount = mListContainer.getListView().getHeaderViewsCount();
- int firstVisiblePos = mListContainer.getListView().getFirstVisiblePosition();
- int newCursorPosition = getPositionInNewCursor(cursor.getCount(), firstVisiblePos);
- int offsetY = getOffsetY(cursor, firstVisiblePos, newCursorPosition);
- mAdapter.changeCursor(cursor);
- mUpRefreshLayout.setVisibility(View.GONE);
- mListContainer.getListView().setSelectionFromTop(newCursorPosition + headerCount, offsetY);
getPositionInNewCursor函数:
- private int getPositionInNewCursor(int newCursorCount, int firstVisiblePos){
- if(firstVisiblePos == 0){
- firstVisiblePos += 1;
- }
- int headerCount = mListContainer.getListView().getHeaderViewsCount();
- int newCursorPos = newCursorCount - mAdapter.getCount() + firstVisiblePos - headerCount;
- return newCursorPos;
- }
getOffsetY函数:
- private int getOffsetY(Cursor cursor, int firstVisiblePos, int newCursorPosition){
- int y;
- View firstVisibleItem = null;
- if(firstVisiblePos == 0){
- firstVisibleItem = mListContainer.getListView().getChildAt(1);
- }else{
- firstVisibleItem = mListContainer.getListView().getChildAt(0);
- }
- y = firstVisibleItem.getTop();
- View timeView = firstVisibleItem.findViewById(R.id.time_text_view);
- if(timeView != null && timeView.getVisibility() == View.VISIBLE){
- Cursor curItem = (Cursor)mAdapter.getItem(newCursorPosition);
- Cursor preItem = (Cursor)mAdapter.getItem(newCursorPosition - 1);
- if(curItem != null || preItem != null){
- long curTimeStamp = curItem.getLong(MessagesProjection.JEDI_CREATE_DATE_INDX);
- long preTimeStamp = preItem.getLong(MessagesProjection.JEDI_CREATE_DATE_INDX);
- if(Math.abs(curTimeStamp - preTimeStamp) <= SHOW_TIME_STAMP_TEN_MINS){
- LayoutParams param = (LinearLayout.LayoutParams)mTimeView.getLayoutParams();
- y += mTimeView.getHeight() + param.topMargin + param.bottomMargin;
- }
- }
- }
- return y;
- }
getOffsetY中有一段计算图中TimeStamp的高度的代码,不关心的可以自己跳过一下。因为查询出历史数据后可能会造成原先有TimeStamp的那一条在刷新后不再显示TimeStamp(与上一条合并到一个时间段了),所以要把它的高度也计算进去。
- 顶
- 3
- 踩
Android入门 在ListView中如何进行精确的定位的更多相关文章
- Android 如何在 ListView 中更新 ProgressBar 进度
=======================ListView原理============================== Android 的 ListView 的原理打个简单的比喻就是: 演员演 ...
- Android如何在ListView中嵌套ListView
前几天因为项目的需要,要在一个ListView中放入另一个ListView,也即在一个ListView的每个ListItem中放入另外一个ListView.但刚开始的时候,会发现放入的小ListVie ...
- [Android学习笔记]ListView中含有Button导致无法响应onItemClick回调的解决办法
转自:http://www.cnblogs.com/eyu8874521/archive/2012/10/17/2727882.html 问题描述: 当ListView的Item中的控件只是一些展示类 ...
- Android入门:MVC模式(中)
MVC 模式的最基本概念是分层设计,把我们的代码基于 View(视图).Model(模型).Controller(控制器)进行分类封装,这样做的目的是为了清晰结构,使代码更易维护和扩展. 在上一篇文章 ...
- android开发之 listview中的item去掉分割线 隐藏分割线
有三种方法: 1> 设置android:divider="@null" 2> android:divider="#00000000" #000000 ...
- Android单选中listview中的一项
public class LipsListAdapter extends BaseAdapter { private Context context; private List<Lips> ...
- ListView 中的ImageView Button
1.ListView 中的ImageView Button的点击事件会屏蔽掉ItemView的点击事件 原因是ImageView Button在回执过程中会抢夺item的焦点 解决办法:在xml中添 ...
- Android入门(五)UI-单位与尺寸、ListView
原文链接:http://www.orlion.ga/453/ 一.单位与尺寸 布局文件中一共有以下单位供选择:px,pt,dp,sp px:是像素,屏幕中可见的最小元素单位. pt:是磅,1磅等于1/ ...
- Android,LIstView中的OnItemClick点击无效的解决办法
在List_Item布局文件中的根节点加上如下背景标黄的这一行 <?xml version="1.0" encoding="utf-8"?> < ...
随机推荐
- CentOS7.0使用Yum安装Nginx
安装Nginx yum install nginx 正常情况下必定是: 已加载插件:fastestmirror, langpacks base | 3.6 kB 00:00:00 docker-mai ...
- JavaScript 开发者的 10 款必备工具
JavaScript,一种所有主流浏览器都支持的语言,是开发基于浏览器的 Web 应用程序的主力,几乎每年都会受到来自众多开发人员的关注.自然地,框架和库的生态系统自然而然地围绕着 JavaScrip ...
- J.U.C并发框架源码阅读(四)CountDownLatch
基于版本jdk1.7.0_80 java.util.concurrent.CountDownLatch 代码如下 /* * ORACLE PROPRIETARY/CONFIDENTIAL. Use i ...
- Codeforces Round #428 A. Arya and Bran【模拟】
A. Arya and Bran time limit per test 1 second memory limit per test 256 megabytes input standard inp ...
- #422 Div2 D
#422 Div2 D 题意 假设有 n 个人比赛,每次比赛进行分组,每组人数必须相同,如果一组有 x 人,则那一组要比赛 $ \frac{x * (x - 1)}{2}$次,最终一人获胜,其它人淘汰 ...
- jqgrid postData setGridParam 调用多次时查询条件累加的问题--详情页查询导致的无法在新的页面中查询
$("#btn_search").click(function () { url = "/AMEvents/GetGridJson?evtype=1"; var ...
- Nand 的几个名词:oob,bbt,ecc
转:http://blog.csdn.net/lanmanck/article/details/4230904 例如Samsung K9F1208U0B,数据存储容量为64MB,采用块页式存储管理.8 ...
- 【微信】微信小程序 微信开发工具中新创建的json文件,编译报错VM1781:2 pages/module/module.json 文件解析错误 SyntaxError: Unexpected end of JSON input
如果新创建报错:编译报错VM1781:2 pages/module/module.json 文件解析错误 SyntaxError: Unexpected end of JSON input 解决方法 ...
- Ubuntu16.04 -- 后台进程Nohup
nohup用于使程序在用户退出登陆.关闭终端之后仍能继续运行 用法: nohup your_command & #(符号&使程序在后台运行) exit #(退出nohup模式) 启动后 ...
- vue axios跨域请求,apache服务器设置
问题所在axios请求会发送两次请求 也就是说,它会先使用options去测试,你这个接口是否能够正常通讯,如果不能就不会发送真正的请求过来,如果测试通讯正常,则开始正常请求. 思路: 跨域--> ...