Why is getCount called so many times in PagerAdapter?

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.

  • ImageView in circular through xml
  • Exception when I run my application from Eclipse
  • Is there a way to generate ant file local.properties without overwriting build.xml?
  • Efficient loop through Java List
  • Gap between left drawable and text in a EditText
  • android get device ID for adMob
  • log4j support in Android
  • How to open Settings of Android Phone on a button click in our Android App
  • Video compression on android using new MediaCodec Library
  • Android: Accessing the microphone simultaniously (RecognizerIntent + own app)
  • Android Sleep/Standby Mode
  • Android:How to add a button in surface view
  • One Solution collect form web for “Why is getCount called so many times in PagerAdapter?”

    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.

    Android Babe is a Google Android Fan, All about Android Phones, Android Wear, Android Dev and Android Games Apps and so on.