2017-10-25 10 views
0

複数のファイルの進行状況をListViewのBox.comにアップロード中に表示しようとしています。私はこのリンクhttps://github.com/lzyzsd/CircleProgressから見つけたドーナツプログレスバーを使って、リストの各項目の進捗状況を表示しています。リストビューで各ファイルのアップロードの進行状況を表示しようとしたときにUIが詰まった

しかし、私はアップロード中にUIを更新できないようです。 UIが停止し、ユーザーのクリックに反応しなくなります。各項目の進行状況バーは0%で始まり、アップロードが完了するたびに100%に変更されます。その間にあるすべてのパーセンテージは、UIが停止してから表示されません。

私は現在、リストビューの各項目にカスタム表示を使用しています。私は、それぞれのビューに進捗リスナーを実装する必要があったので、これを行いました。次に、各ビューをそれぞれの非同期タスクに追加し、そこから進捗状況を更新します。

次は私のコードです:私は

public interface BoxUploadProgressListener { 
    void onProgressUpdate(int progress); 
} 
を作成

public class CustomSurveyView extends RelativeLayout implements BoxUploadProgressListener{ 
View rootView; 
LinearLayout linearLayout; 
TextView fileIDTextView; 
TextView dateModifiedTextView; 
DonutProgress donutProgress; 

public CustomSurveyView(Context context) { 
    super(context); 
    init(context); 
} 

public CustomSurveyView(Context context, AttributeSet attrs) { 
    super(context, attrs); 
    init(context); 
} 

private void init(Context context) { 
    rootView = inflate(context, R.layout.custom_survey_view, this); 
    linearLayout = rootView.findViewById(R.id.fileInfoLayout); 
    fileIDTextView = rootView.findViewById(R.id.fileIDTextView); 
    dateModifiedTextView = rootView.findViewById(R.id.dateModifiedTextView); 
    donutProgress = rootView.findViewById(R.id.fileUploadProgressBar); 
} 

@Override 
public void onProgressUpdate(int progress) { 
    donutProgress.setVisibility(View.VISIBLE); 
    ObjectAnimator anim = ObjectAnimator.ofInt(donutProgress, "progress", donutProgress.getProgress(), progress); 
    donutProgress.setProgress(progress); 
    anim.setInterpolator(new DecelerateInterpolator()); 
    anim.setDuration(500); 
    anim.start(); 
    invalidate(); 
} 

public LinearLayout getLinearLayout() { 
    return linearLayout; 
} 

public TextView getFileIDTextView() { 
    return fileIDTextView; 
} 

public TextView getDateModifiedTextView() { 
    return dateModifiedTextView; 
} 

public DonutProgress getDonutProgress() { 
    return donutProgress; 
} 
} 

進捗リスナーインタフェース:私はListViewコントロール

