1

I'm currently working on an app that downloads videos from the internet. I'm using RecyclerView to show a list of videos and I'm using a ProgressBar in each item. The problem is, when a download starts, the ProgressBar shows the download progress, but when I try to scroll or even touch the screen the progress goes back down to 0.

My question is: Is there a way to prevent it?

I'm using ThinDownloadManager to download the videos.

Downloader.class

private DownloadRequest downloadRequest(Context context,
                                         String destinationFolder,  ArrayList<String> information) {
    return new DownloadRequest
            .setRetryPolicy(new DefaultRetryPolicy())
            .setDestinationURI(Uri.parse(context.getExternalCacheDir() + destinationFolder))
            .setDownloadResumable(true)
            .setPriority(DownloadRequest.Priority.HIGH)
            .setStatusListener(new DownloadStatusListenerV1() {

                @Override
                public void onDownloadComplete(DownloadRequest downloadRequest) {
                    addToFavorites(context, information, destinationFolder);

                @Override
                public void onDownloadFailed(DownloadRequest downloadRequest, int errorCode, String errorMessage) {

                }

                @Override
                public void onProgress(DownloadRequest downloadRequest, long totalBytes, long downloadedBytes, int progress) {
                    mProgressBar.setProgress(progress);
                }

            });

An Adapter for YouTube Playlist:

Adapter.class

@NonNull
@Override
public VideoHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    View view = mLayoutInflater.inflate(mResource, parent, false);
    return new VideoHolder(view, mClickListener, mDownloadListener);
}

@Override
public void onBindViewHolder(@NonNull VideoHolder holder, int position) {
    RelatedVideosResponse.RelatedItems relatedItems = mPlayListResults.get(position);
    RelatedVideosResponse.RelatedSnippet relatedSnippet = relatedItems.getRelatedSnippet();

    Glide.with(mContext).load(relatedSnippet.getRelatedThumbNails().getHigh().getUrl()).into(holder.image);
    holder.title.setText(relatedSnippet.getTitle());


}

ViewHolder

    private LinearLayout itemBackground;
    private CircleImageView image;
    private TextView title;
    private ImageButton more;
    private NumberProgressBar downloadProgress;

    private OnRelatedVideoClickListener mPlayVideoClickListener;
    private MoreClickListener mMoreListener;

    VideoHolder(View itemView, OnRelatedVideoClickListener clickListener, MoreClickListener moreListener) {
        super(itemView);

        itemBackground = itemView.findViewById(R.id.ll_related_videos);
        image = itemView.findViewById(R.id.civ_related_video);
        title = itemView.findViewById(R.id.tv_related_video_title);
        more = itemView.findViewById(R.id.btn_download_related_video);
        downloadProgress = itemView.findViewById(R.id.pb_download);

        mPlayVideoClickListener = clickListener;
        mMoreListener = moreListener;

        itemView.setOnClickListener(this);
        more.setOnClickListener(this);
    }

ANSWER: The reason for that behavior is because some view covered my RecyclerView a little bit, so I fixed it by moving the view away.

Yogev Uzan
  • 81
  • 1
  • 3
  • the view is being recycled. You may want to take a look at this https://stackoverflow.com/questions/32065267/recyclerview-changing-items-during-scroll/38158122 – Zun Apr 20 '18 at 12:44
  • Honestly I did not know about this solution either. I just googeled it and this was my first search result. lol – Zun Apr 20 '18 at 12:52
  • By your explanation I do not think these view are actually recycled. Recycling is only done on views that are not currently visible. For the debugging purposes you can override `onViewRecycled(holder)` and catch when it is triggered. – Gotiasits Apr 20 '18 at 12:59
  • @ZUNJAE I just checked it once again, sometimes it works, sometimes it doesn't... – Yogev Uzan Apr 20 '18 at 13:05
  • @Gotiasits Yep, you're right... the method only gets triggered when I scroll... – Yogev Uzan Apr 20 '18 at 13:09
  • Ok, than in that case its probably problem with your background threads. Your most likely using AsyncTask, whereas some library or custom build background thread class should be more suitable. In any case, you should post some code... – Gotiasits Apr 20 '18 at 13:22
  • I added some code... – Yogev Uzan Apr 20 '18 at 13:37
  • Sorry, to little info... where is `mProgressBar` ? Where do you call `downloadRequest()` ? I suppose you'd have to find some way for `onProgress` to request ItemLayoutChange from RecyclerView. – Gotiasits Apr 20 '18 at 16:04
  • I found the problem, it was the XML file. Apparently there was a view that covered the top of the RecyclerView a little bit and caused this weird behavior.. – Yogev Uzan Apr 20 '18 at 17:41

0 Answers0