Android 一天撸一个文件路径选择器FileChooser

前言

额,突然心血来潮,把这个功能给做出来了。之前一直以为要一次遍历手机的所有文件再显示出来,后来仔细想了会,没那么复杂,只需要先指定一个路径再获取其子目录,即可完成我想要的效果。

效果

传送门

https://github.com/Ccapton/FileChooser
### 项目结构
2121515255059_.pic.jpg
FileChooser、FileChooserActivity、FileTourController 这三个类实现界面与文件列表展示逻辑的耦合与内聚。

不过,因为我用到了databinding框架和vectorDrawable文件,所以要在app的build.gradle中如下图绿色框配置代码。
image.png

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
  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值