防止RecyclerView回收特定视图

时间:2018-04-20 12:39:32

标签: android android-recyclerview

我目前正在开发一款从互联网上下载视频的应用。 我正在使用RecyclerView显示视频列表,我在每个项目中使用ProgressBar。问题是,当下载开始时,ProgressBar会显示下载进度,但当我尝试滚动甚至触摸屏幕时,进度会回落到0。

我的问题是:有没有办法阻止它?

我正在使用ThinDownloadManager下载视频。

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);
                }

            });

适用于YouTube播放列表的适配器:

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);
    }

解答: 这种行为的原因是因为某些视图覆盖了我的RecyclerView,所以我通过移开视图来修复它。

0 个答案:

没有答案