SimpleCursorAdapter bindView 方法被多次调用

March 21, 2013

现象:实现了一个SimpleCursorAdapter , 其中bindView()方法被多次调用,通过日志发现,被调用的位置是0。
大概的思路是,上层父容器一直在询问这个gridview需要多少空间,所以gridview一直在计算它的高度,所以会导致bindView方法多次被调用。

Gridview 

onMeasure(int widthMeasureSpec, int heightMeasureSpec)

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

AbsListView

View obtainView(int position, boolean[] isScrap)

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

CursorAdapter

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;
    }

到现在为止,没有找到办法。应该是需要从上层的父容器入手。或者另外一个解决办法是自定义一个gridview之类的。


====================

对于这个问题,我的同事是这样答复我的。
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 ---

添加新评论