我想问一下为什么 CursorAdapter
将创建 View 和用数据填充它的过程拆分为 newView()
和 bindView()
而 BaseAdapter
仅通过 getView()
执行此操作?
最佳答案
来自 CursorAdapter.java 的源代码, CursorAdapter
扩展了 BaseAdapter
。
并且可以看到getView()
函数实现:
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;
}
它做我们通常在 getView()
中做的事情(如果 convertView 为 null,则膨胀 View ,否则重用 View ),所以它只是为了让开发人员更容易或强制用户使用 ViewHolder 模式。
PS:一些开发者在 newView() 实现中调用了 bindViews() 函数,从源代码中可以看出没有必要。
关于android - 为什么 CursorAdapter 与 BaseAdapter 不同?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18561744/