Issue
I am currently working on app where you can download video files. I am using Download manager for downloading files and everything is working fine as the files are being downloaded properly and I can see the progress of downloading in my notifications. Now I want to show those downloading files in another class (containing recyclerview) with the progress. So what should i do to implement this module?
String urlDownload = mUrl;
String filename = mUrl.substring(mUrl.lastIndexOf('/') + 1);
Log.i("onLoadResource()", "url = " + mUrl + "\n" + filename);
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(urlDownload));
request.setDescription("Testando");
request.setTitle("Download");
request.allowScanningByMediaScanner();
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, filename);
final DownloadManager manager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
final long downloadId = manager.enqueue(request);
final ProgressBar mProgressBar = (ProgressBar) findViewById(R.id.progress_bar);
new Thread(new Runnable() {
@Override
public void run() {
boolean downloading = true;
while (downloading) {
DownloadManager.Query q = new DownloadManager.Query();
q.setFilterById(downloadId);
Cursor cursor = manager.query(q);
cursor.moveToFirst();
int bytes_downloaded = cursor.getInt(cursor
.getColumnIndex(DownloadManager.COLUMN_BYTES_DOWNLOADED_SO_FAR));
int bytes_total = cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_TOTAL_SIZE_BYTES));
if (cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_STATUS)) == DownloadManager.STATUS_SUCCESSFUL) {
downloading = false;
}
final int dl_progress = (int) ((double)bytes_downloaded / (double)bytes_total * 100f);
runOnUiThread(new Runnable() {
@Override
public void run() {
mProgressBar.setProgress((int) dl_progress);
}
});
cursor.close();
}
}
}).start();
}
Solution
Maintain a variable for progress in the data model you are using for each RecyclerViewItem. When you receive an update on progress, update the dataset you are using and after that call notifyItemChanged(mPos)
or notifyItemRangeChanged()
to notify the adapter of the same. notifyItemChanged(mPos)
triggers onBindVieHolder
for corresponding position even if it's currently not visible.
Answered By - Ayush Pahwa
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.