安卓文件夹路径选择功能的实现

开发环境:Android Studio
库文件下载:compile ‘li.filechoose:FileDirChoose:1.0.3’(可以看一下Android Studio3.0 关键字 compile 和 implementation 的区别,有坑)
github地址:https://github.com/zw21544182/RecordDemo(filechoose)
关键知识:RecyclerView.Adapter的使用
最终效果:
效果.gif
界面代码比较简单,就不贴了
先来看看初始化权限

 private void initData() {
        if (Build.VERSION.SDK_INT >= 23) {//判断系统版本是否大于6.0
            if (checkSelfPermission(NEED_PERMISSION) == PackageManager.PERMISSION_GRANTED) {
                //检查是否有读写权限
                loadDataFrompATH(mPath);//从路径中加载数据 
            } else {
                requestPermissions(new String[]{NEED_PERMISSION}, REQUESCODE);//申请权限
            }
            return;
        }
        loadDataFrompATH(mPath);//系统版本小于6.0直接加载数据
    }

接下来是加载数据方法

private void loadDataFrompATH(final String mPath) {
        data.clear();//data为RecyclerView中要显示的数据
        new Thread() {
            public void run() {
                super.run();
                File file = new File(mPath);
                File[] listFiles = file.listFiles();//获取子文件
                for (File f : listFiles
                        ) {
                    if (!f.isDirectory() || f.getName().startsWith(".")) {//如果不是路径或者以 . 开头的文件夹 则直接跳过
                        continue;
                    }
                    data.add(f.getAbsolutePath());//往集合中添加符合条件的数据
                }
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        fileAdapter.setData(data);//将数据载入适配器当中
                    }
                });
            }
        }.start();

    }

#重点来了
适配器FileAdapter的onBindViewHolder方法

    @Override
    public void onBindViewHolder(final ViewHolder holder, final int position) {
        final String path = data.get(position);
        boolean isChecked = false;//初始化一个boolean值 来判断是否被选中
        for (String s : selectData
                ) {//遍历选中的集合
            if (s.trim().equals(path)) {//如果集合中的子元素与适配其中的路径相同
                isChecked = true;//判断已被选中
                break;//终止循环
            }
        }
        holder.checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {//设置checkBox的点击事件
                if (b) {//选中状态
                    if (selectData.contains(path))//如果集合中包含了该元素则直接返回
                        return;
                    else//否则添加
                        selectData.add(path);
                } else {//未选中状态
                    if (selectData.contains(path))//如果集合中包含了该元素则移除
                        selectData.remove(path);
                    else//否则 返回
                        return;
                }
            }
        });
        holder.checkBox.setChecked(isChecked);
        holder.tvFileDir.setText(path.substring(path.lastIndexOf("/") + 1));
        holder.rootLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (!holder.checkBox.isChecked()) //获取checkBox的选中状态来判断是否能进入下一个文件夹
                {
                    event.enterNextDir(data.get(position));//接口,用于在activity中回调
                } else {
                    Toast.makeText(context, context.getString(R.string.filechoose_already), Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

再看看ativity中的回调方法

  fileAdapter.setEvent(new FileAdapter.Event() {
            @Override
            public void enterNextDir(String path) {
                editText.setText("");
                mPath = path;//将子路径赋值给当前路径
                loadDataFrompATH(mPath);//加载数据
            }
        });

最后是我在上传库的时候遇到的问题,源码库已经上传成功了,但是把库导入项目时,运行报错,原来是我在配置文件中没有注意android studio 3.0 中 implementation 和 compile的区别 导致无法找到RecyclerVier控件
有兴趣的朋友可以去看一看啦

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值