I implemented a Section List View following instruction from https://nodeload.github.com/necronet/section-list/zip/master.
Everything working fine, but just out of curiosity, I want to know how Android OS give me correct convertView in the getView function of the Adapter (code below). There are two types of views (two different layout files), one SectionView and ItemView. Imagine a situation, when a fist section and an item are scrolled out of the screen, so there are two views that are in the View Recycler. Then a new view is about to be scrolled in from the bottom. In the getView function, I have to check the position, by the function isSection, to determine what view should I give at that position. The amazing thing is that Android OS (or whatever underlying) always give me the correct convertView (among the two types of views in the Recycler) to recycle, how does it know before I even check? Thanks.
public View getView(final int position, final View convertView,
final ViewGroup parent) {
if (isSection(position)) {
return getSectionView(convertView, sectionPositions.get(position));
}
return getItemView(getLinkedPosition(position), convertView,
parent);
}
This is my understanding of how
getViewworks in an adapter:This will return what ‘type’ of view the particular item in the list is – and thus use the correct
convertViewingetView()And this returns how many different types of views there are:
Of course these don’t return 0 in proper code.