Could you explain the getView()
method of an ArrayAdapter
.
I read the docs and it has three parameters:
position
: The position of the item within the adapter's data set of the item whose view we want.convertView
: The old view to reuse, if possible. Note: You should check that this view is non-null and of an appropriate type before using. If it is not possible to convert this view to display the correct data, this method can create a new view.
Heterogeneous lists can specify their number of view types, so that this View is always of the right type (see getViewTypeCount() and getItemViewType(int)).parent
: The parent that this view will eventually be attached to
I understood the position
parameter. Like they said, it's imply the position of the item, who view has been requested.
Where does convertView
come from. I've seen many examples where they check if convertView
is null. If is is null, they inflate a new instance of the row layout, populate it and return it. I guess I've gotten my head around that too but one thing still baffles me. What is the layout that is passed in through the convertView
parameter. Is if the resource
parameter that is passed in when initialising the ArrayAdapter
? Is is a cached copy of the last layout returned by getView()
?
And lastly. What does the parent
parameter do. I haven't seen too many examples utilising this. Most of them simply reuse/inflate a row layout and return it.
(I'm asking because I have an on-click animation in my ListView
. Specifically this one that aims to replicate Spotify's dropdown quick action menu. My animation has been a little sluggish. After diagnosing this issue for a while, I've realized that this has been due to the fact that my getView()
method takes a bit of time to complete as I'm inflating a fresh row layout in every iteration. Someone suggested caching the row layout in a ViewHolder
while other examples point to reusing the convertView
parameter i.e. only inflating a row layout if convertView
is null.)