Search code examples
androidandroid-listviewandroid-arrayadapterrecycle

Why does the Android View API care about an ArrayAdapter's getViewTypeCount?


I've been learning about creating custom ArrayAdapters and have familiarized myself with overriding an ArrayAdapter's getViewTypeCount and getItemViewType methods.

Why does the Android View API care about the 'number of possible view types' returned by getViewTypeCount ? And, as the programmer, why should I care?


Solution

  • I've just reviewed the Android source to see exactly where getViewTypeCount is used. It's in the abstract AbsListView class' inner RecycleBin class' setViewTypeCount method.

    This setViewTypeCount method is used to specify the initial size of the ArrayList<View> of scrapViews. This ArrayList itself is an ArrayList of ArrayList<View>s -- one for each "view type count."

    Furthermore the value returned by getViewTypeCount is assigned to RecycleBin's member variable mViewTypeCount. This member is used to control the logic for recycling for cases where there is only one vs. many views throughout a handful of methods in the RecycleBin class(as everyone has been alluding to).

    So the answer is, I believe, that getViewTypeCount is used by RecycleBin so that it knows whether it has just one or numerous Views to handle. So to speak.

    (Thanks everyone for opening my eyes to recycling and motivating me to read the source.)