GridView.getCount()(实际上是 AdapterView.getCount()) 返回的是其 Adapter.getCount() 返回的值。也就是“所包含的 Item 总个数”。
GridView.getChildCount()(ViewGroup.getChildCount) 返回的是显示层面上的“所包含的子 View 个数”。
二者有什么不同?当 GridView中的 Item 比较少无需滚动即可全部显示时,二者是等价的;当 Item 个数较多需要滚动才能浏览全部的话, getChildCount() < getCount() 其中 getChildCount() 返回的是当前可见的 Item 个数。
其实 Android framework 的这一设计并不难理解:当一些 Item 当前不显示的时候为什么还要保留它们的 View 呢?移动设备的资源有限,“能省则省”嘛。
本文详细解释了GridView中getCount()与getChildCount()的区别及应用场景。getCount()返回GridView的总项数,而getChildCount()返回当前可视项的数量。通过对比这两种方法,文章揭示了Android框架如何高效管理视图资源。
594

被折叠的 条评论
为什么被折叠?



