網(wǎng)站首頁 編程語言 正文
本文實(shí)例為大家分享了Android文件資源管理器雛形的實(shí)現(xiàn)代碼,供大家參考,具體內(nèi)容如下
學(xué)習(xí)Android,應(yīng)該在掌握單個(gè)知識點(diǎn)之后,把多個(gè)知識點(diǎn)串聯(lián)起來實(shí)現(xiàn)一些有一定代碼量的小項(xiàng)目比較好。當(dāng)然,這也是我教學(xué)中總結(jié)出來的一點(diǎn)經(jīng)驗(yàn)心得,并不適合所有人。
先做需求分析(實(shí)現(xiàn)的功能):
1.ListView開始顯示sdcard目錄下的子目錄和文件。
2.點(diǎn)擊文件,Toast顯示“點(diǎn)擊的是文件”
3.點(diǎn)擊目錄,進(jìn)入子目錄,顯示子目錄下的子目錄和文件。
4.back鍵回退到上層目錄。
5.異常情況處理:
5.1如果sdcard沒有插入,則不顯示列表,且提示用戶應(yīng)該插入sdcard后操作
5.2不允許進(jìn)入sdcard的上層目錄
下面開始實(shí)現(xiàn):
布局有兩個(gè):
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>
布局很簡單,就是放置了一個(gè)ListView控件,這里要注意的是,ListView標(biāo)簽下不能再放入其他的子控件。內(nèi)容是通過子布局和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>
子布局也很簡單,就是在水平方向上左邊顯示一個(gè)圖標(biāo),用來顯示文件夾或文件圖標(biāo),右邊顯示文件名。
3.Activity代碼(功能點(diǎn)寫在注釋中)
public class FileExplorerActivity extends Activity { ?? ?//Adapter中ICON和Filename鍵值對常量 ?? ?private static final String ICON = "icon"; ?? ?private static final String FILENAME = "filename"; ?? ?private TextView currentTv;//ListView上顯示當(dāng)前路徑的TextView ?? ?private ListView fileLv;//文件列表顯示的ListView ?? ?SimpleAdapter adapter;//適配器 ?? ?private List<HashMap<String, Object>> data;//填充的數(shù)據(jù) ?? ?private File root;//文件夾根節(jié)點(diǎn) ?? ?private File[] currentFiles; //根節(jié)點(diǎn)下的所有文件(包括文件夾) ?? ?private File currentPath;//記錄當(dāng)前節(jié)點(diǎn) ?? ?@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é)點(diǎn)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) { ?? ??? ??? ??? ?//點(diǎn)擊listview 項(xiàng)時(shí),如果是目錄,則進(jìn)入下一層次,如果是文件,不做處理 ?? ??? ??? ??? ?File currentPosFile = currentFiles[position]; ?? ??? ??? ??? ?if(currentPosFile.isDirectory()){ ?? ??? ??? ??? ??? ?getFileListFromSdcard(currentPosFile); ?? ??? ??? ??? ?}else{ ?? ??? ??? ??? ??? ?Toast.makeText(FileExplorerActivity.this, "您點(diǎn)擊的是文件夾", 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();//列出當(dāng)前目錄下的所有文件和目錄 ?? ??? ?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放進(jìn)去,再通知adapter ?? ??? ?if(data != null){ ?? ??? ??? ?data.clear(); ?? ??? ??? ?data.addAll(list); ?? ??? ??? ?adapter.notifyDataSetChanged(); ?? ??? ?} ?? ??? ?return list; ?? ?} }
運(yùn)行效果:
功能展望:
以上代碼是通過精簡功能達(dá)到的,如果要增加以下功能也是相當(dāng)之簡單的:
1.文件夾和文件的刪除功能
2.文件夾和文件的重命名功能
3.文件的分類調(diào)用App查看功能
4.文件詳細(xì)信息顯示功能
...
從上面示例可以看出,其實(shí)做一個(gè)文件資源管理器是相當(dāng)簡單的。
原文鏈接:https://blog.csdn.net/AB_BA/article/details/8759369
相關(guān)推薦
- 2023-01-15 PyQt5+QtChart實(shí)現(xiàn)繪制極坐標(biāo)圖_python
- 2022-11-30 關(guān)于pyinstaller?打包多個(gè)py文件的問題_python
- 2023-03-23 Pandas時(shí)間數(shù)據(jù)處理詳細(xì)教程_python
- 2022-10-15 C語言利用UDP實(shí)現(xiàn)群聊聊天室的示例代碼_C 語言
- 2022-06-21 Android?studio實(shí)現(xiàn)動(dòng)態(tài)背景頁面_Android
- 2022-08-25 windows下搭建Consul集群_云其它
- 2022-10-15 script 標(biāo)簽 async 屬性
- 2023-03-28 Go字符串操作深入解析_Golang
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支