@Override public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) { // TODO Auto-generated method stub System.out.println(">>>>>>onItemLongClick->position:" + position + " id:" + id); final int location = position; MyFile myFile = mData.get(location); // 如果是路径 暂时先不处理 File file = new File(myFile.getPath()); if (file.isDirectory() || "返回".equals(myFile.getName())) { Toast.makeText(this, "当前是文件夹或返回", Toast.LENGTH_SHORT).show(); return false; } AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setTitle("提示"); builder.setMessage("确定要删除" + myFile.getName() + (file.isDirectory() ? "文件夹" : "文件")); builder.setPositiveButton( "确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub mData.remove(location); fileListAdapter.notifyDataSetChanged(); } }); builder.setNegativeButton( "取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub dialog.dismiss(); } }); builder.show(); return false; }
@Override public void onClick(View view, int position, long id) { // TODO Auto-generated method stub MyFile myFile = mData.get(position); File file = new File(myFile.getPath()); if (file.isDirectory()) { // 如果是文件夹节点,则进入下一层,并初始化数据 initFileData(myFile.getPath()); } else { // Toast.makeText(MainActivity.this, "阅读器待开发", Toast.LENGTH_SHORT).show(); LayoutInflater layoutInflater = LayoutInflater.from(this); View v = layoutInflater.inflate(R.layout.popup_item, null); PopupWindow pw = new PopupWindow(v, LayoutParams.MATCH_PARENT, 400, true); pw.setBackgroundDrawable(getResources().getDrawable(R.drawable.local_popup_bg)); TextView tv_fileName = (TextView) v.findViewById(R.id.tv_fileName); tv_fileName.setText(myFile.getName()); TextView tv_filePath = (TextView) v.findViewById(R.id.tv_filePath); tv_filePath.setText(myFile.getPath()); TextView tv_modifyTime = (TextView) v.findViewById(R.id.tv_modifyTime); tv_modifyTime.setText(myFile.getModifyTime()); // 获得Item在屏蔽上的坐标位置 int[] location = new int[2]; view.getLocationInWindow(location); int windowWidth = FunctionHelper.getWindowWidth(this); pw.showAtLocation( view, Gravity.TOP | Gravity.LEFT, location[0] + windowWidth / 8, location[1] - 100); } }
@Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { // TODO Auto-generated method stub System.out.println(">>>>>>onItemClick->position:" + position + " id:" + id); MyFile myFile = mData.get(position); File file = new File(myFile.getPath()); if (file.isDirectory()) { // 如果是文件夹节点,则进入下一层,并初始化数据 initFileData(myFile.getPath()); } else if (file.getParentFile() == null) { Toast.makeText(MainActivity.this, "无上级目录", Toast.LENGTH_SHORT).show(); return; } else { // Toast.makeText(MainActivity.this, "阅读器待开发", Toast.LENGTH_SHORT).show(); // Intent intent = new // Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); // startActivityForResult(intent, 1); // 如果是图片则读取图片 if (myFile.isImg()) { Intent intent = new Intent(); intent.setAction("ACTION_PICK"); intent.putExtra("path", myFile.getPath()); intent.putExtra("type", FILE_TYPE_IMG); startActivity(intent); } else { // 如果是txt/html/xml 类型的文件则打开 String path = myFile.getPath().toLowerCase(); if (path.endsWith(".txt") || path.endsWith(".html") || path.endsWith(".xml") || path.endsWith(".cfg")) { Intent intent = new Intent(); intent.setAction("ACTION_PICK"); intent.putExtra("path", myFile.getPath()); intent.putExtra("type", FILE_TYPE_TXT); startActivity(intent); } else { Toast.makeText(MainActivity.this, "未知文件", Toast.LENGTH_SHORT).show(); } } } }
/** * 初始化数据 传入的必须是文件夹节点 * * @param fileNode */ private void initFileData(String fileNode) { // 清理数据列表 clearData(); try { // 文件 节点 File pfile = new File(fileNode); // 将节点下面的文件及文件夹(不包含子目录下面的)生成数组 File[] listFiles = pfile.listFiles(); // 添加返回目录 mData.add( 0, new MyFile( "返回", BitmapFactory.decodeResource(getResources(), R.drawable.back), // 获取 当前节点 的上级节点绝对路径 pfile.getParentFile().getAbsolutePath(), false)); MyFile myFile = null; for (File file : listFiles) { myFile = new MyFile(); // 文件名 myFile.setName(file.getName()); // 文件绝对路径 myFile.setPath(file.getAbsolutePath()); // 文件最后修改时间 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); myFile.setModifyTime(sdf.format(new Date(file.lastModified()))); // 如果是文件夹 if (file.isDirectory()) { // 设置为文件夹的图片 myFile.setIcon(BitmapFactory.decodeResource(getResources(), R.drawable.folder)); // 设置为非图片 myFile.setIsImg(false); } else { if (file.getName() != null && (file.getName().toLowerCase().endsWith(".jpg") || file.getName().toLowerCase().endsWith(".jpeg") || file.getName().toLowerCase().endsWith(".png"))) { // 使用缓存技术,这里不加载图片 // myFile.setIcon(BitmapFactory.decodeFile(file.getAbsolutePath())); myFile.setIcon(null); // 设置为图片 myFile.setIsImg(true); } else { myFile.setIcon(BitmapFactory.decodeResource(getResources(), R.drawable.file)); // 设置为非图片 myFile.setIsImg(false); } } mData.add(myFile); } } catch (Exception e) { // TODO: handle exception } // 创建文件列表适配器adapter fileListAdapter = new FileExplorerAdapter(this, mData); // 适配器加载视图 lv.setAdapter(fileListAdapter); // 设置条目单击监听 lv.setOnItemClickListener(this); // 设置条目长按监听 lv.setOnItemLongClickListener(this); // 设置条目长按监听 fileListAdapter.setOnItemButtonClickListener(this); }