博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
文件ListView
阅读量:6083 次
发布时间:2019-06-20

本文共 3960 字,大约阅读时间需要 13 分钟。

hot3.png

package com.example.testlistview;import java.io.File;import java.util.ArrayList;import java.util.Collections;import android.content.Context;import android.util.AttributeSet;import android.view.View;import android.widget.AdapterView;import android.widget.ArrayAdapter;import android.widget.ListView;public class MyListView extends ListView {	private Context mContext = null;	private ArrayList
mList = new ArrayList
(); private ArrayList
mFolderList = new ArrayList
(); private ArrayList
mFileList = new ArrayList
(); private ArrayAdapter
mAdapter = null; // Property private String mPath = ""; // Event private OnPathChangedListener mOnPathChangedListener = null; private OnFileSelectedListener mOnFileSelectedListener = null; public MyListView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); init(context); } public MyListView(Context context, AttributeSet attrs) { super(context, attrs); init(context); } public MyListView(Context context) { super(context); init(context); } private void init(Context context) { mContext = context; setOnItemClickListener(mOnItemClick); } private boolean openPath(String path) { mFolderList.clear(); mFileList.clear(); File file = new File(path); File[] files = file.listFiles(); if (files == null) { return false; } for (int i = 0; i < files.length; i++) { if (files[i].isDirectory()) { mFolderList.add("<" + files[i].getName() + ">"); } else { mFileList.add(files[i].getName()); } } Collections.sort(mFolderList); Collections.sort(mFileList); mFolderList.add(0, "<..>"); return true; } private void updateAdapter() { mList.clear(); mList.addAll(mFolderList); mList.addAll(mFileList); mAdapter = new ArrayAdapter
(mContext, android.R.layout.simple_list_item_1, mList); setAdapter(mAdapter); } public void setPath(String value) { if (value.length() == 0) { value = "/"; } else { String lastChar = value.substring(value.length() - 1, value.length()); if (lastChar.matches("/") == false) { value = value + "/"; } } if (openPath(value)) { mPath = value; updateAdapter(); if (mOnPathChangedListener != null) { mOnPathChangedListener.onChanged(value); } } } public String getPath() { return mPath; } public void setOnPathChangedListener(OnPathChangedListener value) { mOnPathChangedListener = value; } public OnPathChangedListener getOnPathChangedListener() { return mOnPathChangedListener; } public void setOnFileSelected(OnFileSelectedListener value) { mOnFileSelectedListener = value; } public OnFileSelectedListener getOnFileSelected() { return mOnFileSelectedListener; } public String DelteRight(String value, String border) { String list[] = value.split(border); String result = ""; for (int i = 0; i < list.length; i++) { result = result + list[i] + border; } return result; } private String delteLastFolder(String value) { String list[] = value.split("/"); String result = ""; for (int i = 0; i < list.length - 1; i++) { result = result + list[i] + "/"; } return result; } private String getRealPathName(String newPath) { String path = newPath.substring(1, newPath.length() - 1); if (path.matches("..")) { return delteLastFolder(mPath); } else { return mPath + path + "/"; } } private AdapterView.OnItemClickListener mOnItemClick = new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView
arg0, View arg1, int position, long id) { String fileName = getItemAtPosition(position).toString(); if (fileName.matches("<.*>")) { setPath(getRealPathName(fileName)); } else { if (mOnFileSelectedListener != null) { mOnFileSelectedListener.onSelected(mPath, fileName); } } } }; interface OnFileSelectedListener { public void onSelected(String path, String fileName); } interface OnNotifyEventListener { public void onNotify(Object sender); } interface OnPathChangedListener { public void onChanged(String path); }}

转载于:https://my.oschina.net/xsjayz/blog/290199

你可能感兴趣的文章
第三章 计算机及服务器硬件组成结合企业运维场景 总结
查看>>
IntelliJ IDEA解决Tomcal启动报错
查看>>
默认虚拟主机设置
查看>>
php中的短标签 太坑人了
查看>>
[译] 可维护的 ETL:使管道更容易支持和扩展的技巧
查看>>
### 继承 ###
查看>>
数组扩展方法之求和
查看>>
astah-professional-7_2_0安装
查看>>
函数是对象-有属性有方法
查看>>
uva 10107 - What is the Median?
查看>>
Linux下基本栈溢出攻击【转】
查看>>
c# 连等算式都在做什么
查看>>
使用c:forEach 控制5个换行
查看>>
java web轻量级开发面试教程摘录,java web面试技巧汇总,如何准备Spring MVC方面的面试...
查看>>
使用ansible工具部署ceph
查看>>
linux系列博文---->深入理解linux启动运行原理(一)
查看>>
Android反编译(一) 之反编译JAVA源码
查看>>
结合当前公司发展情况,技术团队情况,设计一个适合的技术团队绩效考核机制...
查看>>
python-45: opener 的使用
查看>>
cad图纸转换完成的pdf格式模糊应该如何操作?
查看>>