Reputation: 7358
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);
}
Upvotes: 2
Views: 337
Reputation: 771
This is my understanding of how getView
works in an adapter:
This will return what 'type' of view the particular item in the list is - and thus use the correct convertView
in getView()
@Override
public int getItemViewType(int position) {
return 0;
}
And this returns how many different types of views there are:
@Override
public int getViewTypeCount() {
return 0;
}
Of course these don't return 0 in proper code.
Upvotes: 1