从列表顶部的ListView距离 [英] ListView distance from top of the list

查看:130
本文介绍了从列表顶部的ListView距离的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个ListView,我想结交有背景,与列表滚动。

I have a ListView, and I want to make a background that scrolls with the list.

我看到了code的<一个href="http://$c$c.google.com/p/shelves/source/browse/trunk/Shelves/src/org/curiouscreature/android/shelves/view/ShelvesView.java"相对=nofollow>货架,该作品,因为所有在GridView的项目都是相同的高度,这是一个保证,我不能做。我想,我可以使用类似的技术(即,覆盖dispatchDraw),但我需要知道在ListView每个项目的顶部的距离 - 我怎能pre-计算呢? ListView控件主要是静态的,相当小的,所以它的好,如果我有添加新项的列表时重新计算。

I saw the code for Shelves, which works because all of the items in the GridView are the same height, which is a guarantee that I can't make. I figure that I can use a similar technique (that is, override dispatchDraw), but I need to know the distance from the top of the ListView for each item - how can I pre-calculate this? The ListView is mostly static and fairly small, so it's okay if I have to recalculate when adding a new item to the list.

所以我的问题是这样的:给定一个ListView和一个列表适配器,我怎么能计算从列表的顶部每个项目的距离是多少?即,项0将是从顶部0像素,第20项可能是从顶部4032个像素。或者,有没有更好的办法做我想做的事?

So my question is this: Given a ListView and an List adapter, how can I calculate the distance of each item from the top of the list? That is, item 0 would be 0 pixels from the top, item 20 might be 4032 pixels from the top. Alternately, is there a better way to do what I want to do?

推荐答案

在您的适配器,保持距离的第二个列表是列表的大小相同。初始化它在构造函数。然后,对于每个getView()调用,您可以添加视图的高度来存储这个新名单previous索引处的值。因此,例如,假设你有名单,其中,整数GT;距离。当你调用 getView(...)位置= 0 ,将距离。加(0 + view.getHeight()); (一旦你的观点已经被初始化)。而对于位置1时,您会使用 distance.add(distances.get(位置-1)+ view.getHeight); 等。这时正好有一个方法获取的距离对于给定的视图索引,它返回距离存储在索引处的值

In your adapter, keep a second list of distances that is the same size of your list. Initialize it in your constructor. Then, for each getView() call, you can add the height of the view to the value stored at the previous index of this new list. So for example, say you have List<Integer> distances. When you call getView(...) with position = 0, you set distances.add(0 + view.getHeight()); (once your view has been initialized). And for position 1, you will use distance.add(distances.get(position-1) + view.getHeight); and so on. Then just have a method for getting the distance for a given view index, which returns the value stored at that index in distances.

这篇关于从列表顶部的ListView距离的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