recyclerview中的Progressbars重叠

时间:2016-12-02 05:43:59

标签: android android-recyclerview

在我的android项目中,我正在使用RecyclerView。在运行时我需要向它添加文件,以便它将这些文件上传到我的服务器。并且每个项目上都会有一个ProgressBar来显示上传进度。我的问题是每当我想要一次上传更多文件时,一个项目的进度显示在另一个项目上。这只发生在当前正在运行的上传中,因为我隐藏了完成的ProgressBar。每当我上下滚动时,它也会发生。我试图在hashMap中存储上传进度,并在项目出现时检索它,但它不起作用。我知道这是RecyclerView与listview相比的自然行为。我想知道如何实现这一目标。我用s3来上传文件。如果文件是图像,则滑动显示文件的缩略图。

CODE:

  @Override
    public void onBindChildViewHolder(final MyChildViewHolder holder, final int groupPosition, final int childPosition, int viewType) {
        final MyChildItem child = Items.get(groupPosition).children.get(childPosition);
        Context ctx = holder.imageView.getContext();

        final String status = Smap.get(child.fileInfo.unique_id);
        Log.d("STATUS", status);
        switch (status) {
            case Constants.UNKNOWN:
            case Constants.RESUMED_WAITING:
            case Constants.PAUSED:
            case Constants.FAILED:
            case Constants.CANCELED:
                holder.upload_cancel.setVisibility(View.VISIBLE);
                holder.resume_pause_select.setVisibility(View.VISIBLE);
                holder.resume_pause_select.setImageResource(R.drawable.ic_action_ic_refresh_white_24dp);
                holder.fileprogressbar.setVisibility(View.INVISIBLE);
                holder.file_name.setVisibility(View.INVISIBLE);
                break;
            case Constants.PENDING_CANCEL:
            case Constants.PENDING_NETWORK_DISCONNECT:
            case Constants.PART_COMPLETED:
            case Constants.PENDING_PAUSE:
            case Constants.WAITING_FOR_NETWORK:
            case Constants.IN_PROGRESS:
            case Constants.WAITING:
                holder.upload_cancel.setVisibility(View.VISIBLE);
                holder.fileprogressbar.setIndeterminate(true);
                holder.resume_pause_select.setVisibility(View.VISIBLE);
                holder.resume_pause_select.setImageResource(R.drawable.ic_action_ic_pause_white_24dp);
                holder.fileprogressbar.setVisibility(View.VISIBLE);
                holder.file_name.setVisibility(View.INVISIBLE);
                try {
                    file file = new file(null, child.fileInfo.unique_id, child.fileInfo.localpath, child.fileInfo.remotepath, status, child.fileInfo.privacy, child.fileInfo.file_size, "UPLOAD", Umap.get(child.fileInfo.unique_id), child.fileInfo.pond_id, Constants.getLastTimeOpened());
                    fileDao.insert(file);
                    Log.d("new file", "Inserted");
                } catch (SQLException e) {
                    Log.d("ERROR", "Not Inserted");
                }
                Integer prevprogress = Pmap.get(child.fileInfo.unique_id);
                if(prevprogress!=null){
                    holder.fileprogressbar.setIndeterminate(false);
                    holder.fileprogressbar.setProgress(prevprogress);
                }
                transferObserver = Util.getTransferUtility(getContext()).getTransferById(Integer.parseInt(Umap.get(child.fileInfo.unique_id)));
                if (transferObserver != null) {
                    transferObserver.setTransferListener(new TransferListener() {
                        @Override
                        public void onStateChanged(int i, TransferState transferState) {
                            if (child.fileInfo.unique_id != null && !child.fileInfo.unique_id.isEmpty()) {
                                Umap.put(child.fileInfo.unique_id, String.valueOf(i));
                                Smap.put(child.fileInfo.unique_id, transferState.toString());
                            }
                            holder.fileprogressbar.setIndeterminate(true);

                            if (transferState.toString().equals(Constants.COMPLETED)) {
                                com.ficean.android.ficean.File.File.fileStore(getContext(), phonenumber, "UPLOAD", child.fileInfo, new com.ficean.android.ficean.File.File.Call() {
                                    @Override
                                    public void onSuccess(boolean result) {
                                        if (result) {
                                        }
                                    }
                                });
                            }
                            List<file> files = fileDao.queryBuilder().where(com.ficean.android.ficean.db.fileDao.Properties.File_unique_id.eq(child.fileInfo.unique_id)).limit(1).build().list();
                            for (file file : files) {
                                file.setFile_status(transferState.toString());
                                file.setU_id(String.valueOf(i));
                                fileDao.update(file);
                            }
                            expMgr.notifyChildItemChanged(groupPosition,childPosition);
                        }

                        @Override
                        public void onProgressChanged(int i, long l, long l1) {
                            int value = Constants.percentage(l, l1);
                            holder.fileprogressbar.setIndeterminate(false);
                            holder.fileprogressbar.setProgress(value);
                            Pmap.put(child.fileInfo.unique_id,value);
                        }

                        @Override
                        public void onError(int i, Exception e) {

                        }
                    });
                }
                break;
            default:
                String link = SaveUtil.getEncryptedPreferences(getContext()).getString(child.fileInfo.unique_id, Constants.NOTHING);
                Glide.with(getContext()).load(link).skipMemoryCache(true).diskCacheStrategy(DiskCacheStrategy.SOURCE).crossFade().placeholder(R.drawable.image_icon).listener(new CustomRequestListener(new CustomRequestListener.requestCallBack() {
                    @Override
                    public void onSuccess(boolean isSuccess) {
                        if (!isSuccess) {
                            String remotePath;
                            if (Opener.isFileVideo(child.fileInfo.localpath)) {
                                remotePath = phonenumber.concat("/").concat("thumbnails").concat("/").concat(FilenameUtils.removeExtension(new File(child.fileInfo.localpath).getName())).concat(".png");
                            } else {
                                remotePath = child.fileInfo.remotepath;
                            }
                            LoginCredentials.getSignedUrl(getContext(), remotePath, new LoginCredentials.UrlCallback() {
                                @Override
                                public void onSuccess(final URL url) {
                                    if (url != null) {
                                        Glide.with(getContext()).load(url.toString()).skipMemoryCache(true).diskCacheStrategy(DiskCacheStrategy.SOURCE).crossFade().placeholder(R.drawable.image_icon).listener(new CustomRequestListener(new CustomRequestListener.requestCallBack() {
                                            @Override
                                            public void onSuccess(boolean isSuccess) {
                                                if (isSuccess) {
                                                    SaveUtil.getEncryptedEditor(getContext()).putString(child.fileInfo.unique_id, url.toString()).apply();
                                                }
                                            }
                                        })).error(R.drawable.image_icon).into(holder.imageView);
                                    }
                                }
                            });
                        }
                    }
                })).error(R.drawable.image_icon).into(holder.imageView);
                holder.resume_pause_select.setVisibility(View.INVISIBLE);
                holder.fileprogressbar.setVisibility(View.INVISIBLE);
                holder.upload_cancel.setVisibility(View.INVISIBLE);
                break;
        }
    }

0 个答案:

没有答案