public class SurveyListAdapter extends BaseAdapter { 

private LayoutInflater inflater; 
private LinkedList<File> files; 
private static LinkedList<String> savedIDs; 
private ViewHolder holder; 
private BoxApiFile mFileApi; 
private HomeActivity homeActivity; 
private int [] progress; 
private int mScrollState = AbsListView.OnScrollListener.SCROLL_STATE_IDLE; 
private ListView listView; 
private File file; 

public class ViewHolder { 
    TextView fileIDTextView; 
    TextView dateModifiedTextView; 
    DonutProgress fileUploadProgress; 
    CustomSurveyView customSurveyView; 
    UploadToBoxTask uploadTask; 
} 

public SurveyListAdapter(HomeActivity homeActivity, LinkedList<File> files, ListView listView) { 
    this.homeActivity = homeActivity; 
    inflater = LayoutInflater.from(homeActivity); 
    this.files = files; 
    savedIDs = new LinkedList<>(); 
    progress = new int [files.size()]; 
    this.listView = listView; 
} 

public int getCount() { 
    return files.size(); 
} 

public File getItem(int position) { 
    return files.get(position); 
} 

public long getItemId(int position) { 
    return position; 
} 

public View getView(int position, View convertView, ViewGroup parent) { 
    file = files.get(position); 
    holder = null; 
    if(convertView == null) { 
     holder = new ViewHolder(); 
     convertView = inflater.inflate(R.layout.survey_list_view, null); 
     holder.customSurveyView = (CustomSurveyView) convertView.findViewById(R.id.customSurveyView); 
     holder.fileUploadProgress = holder.customSurveyView.getDonutProgress(); 
     holder.fileIDTextView = holder.customSurveyView.getFileIDTextView(); 
     holder.dateModifiedTextView = holder.customSurveyView.getDateModifiedTextView(); 
     holder.uploadTask = new UploadToBoxTask(this, holder.customSurveyView, file); 
     convertView.setTag(holder); 
    } else { 
     holder = (ViewHolder) convertView.getTag(); 
    } 
    String fileName = file.getName(); 
    String fileShouldStartWith = HomeActivity.FILE_SHOULD_START_WITH; 
    String fileShouldEndWith = HomeActivity.FILE_SHOULD_END_WITH; 

    String ID = fileName.substring(fileShouldStartWith.length(), fileName.length() - fileShouldEndWith.length()).trim(); 
    savedIDs.add(ID); 

    SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy hh:mm a"); 
    String lastDateModified = sdf.format(file.lastModified()); 

    holder.fileIDTextView.setText("Subject ID " + ID); 
    holder.dateModifiedTextView.setText(lastDateModified); 

    return convertView; 
} 

public View getViewByPosition(int pos) { 
    final int firstListItemPosition = listView.getFirstVisiblePosition(); 
    final int lastListItemPosition = firstListItemPosition + listView.getChildCount() - 1; 

    if (pos < firstListItemPosition || pos > lastListItemPosition) { 
     return listView.getAdapter().getView(pos, null, listView); 
    } else { 
     final int childIndex = pos - firstListItemPosition; 
     return listView.getChildAt(childIndex); 
    } 
} 

public static LinkedList<String> getSavedIDs() { 
    return savedIDs; 
} 

public void syncWithBox(BoxApiFile mFileApi){ 
    this.mFileApi = mFileApi; 

    ExecutorService executor = Executors.newSingleThreadExecutor(); 
    for (int i = 0; i < files.size(); i++) { 
     ViewHolder holder = (ViewHolder) getViewByPosition(i).getTag(); 
     holder.fileUploadProgress.setVisibility(View.VISIBLE); 
     holder.uploadTask.executeOnExecutor(executor);} 

    executor.shutdown(); 

    while (!executor.isTerminated()) { } 

    System.out.println("Finished uploading"); 
} 

private class UploadToBoxTask extends AsyncTask<Void, Integer, Void>{ 

    SurveyListAdapter surveyListAdapter; 
    BoxUploadProgressListener progressListener; 
    File uploadFile; 

    public UploadToBoxTask(SurveyListAdapter surveyListAdapter, BoxUploadProgressListener progressListener, File uploadFile){ 
     this.surveyListAdapter = surveyListAdapter; 
     this.progressListener = progressListener; 
     this.uploadFile = uploadFile; 
    } 

    @Override 
    protected Void doInBackground(Void... params) { 
     try { 
      final BoxRequestsFile.UploadFile request = mFileApi.getUploadRequest(uploadFile, BoxConstants.ROOT_FOLDER_ID); 
      request.setProgressListener(new ProgressListener() { 
       @Override 
       public void onProgressChanged(long numBytes, long totalBytes) { 
        publishProgress((int) (100 * (numBytes/totalBytes))); 
       } 
      }); 
      final BoxFile uploadFileInfo = request.send(); 
      showToast("Uploaded " + uploadFileInfo.getName()); 
      //loadRootFolder(); 
     } catch (BoxException e) { 
      BoxError error = e.getAsBoxError(); 
      if (error != null && error.getStatus() == HttpURLConnection.HTTP_CONFLICT) { 
       ArrayList<BoxEntity> conflicts = error.getContextInfo().getConflicts(); 
       if (conflicts != null && conflicts.size() == 1 && conflicts.get(0) instanceof BoxFile) { 
        //uploadNewVersion((BoxFile) conflicts.get(0), position, adapter); 
        publishProgress(100); 
        return null; 
       } 
      } 
      showToast("Upload failed"); 
      e.printStackTrace(); 
     } 
     return null; 
    } 

    @Override 
    protected void onProgressUpdate(Integer... integers){ 
     progressListener.onProgressUpdate(integers[0]); 
     showToast(String.valueOf(integers[0])); 
    } 

    @Override 
    protected void onPostExecute(Void param){ 

    } 
} 
} 

のために私が作成したカスタムビューを使用しています

アダプタ

これは、各アップロードの進行状況とUIを更新し、正しく助けてください

app layout

ように私のレイアウトが見えるものです。私は今、この2日間行ってきました。

答えて

0

問題が見つかりました。私はexecutorの後に置いたwhileループです。すべてのタスクが終了するまで実行を続けました。

0

考えられる原因:すべてのホルダーインスタンスと一緒に新しいスレッドを追加する非同期タスクから

  • 回避するUIを変更

    1. 避け、それはアプリが応答しないため、先のRAMの消費量が増加します。

    メインスレッドでUIを作成し、UIを更新します。

  • +0

    これは問題ではありません。しかし、助けてくれてありがとう。 – adonayresom

    関連する問題