此次在原有基础上增添了许多功能,包括删除操做,重命名,复制和查找。
在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)); }