Android實現(xiàn)文件資源管理器雛形
本文實例為大家分享了Android文件資源管理器雛形的實現(xiàn)代碼,供大家參考,具體內容如下
學習Android,應該在掌握單個知識點之后,把多個知識點串聯(lián)起來實現(xiàn)一些有一定代碼量的小項目比較好。當然,這也是我教學中總結出來的一點經驗心得,并不適合所有人。
先做需求分析(實現(xiàn)的功能):
1.ListView開始顯示sdcard目錄下的子目錄和文件。
2.點擊文件,Toast顯示“點擊的是文件”
3.點擊目錄,進入子目錄,顯示子目錄下的子目錄和文件。
4.back鍵回退到上層目錄。
5.異常情況處理:
5.1如果sdcard沒有插入,則不顯示列表,且提示用戶應該插入sdcard后操作
5.2不允許進入sdcard的上層目錄
下面開始實現(xiàn):
布局有兩個:
1.主布局:file_list.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" ? ? xmlns:tools="http://schemas.android.com/tools" ? ? android:layout_width="match_parent" ? ? android:layout_height="match_parent" ? ? tools:context=".FileExplorerActivity" > ?? ?<TextView? ?? ? ? ?android:id="@+id/currentTv" ?? ? ? ?android:layout_alignParentTop="true" ?? ? ? ?android:clickable="true" ?? ? ? ?android:layout_width="match_parent" ? ? ? ? android:layout_height="wrap_content"/> ? ? <ListView? ? ? ? ? android:id="@+id/fileLv" ? ? ? ? android:layout_below="@id/currentTv" ? ? ? ? android:layout_alignParentBottom="true" ? ? ? ? android:layout_width="match_parent" ? ? ? ? android:layout_height="wrap_content"> ? ? ? ?? ? ? </ListView> ? ?? </RelativeLayout>
布局很簡單,就是放置了一個ListView控件,這里要注意的是,ListView標簽下不能再放入其他的子控件。內容是通過子布局和Adapter來顯示的。
2.ListView中的子布局file_list_item.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" ? ? android:layout_width="match_parent" ? ? android:layout_height="match_parent" ? ? android:orientation="horizontal" > ? ? <ImageView? ? ? ? ? android:id="@+id/icon" ? ? ? ? android:layout_width="wrap_content" ? ? ? ? android:layout_height="wrap_content" ? ? ? ? /> ? ? <TextView ? ? ? ? android:id="@+id/filename" ? ? ? ? android:layout_width="wrap_content" ? ? ? ? android:layout_height="wrap_content"/> ? </LinearLayout>
子布局也很簡單,就是在水平方向上左邊顯示一個圖標,用來顯示文件夾或文件圖標,右邊顯示文件名。
3.Activity代碼(功能點寫在注釋中)
public class FileExplorerActivity extends Activity { ?? ?//Adapter中ICON和Filename鍵值對常量 ?? ?private static final String ICON = "icon"; ?? ?private static final String FILENAME = "filename"; ?? ?private TextView currentTv;//ListView上顯示當前路徑的TextView ?? ?private ListView fileLv;//文件列表顯示的ListView ?? ?SimpleAdapter adapter;//適配器 ?? ?private List<HashMap<String, Object>> data;//填充的數(shù)據 ?? ?private File root;//文件夾根節(jié)點 ?? ?private File[] currentFiles; //根節(jié)點下的所有文件(包括文件夾) ?? ?private File currentPath;//記錄當前節(jié)點 ?? ?@Override ?? ?protected void onCreate(Bundle savedInstanceState) { ?? ??? ?super.onCreate(savedInstanceState); ?? ??? ?setContentView(R.layout.activity_file_explorer); ?? ??? ? ?? ??? ?currentTv = (TextView)findViewById(R.id.currentTv); ?? ??? ?fileLv = (ListView)findViewById(R.id.fileLv); ?? ??? ? ?? ??? ?//得到根節(jié)點root -->/mnt/sdcard ?? ??? ?root = getFileSystemRoot(); ?? ??? ?//得到第一屏的信息 ?? ??? ?if(root != null){ ?? ??? ??? ?//從/mnt/sdcard下得到文件列表 ?? ??? ??? ?data = getFileListFromSdcard(root); ?? ??? ?}else{ ?? ??? ??? ?//如果沒有掛載sdcard,則提示用戶 ?? ??? ??? ?data = new ArrayList<HashMap<String, Object>>(); ?? ??? ??? ?HashMap<String, Object> map = new HashMap<String, Object>(); ?? ??? ??? ?map.put(ICON, R.drawable.none); ?? ??? ??? ?map.put(FILENAME, "逗我玩啊,插卡啊"); ?? ??? ??? ?data.add(map); ?? ??? ?} ?? ??? ?//創(chuàng)建Adapater ?? ??? ?adapter = new SimpleAdapter( ?? ??? ??? ??? ??? ?this,? ?? ??? ??? ??? ??? ?data,? ?? ??? ??? ??? ??? ?R.layout.file_list_item,? ?? ??? ??? ??? ??? ?new String[]{ICON, FILENAME},? ?? ??? ??? ??? ??? ?new int[]{R.id.icon, R.id.filename}); ?? ??? ? ?? ??? ?fileLv.setAdapter(adapter); ?? ??? ?//綁定事件 ?? ??? ?fileLv.setOnItemClickListener(new OnItemClickListener() { ?? ??? ??? ?@Override ?? ??? ??? ?public void onItemClick(AdapterView<?> parent, View view, ?? ??? ??? ??? ??? ?int position, long id) { ?? ??? ??? ??? ?//點擊listview 項時,如果是目錄,則進入下一層次,如果是文件,不做處理 ?? ??? ??? ??? ?File currentPosFile = currentFiles[position]; ?? ??? ??? ??? ?if(currentPosFile.isDirectory()){ ?? ??? ??? ??? ??? ?getFileListFromSdcard(currentPosFile); ?? ??? ??? ??? ?}else{ ?? ??? ??? ??? ??? ?Toast.makeText(FileExplorerActivity.this, "您點擊的是文件夾", Toast.LENGTH_LONG).show(); ?? ??? ??? ??? ?} ?? ??? ??? ?} ?? ??? ?}); ?? ?} ? ?? ?/** ?? ? * 攔截back鍵返回 ?? ? * @return ?? ? */ ?? ?@Override ?? ?public boolean onKeyDown(int keyCode, KeyEvent event) { ?? ??? ?if(KeyEvent.KEYCODE_BACK == keyCode){ ?? ??? ??? ?File parentFile = currentPath.getParentFile(); ?? ??? ??? ?//不能超過最頂層 ?? ??? ??? ?try { ?? ??? ??? ??? ?if(parentFile.getCanonicalPath().toString().equals("/mnt")){ ?? ??? ??? ??? ??? ?Toast.makeText(this, "別按了,到家了", Toast.LENGTH_LONG).show(); ?? ??? ??? ??? ??? ?return true; ?? ??? ??? ??? ?}else{ ?? ??? ??? ??? ??? ?getFileListFromSdcard(parentFile); ?? ??? ??? ??? ?} ?? ??? ??? ?} catch (IOException e) { ?? ??? ??? ??? ?e.printStackTrace(); ?? ??? ??? ?} ?? ??? ?} ?? ??? ?return super.onKeyDown(keyCode, event); ?? ?} ?? ?private File getFileSystemRoot() { ?? ??? ?//首先得到Sd卡是否加載了 ?? ??? ?if(Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())){ ?? ??? ??? ?//得到sd卡路徑 root --> /mnt/sdcard ?? ??? ??? ?root = Environment.getExternalStorageDirectory(); ?? ??? ?}else{ ?? ??? ??? ?Toast.makeText(this, "逗我玩啊,插卡啊", Toast.LENGTH_LONG).show(); ?? ??? ?} ?? ??? ?return root; ?? ?} ? ?? ?/** ?? ? * 得到Sdcard中的文件列表 ?? ? * @return ?? ? */ ?? ?private List<HashMap<String, Object>> getFileListFromSdcard(File root) { ?? ??? ?try { ?? ??? ??? ?currentPath = root; ?? ??? ??? ?currentTv.setText(root.getCanonicalPath().toString()); ?? ??? ?} catch (IOException e) { ?? ??? ??? ?e.printStackTrace(); ?? ??? ?} ?? ??? ?List<HashMap<String, Object>> list = new ArrayList<HashMap<String, Object>>(); ?? ??? ?currentFiles = root.listFiles();//列出當前目錄下的所有文件和目錄 ?? ??? ?for(File f : currentFiles){ ?? ??? ??? ?HashMap<String, Object> map = new HashMap<String, Object>(); ?? ??? ??? ?String fileName = f.getName(); ?? ??? ??? ?int icon; ?? ??? ??? ?if(f.isDirectory()){ ?? ??? ??? ??? ?icon = R.drawable.dir; ?? ??? ??? ??? ?map.put(ICON, icon); ?? ??? ??? ?}else if(f.isFile()){ ?? ??? ??? ??? ?icon = R.drawable.file; ?? ??? ??? ??? ?map.put(ICON, icon); ?? ??? ??? ?} ?? ??? ??? ?map.put(FILENAME, fileName); ?? ??? ??? ?list.add(map); ?? ??? ?} ?? ??? ?//把原來的data list清空,然后把list放進去,再通知adapter ?? ??? ?if(data != null){ ?? ??? ??? ?data.clear(); ?? ??? ??? ?data.addAll(list); ?? ??? ??? ?adapter.notifyDataSetChanged(); ?? ??? ?} ?? ??? ?return list; ?? ?} }
運行效果:
功能展望:
以上代碼是通過精簡功能達到的,如果要增加以下功能也是相當之簡單的:
1.文件夾和文件的刪除功能
2.文件夾和文件的重命名功能
3.文件的分類調用App查看功能
4.文件詳細信息顯示功能
...
從上面示例可以看出,其實做一個文件資源管理器是相當簡單的。
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
Android中獲得正在運行的程序和系統(tǒng)服務的方法
這篇文章主要介紹了Android中獲得正在運行的程序和系統(tǒng)服務的方法,分別是對ActivityManager.RunningAppProcessInfo類和ActivityManager.RunningServiceInfo類的使用,需要的朋友可以參考下2016-02-02Android Studio三方引用報錯但是項目可以運行的解決方案
今天小編就為大家分享一篇關于Android Studio三方引用報錯但是項目可以運行的解決方案,小編覺得內容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2019-03-03搭建Android上的服務器 “實現(xiàn)隔空取物”的方法
本篇文章主要介紹了搭建Android上的服務器 “實現(xiàn)隔空取物”的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-01-01Android編程實現(xiàn)自定義Tab選項卡功能示例
這篇文章主要介紹了Android編程實現(xiàn)自定義Tab選項卡功能,結合完整實例形式分析了Android自定義tab選項卡的遍歷、設置及屬性操作相關技巧,需要的朋友可以參考下2017-02-02iOS中給UITableView的側滑刪除增加多個按鈕的實現(xiàn)方法
在項目中遇到這樣一個需求,cell的側滑刪除默認只有一個刪除按鈕, 給側滑添加多個按鈕, '刪除', '置頂', '更多'.怎么實現(xiàn)呢?下面小編給大家分享iOS中給UITableView的側滑刪除增加多個按鈕的實現(xiàn)方法,一起看看吧2017-02-02