within - Why is getCount called so many times in PagerAdapter?



updating viewpager with new data dynamically (1)

As you concluded it is used a lot in onTouchEvent. OnTouchEvent is called whenever you interact with the screen, meaning touch move and release events. Moving just one pixel would result in a potential call to this method.

There is not much more to explain, it is just the way it is implemented. Usually adapter.getCount is implemented with something like List.getSize or Cursor.getCount. And has almost zero overhead. If this is a problem, optimize you ListAdapter.getCount method, cache the count or something like that. Only do complex stuff in there when needed and cache the result until it becomes invalid.

I noticed today that the PagerAdapter gets called a large amount times. I counted 393 when scrolling pages slowly. I saw this question but it didn't really provide me with a good answer.

  1. Is this normal behaviour
  2. If so, why is it that the getCount method is called so often?

Just to be clear, I am looking for a more extensive answer then the one in the provided question. I do also realize that I need to keep it as fast and that I have no control over how it is called, but that is not the question here.