簡訊小程式,關於CursorAdapter檢視複用問題
阿新 • • 發佈:2018-11-08
問題:我要現實的item個數為13個,然後CursorAdaper卻只給我new了6次
從而導致顯示結果如下:
百思不得其解,查看了原始碼,只有在View為空的時候才會new,不為空的時候則複用,故我的解決方法是,重寫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(),讓其每一次都new一個View
@Override public View getView(int position, View convertView, ViewGroup parent) { getCursor().moveToPosition(position); View view = newView(mContext, getCursor(), parent); bindView(view,mContext,getCursor()); return嗯哼,記錄一下這個問題view; }