问题
Why do listview.getChildCount() and adapter.getCount() return different values? I have an onScroll method and from within it, if I call the methods above, each return different values. Does anyone know why?
回答1:
ListView.getChildCount() returns the number of Views on the screen. Adapters getCount() returns the total number of objects in your list.
getChildCount()
getCount()
You might have 50 Strings in say an ArrayAdapter but only 10 are drawn on the screen at the current moment so your ListView count will be 10 but your Adapter count will be 50.
回答2:
adapter.getCount() returns total objects count contained in it. listView.getChildCount() return number of views showed in this listview.
Adapter uses caching mechanism, and a lot of views are reused, that's why it's number differs from that shown in adapter.getCount()
来源:https://stackoverflow.com/questions/19142694/listview-count-vs-adapter-count-in-android