SimpleCursorAdapter bindView 方法被多次调用

  • Android,java,cursor,adapter,

该文章移动到:https://binkery.com/archives/2020.08.27-android-simplecursorapdapter-bingview-invoted-manytimes.html

现象:实现了一个 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;
f (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 -

本站文章除注明转载外,均为本站原创或编译。欢迎任何形式的转载,但请务必注明出处,尊重他人劳动。
转载请注明:文章转载自 Binkery 技术博客 [https://binkery.com]
本文标题: SimpleCursorAdapter bindView 方法被多次调用
本文地址: https://binkery.com/archives/210.html