public class SelectorFileActivity extends Activity{ @BindView(R.id.btn_back) ImageView btnBack; @BindView(R.id.selector_file_lsit) RecyclerView selectorFileLsit; @BindView(R.id.file_path) TextView filePath; File[] currentfiles; String pathfile; File parentfile; @BindView(R.id.no_files) TextView noFiles; @BindView(R.id.search_file) EditText searchFile; @BindView(R.id.search_img) ImageView searchImg; private Intent intent; private Bundle bundle;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_selector_file_list);
ButterKnife.bind(this);
initData();
}
private void initData() {
intent = getIntent();
bundle = intent.getExtras();
int type = intent.getIntExtra("type", 0);
File file = new File("要跳转的路径");
if (!file.exists()) {//判断文件是否存在
try {
//按照指定的路径创建文件夹
file.mkdirs();
} catch (Exception e) {
// TODO: handle exception
}
}
pathfile = file.getAbsolutePath(); // 获取绝对路径
filePath.setText("当前路径为" + pathfile);
parentfile = file;//父级路径
currentfiles = file.listFiles();当前路径下的所有文件
this.inflatefiles(currentfiles);
btnBack.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (parentfile.toString().equals(Environment.getExternalStorageDirectory().toString())) {
finish();
} else {
//当点击上一级时,使用parentfile,来刷新listview,
parentfile = parentfile.getParentFile();
currentfiles = parentfile.listFiles();
SelectorFileActivity.this.inflatefiles(currentfiles);
filePath.setText("当前路径为" + parentfile.getAbsolutePath());
}
}
});
}
public void inflatefiles(final File[] files) {
List<String> list = new ArrayList<>();
List<String> searchList = new ArrayList<>();
Log.i("lenght", String.valueOf(files.length));
for (int i = 0; i < files.length; i++) {
list.add(files[i].getName());
}
if (files.length < 1) {
selectorFileLsit.setVisibility(View.GONE);
noFiles.setVisibility(View.VISIBLE);
return;
}
selectorFileLsit.setVisibility(View.VISIBLE);
noFiles.setVisibility(View.GONE);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(SelectorFileActivity.this);
selectorFileLsit.setLayoutManager(linearLayoutManager);
FileAdapter fileAdapter = new FileAdapter(SelectorFileActivity.this, list, files);
selectorFileLsit.setAdapter(fileAdapter);
fileAdapter.setItemClick(new FileAdapter.setOnClick() {
@Override
public void setItemClick(int position) {
if (files[position].isFile()) {
String absolutePath = files[position].getAbsolutePath();
bundle.putString("path", absolutePath);//添加要返回的数据
intent.putExtras(bundle);
SelectorFileActivity.this.setResult(Activity.RESULT_OK, intent);
SelectorFileActivity.this.finish();
return;
} else {
if (files[position].listFiles() != null) {
File[] f = files[position].listFiles();
currentfiles = f;
SelectorFileActivity.this.inflatefiles(f);
pathfile = files[position].getAbsolutePath();
filePath.setText("当前路径" + pathfile);
parentfile = files[position];
}
}
}
});
}
}
|