getItem(getCount() - getPosition() -1) 是如何工作的

How does getItem(getCount() - getPosition() -1) works

最近我遇到了一个在列表视图中反转数据的方法是:

getItem(getCount() - getPosition() - 1)

参考:Display new items at the top of a ListView

这个公式实际上是如何工作的?

例如我有一组数据,其中包含:

a, b, c , d, e 有位置 1. 2, 3 , 4, 5 分别.

因此,getCount() = 5,

使用上面的公式,位置是:

getCount() - getPosition() - 1

a = 5 - 1 -1 = 3
b = 5 - 2 - 1 = 2
c = 5 - 3 - 1 = 1
d = 5- 4 - 1 = 0
e = 5 - 5 - 1 = -1

根据这些计算,listview中的数据如何得到倒序?

使用该方法后我在listiview中得到的结果是:

e, d, c, b, a.

谢谢。

a = 5 - 0 -1 = 4
b = 5 - 1 - 1 = 3
c = 5 - 2 - 1 = 2
d = 5- 3 - 1 = 1
e = 5 - 4 - 1 = 0

项目的位置是从0开始的,不是1。所以从0开始会显示e,d,c,b,a。这就是公式的工作原理。