0

ListView에서 Box.com으로 업로드하는 동안 여러 파일의 진행 상태를 표시하려고합니다. 나는이 링크 https://github.com/lzyzsd/CircleProgress에서 발견 한 도넛 진행 바를 사용하여 목록의 각 항목에 대한 진행 상황을 보여줍니다.listview에서 각 파일 업로드의 진행 상황을 표시하는 동안 UI가 멈추었습니다.

그러나 업로드하는 동안 UI를 업데이트 할 수 없습니다. UI가 중단되어 사용자가 클릭에 응답하지 않습니다. 각 항목의 진행률 막대는 0 %로 시작하고 업로드가 끝나면 100 %로 변경됩니다. 그 사이의 모든 비율은 UI가 멈추었 기 때문에 표시되지 않습니다.

현재 목록보기의 각 항목에 대해 사용자 지정보기를 사용하고 있습니다. 각 뷰에서 진행 리스너를 구현해야하기 때문에 그렇게했습니다. 그런 다음 각보기를 각각의 비동기 작업에 추가하고 거기에서 진행 상황을 업데이트합니다.

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

진행 리스너 인터페이스 내가

public interface BoxUploadProgressListener { 
    void onProgressUpdate(int progress); 
} 
을 만들어 : 나는 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

처럼 내 레이아웃 모습입니다. 나는 이틀 동안이 일을 해왔다.

답변

0

문제점을 발견했습니다. 유언 집행자가 작성한 while 루프. 모든 작업이 끝날 때까지 계속 실행되었습니다.

0

가능한 원인 : 모든 홀더 인스턴스와 함께 새로운 스레드를 추가 비동기 작업에서

  • 피를 UI를 변경

    1. 하지 마십시오, 그것은 응용 프로그램이 응답하지 인한되는 RAM 소비를 증가시킨다.

    메인 스레드에서 인터페이스를 만들고 UI를 업데이트하십시오.

  • +0

    그게 문제가 아닙니다. 그래도 도움을 주셔서 감사합니다. – adonayresom

    관련 문제