SimpleCursorAdapter bindView 方法被多次调用

  • Android,java,cursor,adapter,

现象:实现了一个 SimpleCursorAdapter , 其中 bindView() 方法被多次调用,通过日志发现,被调用的位置是0。

大概的思路是,上层父容器一直在询问这个 gridview 需要多少空间,所以 gridview 一直在计算它的高度,所以会导致 bindView 方法多次被调用。


onMeasure(int widthMeasureSpec, int heightMeasureSpec)

mItemCount = mAdapter == null ? 0 : mAdapter.getCount();
final int count = mItemCount;
if (count > 0)
final View child = obtainView(0, mIsScrap);


View obtainView(int position, boolean[] isScrap)

scrapView = mRecycler.getScrapView(position);
View child;
f (scrapView != null) {
    child = mAdapter.getView(position, scrapView, this);
    child = mAdapter.getView(position, null, this);


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;   




this is out of our control. The android renders a view at least 3 times before it gets displayed.The first two have something to do with measurement. For the grid view, they assume all the views are the same size (the size of the view number 0) and then display the rest. so bind view may be called 20 or 30 times per redraw. We should make it as simple as possible


- EOF -

转载请注明:文章转载自 Binkery 技术博客 []
本文标题: SimpleCursorAdapter bindView 方法被多次调用