这就是我最终解决它的方式(经过多次迭代和不同的实现)。这有点棘手,但基本上你需要三件事:
- 收集元数据的 AsyncTask
- 一个滚动监听器,告诉我们用户何时停止滚动/滚动
- 一种巧妙的算法,可以找到任何需要更新的可见行并要求适配器仅更新该特定行
这是我设计和实现它的方式:

我在这里写了更详细的内容,完整的实现请参见github 代码。
private class UpdaterAsyncTask extends AsyncTask<Void, Void, Void> {
boolean isRunning = true;
public void stop() {
isRunning = false;
}
@Override
protected Void doInBackground(Void... params) {
while (isRunning) {
// Gather data about your adapter objects
// If an object has changed, mark it as dirty
publishProgress();
try {
Thread.sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
return null;
}
@Override
protected void onProgressUpdate(Void... params) {
super.onProgressUpdate();
// Update only when we're not scrolling, and only for visible views
if (mScrollState == OnScrollListener.SCROLL_STATE_IDLE) {
int start = mListview.getFirstVisiblePosition();
for(int i = start, j = mListview.getLastVisiblePosition(); i<=j; i++) {
View view = mListview.getChildAt(i-start);
if (((Content)mListview.getItemAtPosition(i)).dirty) {
mListview.getAdapter().getView(i, view, mListview); // Tell the adapter to update this view
}
}
}
}
}