前言
额,突然心血来潮,把这个功能给做出来了。之前一直以为要一次遍历手机的所有文件再显示出来,后来仔细想了会,没那么复杂,只需要先指定一个路径再获取其子目录,即可完成我想要的效果。
效果
传送门
https://github.com/Ccapton/FileChooser
### 项目结构
FileChooser、FileChooserActivity、FileTourController 这三个类实现界面与文件列表展示逻辑的耦合与内聚。
不过,因为我用到了databinding框架和vectorDrawable文件,所以要在app的build.gradle中如下图绿色框配置代码。
FileChooser.java
package com.capton.fc;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.support.v4.app.Fragment;
/**
* Created by capton on 2018/1/5.
*/
public class FileChooser {
private Context mContext;
private int themeColorRes = R.color.themeColor;
private FileChoosenListener fileChoosenListener;
private String mChoosenFilePath = "";
private String title = "选择目标";
private String doneText = "完成";
private int backIconRes = R.drawable.back_white;
private boolean showFile = true;
public boolean isFileShow() {
return showFile;
}
public FileChooser showFile(boolean showFile) {
this.showFile = showFile;
return this;
}
public FileChooser setCurrentPath(String currentPath){
this.mChoosenFilePath = currentPath;
return this;
}
public FileChooser setTitle(String title){
this.title = title;
return this;
}
public FileChooser setDoneText(String doneText) {
this.doneText = doneText;
return this;
}
public FileChooser setBackIconRes(int backIconRes) {
this.backIconRes = backIconRes;
return this;
}
public FileChooser(Fragment fragment , FileChoosenListener fileChoosenListener) {
this.mContext = fragment.getContext();
this.fileChoosenListener = fileChoosenListener;
}
public FileChooser(Activity activity ,FileChoosenListener fileChoosenListener) {
this.mContext = activity;
this.fileChoosenListener = fileChoosenListener;
}
public void open(){
FileChooserActivity.mFileChooser = this;
Intent intent = new Intent(mContext,FileChooserActivity.class);
intent.putExtra("themeColorRes",this.themeColorRes);
intent.putExtra("currentPath",this.mChoosenFilePath);
intent.putExtra("title",this.title);
intent.putExtra("doneText",this.doneText);
intent.putExtra("backIconRes",this.backIconRes);
intent.putExtra("showFile",this.showFile);
this.mContext.startActivity(intent);
}
protected void finish(String filePath){
if(fileChoosenListener != null)
fileChoosenListener.onFileChoosen(filePath);
}
public FileChooser setThemeColor(int themeColorRes){
this.themeColorRes = themeColorRes;
return this;
}
public FileChooser setFileChoosenListener(FileChoosenListener fileChoosenListener) {
this.fileChoosenListener = fileChoosenListener;
return this;
}
public interface FileChoosenListener{
void onFileChoosen(String filePath);
}
}
FileChooserActivity.java
package com.capton.fc;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.ListPopupWindow;
import android.view.View;
import android.widget.AdapterView;
import com.blankj.utilcode.util.Utils;
import com.capton.fc.databi