问题
I would like to ask why CursorAdapter
splits the process of creating a view and populating it with data into newView()
and bindView()
while BaseAdapter
only does this with getView()
?
回答1:
From Source code of CursorAdapter.java, CursorAdapter
extends BaseAdapter
.
And you can see getView()
function implementation:
public View getView(int position, View convertView, ViewGroup parent) {
if (!mDataValid) {
throw new IllegalStateException("this should only be called when the cursor is valid");
}
if (!mCursor.moveToPosition(position)) {
throw new IllegalStateException("couldn't move cursor to position " + position);
}
View v;
if (convertView == null) {
v = newView(mContext, mCursor, parent);
} else {
v = convertView;
}
bindView(v, mContext, mCursor);
return v;
}
Its do what we usually do in getView()
(inflate the view if convertView is null, otherwise reuse the view), so its just for make it easier for the developer OR force the user to use ViewHolder pattern.
PS: Some devs calls bindViews() function in there newView() implementation, from the source code you can see there is no need for that.
回答2:
If you check CurosrAdapter source code you can see, that in getView
method, both newView
and bindView
methods are used. Method newView
is executed only when there is no view, thus it can spare the creation of some objects. Method bindView
is always called and it purpouse is to update view data.
来源:https://stackoverflow.com/questions/18561744/why-is-cursoradapter-different-from-baseadapter