标签:文件 ret 情况 string 包括 eid change 列表 表视图
XC文件管理器,是基于Android6.0开发的一个方便易用的文件管理器,具有文件的目录管理和文件的管理,主要包括文件的新建、删除、重命名、复制,移动剪切以及文件详情查看等文件和目录的功能,同时支持文件和目录的批量管理,应该功能较全,视图提供两种:网格视图以及列表视图,应用界面简洁美观,易用性强,是较好易用的一款Android文件管理应用。
1.整体页面框架采用Header+body+Footer三段式设计,核心首页页面以及Footer布局代码如下:
<RelativeLayout xmlns:android=" http://schemas.android.com/apk/res/android " xmlns:tools=" http://schemas.android.com/tools " android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="#F6F6F6" > <include layout="@layout/main_header"/> <include layout="@layout/main_start"/> <include layout="@layout/main_path"/> <include layout="@layout/sdcard_listview"/> <include layout="@layout/sdcard_gridview"/> <include layout="@layout/morefile_menu_listview"/> <include layout="@layout/file_search_view"/> <include layout="@layout/main_footer"/> </RelativeLayout> <RelativeLayout xmlns:android=" http://schemas.android.com/apk/res/android " xmlns:tools=" http://schemas.android.com/tools " android:layout_width="fill_parent" android:layout_height="60dp" android:layout_alignParentBottom="true" android:background="#F6F6F6" android:id="@+id/main_relativelayout_footer" > <LinearLayout android:id="@+id/llWelcomeSmile" android:layout_width="fill_parent" android:layout_height="60dp" android:orientation="horizontal" android:gravity="center" > <ImageView android:id="@+id/ivWelcomeSmile" android:layout_width="wrap_content" android:layout_height="60dp" android:src="@drawable/smile" android:visibility="visible" /> <TextView android:id="@+id/tvWelcomeText" android:layout_width="wrap_content" android:layout_height="60dp" android:gravity="center_vertical" android:text=" 欢迎您使用XC文件管理器" android:textColor="#222222" android:visibility="visible" /> </LinearLayout> <GridView android:id="@+id/gvFooterView" android:layout_width="fill_parent" android:layout_height="60dp" android:layout_alignParentBottom="true" android:visibility="gone" android:listSelector="@drawable/meu_item_gb_blue2" android:background="#04AEDA" > </GridView> <GridView android:id="@+id/gvFileMenuGrid" android:layout_width="fill_parent" android:layout_height="60dp" android:layout_alignParentBottom="true" android:visibility="gone" android:listSelector="@drawable/meu_item_gb_blue2" android:background="#04AEDA" > </GridView> <GridView android:id="@+id/gvCopyAndMoveMenuGrid" android:layout_width="fill_parent" android:layout_height="60dp" android:layout_alignParentBottom="true" android:visibility="gone" android:listSelector="@drawable/meu_item_gb_blue2" android:background="#04AEDA" > </GridView> </RelativeLayout>
2.获取手机文件目录核心实现
/** * 设置MainList的数据 */ public void setMainlistData(){ PhoneDir dir = new PhoneDir(); dir.setImageID(R.drawable.sdcard); dir.setStrDirName("存储卡/SD卡目录"); String dAllSpace = CommonUtils.getSdCardAllSpace(); String dFreeSpace = CommonUtils.getSdCardFreeSpace(); String strOther = ""+dFreeSpace +"GB可用/"+dAllSpace+"GB总计"; dir.setStrDirOther(strOther); mainlist.add(dir); dir = new PhoneDir(); dir.setImageID(R.drawable.systemdir); dir.setStrDirName("系统目录/手机目录"); dir.setStrDirOther("请谨慎操作系统目录文件"); mainlist.add(dir); } /** * 设置要显示的文件列表数据的数据 */ public void setFileListData(String strPath){ String strShowPath; if(strCurPath.contains(CommonUtils.getSdCardRoot())){ strShowPath = strCurPath.replaceAll(CommonUtils.getSdCardRoot(), "我的手机》存储卡"); }else{ strShowPath = strCurPath.replaceAll(FileUtils.getPhoneRoot(), "我的手机》系统目录"); } strShowPath =strShowPath.replaceAll("/", "》"); tvShowPath.setText(strShowPath); ArrayList fileList = FileUtils.getFilesOfCurDir(strPath); FileItem item ; File file ; sdcardlist.clear(); sdcardGrid.clear(); for(int i = 0;i<fileList.size();i++){ item = new FileItem(); file = (File) fileList.get(i); if(file.isDirectory()){//目录情况 item.setImageID(R.drawable.folder); item.setbIsDir(true); item.setnFileSize(0); }else{//文件情况 item.setImageID(R.drawable.file); item.setbIsDir(false); item.setnFileSize(file.length()); } item.setStrFilePath(file.getAbsolutePath()); item.setStrFileName(file.getName()); item.setStrDateTime(FileUtils.getFileLastModifiedTime(file)); sdcardlist.add(item); sdcardGrid.add(item); } }
3.实现列表和缩略图方格UI布局样式切换
该功能主要是基于ListView和GridView进行UI 和数据适配器的更新切换,核心代码如下
if(mViewTypeStatus == Constant.VIEW_LISTVIEW) {//ListView切换到GridView llSdcardList.setVisibility(View.GONE); llSdcardGrid.setVisibility(View.VISIBLE); sdcardListView.setVisibility(View.GONE); sdcardGridView.setVisibility(View.VISIBLE); ImageView iv = (ImageView)view.findViewById(R.id.ivItemImage); iv.setImageResource(R.drawable.footer_menu_list); mViewTypeStatus = Constant.VIEW_GRIDVIEW; setFileListData(strCurPath); refreshData(); } else{//GridView切换到ListView llSdcardList.setVisibility(View.VISIBLE); llSdcardGrid.setVisibility(View.GONE); sdcardGridView.setVisibility(View.GONE); sdcardListView.setVisibility(View.VISIBLE); ImageView iv = (ImageView)view.findViewById(R.id.ivItemImage); iv.setImageResource(R.drawable.footer_menu_grid); mViewTypeStatus = Constant.VIEW_LISTVIEW; setFileListData(strCurPath); refreshData(); } break; /** * 刷新当前页面(ListView或者GridView)数据 */ public void refreshData(){ if(mViewTypeStatus == Constant.VIEW_LISTVIEW){ sdcardListAdapter = new SdcardListAdapter(MainActivity.this, sdcardlist); sdcardListView.setAdapter(sdcardListAdapter); }else { sdcardGridAdapter = new SdcardGridAdapter(MainActivity.this, sdcardGrid); sdcardGridView.setAdapter(sdcardGridAdapter); } if(sdcardlist.size() <= 0 ){ ToastUtils.ToastAtCenterPosition(MainActivity.this, "目录为空,没有任何文件!"); } }
4.添加文件搜索功能
搜索功能是搜索文件目录下的所有符合搜索关键字的文件或者目录,相关初始化和核心实现代码如下:
/** * 初始化 搜索框视图 即rlSearchView视图 */ public void initSearchView(){ //初始化搜索框视图 rlSearchView = (RelativeLayout)findViewById(R.id.rlSearchView); rlSearchView.setVisibility(View.GONE); final EditText editText = (EditText)rlSearchView.findViewById(R.id.etSearchTextView); editText.addTextChangedListener(new TextWatcher() { @Override public void onTextChanged(CharSequence s, int start, int before, int count) { // TODO Auto-generated method stub String condition = editText.getText().toString(); if (condition.equals("")) { setFileListData(strCurPath); refreshData(); }else{ searchFilesList.clear(); searchSdCardFiles(new File(strCurPath)); setSearchListData(condition); refreshData(); } } @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { // TODO Auto-generated method stub } @Override public void afterTextChanged(Editable s) { // TODO Auto-generated method stub } }); ImageView ivCancelSearch = (ImageView)rlSearchView.findViewById(R.id.ivCancelSearch); ivCancelSearch.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub rlSearchView.setVisibility(View.GONE); } }); } /** * 遍历SD卡 */ public void searchSdCardFiles(File root){ File files[] = root.listFiles(); if(files != null) for(File f:files){ if(f.isDirectory()){ searchFilesList.add(f); searchSdCardFiles(f); } else{ searchFilesList.add(f); } } } /** * 设置要显示的搜索出来的文件列表数据的数据 */ public void setSearchListData(String condition){ String strShowPath = "搜索结果:"; tvShowPath.setText(strShowPath); FileItem item ; File file ; sdcardlist.clear(); for(int i = 0;i<searchFilesList.size();i++){ item = new FileItem(); file = (File) searchFilesList.get(i); if(!file.getAbsolutePath().contains(condition)) continue; if(file.isDirectory()){//目录情况 item.setImageID(R.drawable.folder); item.setbIsDir(true); item.setnFileSize(0); }else{//文件情况 item.setImageID(R.drawable.file); item.setbIsDir(false); item.setnFileSize(file.length()); } item.setStrFilePath(file.getAbsolutePath()); item.setStrFileName(file.getName()); item.setStrDateTime(FileUtils.getFileLastModifiedTime(file)); sdcardlist.add(item); } }
5.由于本项目代码量比较多,在此就不把其他所有功能都一一罗列了,其他的常规的复制,剪切,新建文件等功能,以及版权和检查更新弹窗功能实现,请大家下载源码具体,谢谢大家,希望对大家有所帮助。
标签:文件 ret 情况 string 包括 eid change 列表 表视图
原文地址:https://www.cnblogs.com/demodashi/p/10503350.html