From d56b38e06829c40afae2422300017a1788401411 Mon Sep 17 00:00:00 2001 From: liyujiang Date: Wed, 16 Mar 2022 15:35:36 +0800 Subject: [PATCH] fix: #293#issuecomment-1068807121 https://github.com/gzu-liyujiang/AndroidPicker/issues/293#issuecomment-1068807121 --- .../gzuliyujiang/filepicker/FileExplorer.java | 3 ++ .../filepicker/adapter/FileAdapter.java | 28 ++++++++----------- 2 files changed, 15 insertions(+), 16 deletions(-) diff --git a/FilePicker/src/main/java/com/github/gzuliyujiang/filepicker/FileExplorer.java b/FilePicker/src/main/java/com/github/gzuliyujiang/filepicker/FileExplorer.java index 894a73c8..d227d06c 100644 --- a/FilePicker/src/main/java/com/github/gzuliyujiang/filepicker/FileExplorer.java +++ b/FilePicker/src/main/java/com/github/gzuliyujiang/filepicker/FileExplorer.java @@ -116,10 +116,12 @@ public void onFileLoaded(@NonNull File file) { } if (itemCount < 1) { DialogLog.print("no files, or dir is empty"); + fileListView.setVisibility(View.INVISIBLE); emptyHintView.setVisibility(View.VISIBLE); emptyHintView.setText(emptyHint); } else { DialogLog.print("files or dirs count: " + itemCount); + fileListView.setVisibility(View.VISIBLE); emptyHintView.setVisibility(View.INVISIBLE); } pathListView.post(new Runnable() { @@ -201,6 +203,7 @@ public final void refreshCurrent(File current) { return; } loadingView.setVisibility(VISIBLE); + fileListView.setVisibility(View.INVISIBLE); emptyHintView.setVisibility(View.INVISIBLE); long millis = System.currentTimeMillis(); pathAdapter.updatePath(current); diff --git a/FilePicker/src/main/java/com/github/gzuliyujiang/filepicker/adapter/FileAdapter.java b/FilePicker/src/main/java/com/github/gzuliyujiang/filepicker/adapter/FileAdapter.java index da4d6ded..96b12bae 100644 --- a/FilePicker/src/main/java/com/github/gzuliyujiang/filepicker/adapter/FileAdapter.java +++ b/FilePicker/src/main/java/com/github/gzuliyujiang/filepicker/adapter/FileAdapter.java @@ -48,9 +48,9 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; -import java.util.LinkedList; import java.util.List; import java.util.concurrent.Callable; +import java.util.concurrent.ConcurrentLinkedQueue; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.FutureTask; @@ -73,7 +73,7 @@ public class FileAdapter extends RecyclerView.Adapter { private File currentFile = null; private String[] allowExtensions = null; private boolean loadAsync = true; - private final LinkedList> futureTasks = new LinkedList<>(); + private final ConcurrentLinkedQueue> futureTasks = new ConcurrentLinkedQueue<>(); private boolean onlyListDir = false; private boolean showHomeDir = true; private boolean showUpDir = true; @@ -293,22 +293,18 @@ public void loadData(final File dir) { reallyRefresh(loadDataSync(dir)); return; } - if (!futureTasks.isEmpty()) { - FutureTask futureTask = futureTasks.getFirst(); - if (futureTask != null && !futureTask.isDone()) { - futureTask.cancel(true); - } + FutureTask lastTask = futureTasks.peek(); + if (lastTask != null && !lastTask.isDone()) { + lastTask.cancel(true); } - FutureTask futureTask = new FutureTask<>(new Callable() { + FutureTask newTask = new FutureTask<>(new Callable() { @Override public Void call() { final List temp = loadDataSync(dir); - if (!futureTasks.isEmpty()) { - FutureTask futureTask = futureTasks.removeFirst(); - if (futureTask != null && futureTask.isCancelled()) { - DialogLog.print("data load is canceled: " + currentFile); - return null; - } + FutureTask task = futureTasks.poll(); + if (task != null && task.isCancelled()) { + DialogLog.print("data load is canceled: " + currentFile); + return null; } UI_HANDLER.post(new Runnable() { @Override @@ -319,8 +315,8 @@ public void run() { return null; } }); - futureTasks.addLast(futureTask); - THREAD_POOL.execute(futureTask); + futureTasks.add(newTask); + THREAD_POOL.execute(newTask); } @SuppressLint("NotifyDataSetChanged")