此次在原有基礎上增添了許多功能,包括刪除操做,重命名,複製和查找。
在adapter中直接執行刪除操做:
ide
/**
刪除
/
private void doRemove() {
final File file = filedata.get(position);
judgeAlertDialog(context, "提醒", "你確認刪除" + file.getName() + "嗎(不可逆)?", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
deleteDir(file);
filedata.remove(file);
notifyDataSetChanged();
showToast(file.getName() + " 刪除成功");
} }, null);
}
private void doRename() { showToast("重命名" + position); RenameFileDialog dialog = new RenameFileDialog(context, filedata, position); dialog.setOnFileRenameListener(new RenameFileDialog.OnFileRenameListener() { @Override public void onFileRenamed(boolean success) { String message = null; if (filedata.get(position).isFile()) { message = "文件"; } else { message = "文件夾"; } if (success) { message += "重命名成功"; } else { message += "重命名失敗"; } showToast(message); } }); dialog.show(); setfiledata(filedata); }
二、複製文件:this
以下圖所示:
使用了簡單的回調執行復制操做,在FileAdapter定義複製接口,而後在MainActivity中實現接口,並經過setListener將自身實例傳入FileAdapter,從而實如今FileAdapter中執行復制操做。
MainActivity中的粘貼操做實現以下:
code
private void doPaste() {
File newFile = new File(getPathString()+"/"+watingCopyFile.getName());
if (watingCopyFile.equals(null)) {
Snackbar.make(findViewById(R.id.main_view), "當前粘貼板爲空,不能粘貼", Snackbar.LENGTH_SHORT).show();
} else {
if (watingCopyFile.isFile()&&watingCopyFile.exists()){
try {
FileInputStream fis = new FileInputStream(watingCopyFile);
FileOutputStream fos = new FileOutputStream(newFile);
int len = -1;
long contentSize = watingCopyFile.length();
long readed = 0;
byte[] buff = new byte[8192];
while ((len=fis.read(buff))!=-1){
//寫文件
fos.write(buff,0,len);
readed+=len;
//發佈進度
}
fos.flush();
fis.close();
fos.close();
} catch (IOException e) {
e.printStackTrace();
} finally {
}
}
if (newFile.exists()) {
Toast.makeText(MainActivity.this,"複製" + newFile.getName() + "成功",Toast.LENGTH_SHORT).show();
fileAdapter.notifyDataSetChanged();
}
}
}
3: 查(對當前路徑下的遞歸查詢).
實現原理:
排序
data = new ArrayList<>();
searchfilemap = new HashMap<>();
searchByPath(path);
if (searchfilemap.size() > 0) {
//取出map中數據,賦值給data
Object[] list = searchfilemap.entrySet().toArray();
for (int i = 0; i < searchfilemap.size(); i++) {
data.add(new File(list[i].toString()));
}
}
private void searchByPath(String path) {
File[] files = new File(path).listFiles();
filenum += files.length;
publishProgress(filenum);
for (int i = 0; i < files.length; i++) {
File f = files[i];
if (f.isDirectory()) {
searchByPath(path + "/" + f.getName());
} else {
if (f.getName().contains(query)) {
searchfilemap.put(files[i], files[i].getName());
}
}
}
}
public class FileSortFactory { public static final int SORT_BY_FOLDER_AND_NAME = 1; public static final int SORT_BY_FOLDER_REVERSE_AND_NAME = 2; public static final int SORT_BY_FOLDER_AND_NAME_REVERSE = 3; public static final int SORT_BY_FOLDER_REVERSE_AND_NAME_REVERSE = 4; public static final int SORT_BY_FOLDER_AND_SIZE = 5; public static final int SORT_BY_FOLDER_REVERSE_AND_SIZE = 6; public static final int SORT_BY_FOLDER_AND_SIZE_REVERSE = 7; public static final int SORT_BY_FOLDER_REVERSE_AND_SIZE_REVERSE = 8; public static final int SORT_BY_FOLDER_AND_TIME = 9; public static final int SORT_BY_FOLDER_REVERSE_AND_TIME = 10; public static final int SORT_BY_FOLDER_AND_TIME_REVERSE = 11; public static final int SORT_BY_FOLDER_REVERSE_AND_TIME_REVERSE = 12; public static ComparatorgetWebFileQueryMethod( int method) { switch (method) { case SORT_BY_FOLDER_AND_NAME: return new SortByFolderAndName(true, true); case SORT_BY_FOLDER_REVERSE_AND_NAME: return new SortByFolderAndName(false, true); case SORT_BY_FOLDER_AND_NAME_REVERSE: return new SortByFolderAndName(true, false); case SORT_BY_FOLDER_REVERSE_AND_NAME_REVERSE: return new SortByFolderAndName(false, false); case SORT_BY_FOLDER_AND_SIZE: return new SortByFolderAndSize(true, true); case SORT_BY_FOLDER_REVERSE_AND_SIZE: return new SortByFolderAndSize(false, true); case SORT_BY_FOLDER_AND_SIZE_REVERSE: return new SortByFolderAndSize(true, false); case SORT_BY_FOLDER_REVERSE_AND_SIZE_REVERSE: return new SortByFolderAndSize(false, false); case SORT_BY_FOLDER_AND_TIME: return new SortByFolderAndTime(true, true); case SORT_BY_FOLDER_REVERSE_AND_TIME: return new SortByFolderAndTime(false, true); case SORT_BY_FOLDER_AND_TIME_REVERSE: return new SortByFolderAndTime(true, false); case SORT_BY_FOLDER_REVERSE_AND_TIME_REVERSE: return new SortByFolderAndTime(false, false); default: break; } return null; } }
以後再對應的具體排序方法類(這裏只列出一種):遞歸
public class SortByFolderAndName implements Comparator{ boolean first; boolean second; public SortByFolderAndName(boolean first,boolean second) { this.first = first; this.second = second; } @Override public int compare(File lhs, File rhs) { if (first) { if (!lhs.isFile() && rhs.isFile()) { return -1; } if (lhs.isFile() && !rhs.isFile()) { return 1; } } else { if (!lhs.isFile() && rhs.isFile()) { return 1; } if (lhs.isFile() && !rhs.isFile()) { return -1; } } if (second) { if (!(lhs.isFile() ^ rhs.isFile())) { return lhs.getName().compareTo(rhs.getName()); } } else { if (!(lhs.isFile() ^ rhs.isFile())) { return -lhs.getName().compareTo(rhs.getName()); } } return 0; } }
最後,在Adapter中經過排序方法實現對全部文件的排序:接口
private void sort() { Collections.sort(this.filedata, FileSortFactory.getWebFileQueryMethod(sortWay)); }