如何在ListView项更新进度 [英] How to update progressbar in a ListView item
问题描述
我有一个的ListView
连接到 ArrayAdapter
。当用户点击下载按钮在的ListView项目
下载开始使用下载管理器
。
I have a ListView
attached to an ArrayAdapter
. When the user clicks a download button for an item in the ListView
a download starts using the DownloadManager
.
我想要做的是跟踪一个进度条(放置在项目布局)下载进度。如何才能实现这一目标?
What I want to do is to track the download progress with a progress bar (placed in the item layout). How can this be achieved?
的方式袖珍演员确实是exacly我后:
The way Pocket Cast does it is exacly what I'm after:
注:我知道如何与下载管理器
,它的进度栏是棘手的即时更新工作的
Note: I know how to work with the DownloadManager
, it's the instant update of the progress bar that is tricky.
推荐答案
这是这样,我终于解开了它(多次迭代和不同的实现之后)。这是一个有点棘手,但基本上你需要三样东西:
This is the way I finally solved it (after many iterations and different implementations). It's a bit tricky but basically you need three things:
- 在它收集的元数据的AsyncTask的
- 在一个滚动监听器,它告诉我们,当用户停止滚动/扔
- 在一个聪明的算法,认为需要更新,并要求适配器仅更新任何可见行的特定行
这是我设计并实现它的方式:
This is the way I designed and implemented it:
我更详细地写了一篇关于在这里和请参阅<一href="https://github.com/slidese/SGU/blob/3be1888115ba56b4a015b127b249c35af5dc11d0/src/se/slide/sgu/ContentFragment.java">github code 的完整imlementation。
I wrote in more detail about it here, and please see the github code for the complete imlementation.
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
}
}
}
}
}
这篇关于如何在ListView项更新进度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!