When this kind of logic processing involves object destruction and reconstruction, attention should be paid to decoupling. The object references should not be matched, but the data values should be matched. Do not place network processing directly in Activity or RecylerView. For downloading, it is best to start a background service to manage your download thread, and then let the service communicate with the Activity. In this way, you don’t have to care about whether your object can correspond. You only need to care about the download progress reported by the service to you. value. As for the interaction between Service and Activity, there are too many tutorials and wheels on the Internet, so I won’t go into details.
Whether it comes with the systemDownloadManager还是自定义的下载组件, 一个文件下载都会有一个下载ID, 或者对应一个Uri. 值得一提的是, 系统自动的DownloadManagerthe download information is maintained through the database.
With this premise, it is still not enough. We need to find a way to get the download progress of each file. This is related to the specific implementation of DownloadManager. Taking the system as an example, it seems that it only provides the Query interface, so we need to create a Background thread to query download progress in real time.
Finally, we need to consider how to manage the correspondence between ID or Uri and RecyclerView的ItemView.
The above 1 & 2 can be implemented by referring to some mainstream image asynchronous loading frameworks (such as github: nostra13/Android-Universal-Image-Loader).
p.s. If you think it’s too complicated, you can refer to github: erehmi/CountDownTask. (I shamelessly recommend my own project [face covering])
p.p.s. It is not recommended to directly notifyDataChanged() to refresh the entire list, the performance is too low.
When this kind of logic processing involves object destruction and reconstruction, attention should be paid to decoupling. The object references should not be matched, but the data values should be matched. Do not place network processing directly in Activity or RecylerView. For downloading, it is best to start a background service to manage your download thread, and then let the service communicate with the Activity. In this way, you don’t have to care about whether your object can correspond. You only need to care about the download progress reported by the service to you. value. As for the interaction between Service and Activity, there are too many tutorials and wheels on the Internet, so I won’t go into details.
With this premise, it is still not enough. We need to find a way to get the download progress of each file. This is related to the specific implementation of
DownloadManager
. Taking the system as an example, it seems that it only provides the Query interface, so we need to create a Background thread to query download progress in real time.Finally, we need to consider how to manage the correspondence between ID or Uri and
RecyclerView
的ItemView
.The above 1 & 2 can be implemented by referring to some mainstream image asynchronous loading frameworks (such as github: nostra13/Android-Universal-Image-Loader).
p.s. If you think it’s too complicated, you can refer to github: erehmi/CountDownTask. (I shamelessly recommend my own project [face covering])
p.p.s. It is not recommended to directly notifyDataChanged() to refresh the entire list, the performance is too low.
It is best to use service for this kind of downloading tasks