仿原生安卓檔案管理工具,安卓檔案管理工具

來源:互聯網
上載者:User

仿原生安卓檔案管理工具,安卓檔案管理工具

仿照安卓原生內建的檔案管理工具開發,這裡只是簡單寫了個demo,依據現有代碼可以很輕鬆實現後續開發,如下:





首先建立一個listview_item,學過適配器的同學應該都知道一會要這是幹什麼的,就是為了繪製每個清單項目的介面,這裡採用表徵圖+檔案名稱

listview_item.xml

<span style="font-size:18px;"><?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/name"        android:textSize="25sp"        android:layout_marginLeft="10dp"        android:layout_gravity="center_vertical"        android:textColor="#080808"        android:layout_width="wrap_content"        android:layout_height="wrap_content"/>            </LinearLayout></span>



然後開始著手寫我們的主介面布局,兩層LinearLayout的嵌套

activity_main.xml

<span style="font-size:18px;"><LinearLayout 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"    android:orientation="vertical"    tools:context="com.example.filemanagerdemo.MainActivity" >      <LinearLayout       android:layout_width="fill_parent"      android:layout_height="wrap_content"      android:orientation="horizontal">            <Button           android:id="@+id/parent"          android:text="返回"          android:background="#00EE76"          android:layout_width="wrap_content"          android:layout_height="wrap_content"/>            <TextView           android:id="@+id/textview"          android:layout_width="wrap_content"          android:layout_height="wrap_content"          android:layout_weight="1"/>       </LinearLayout>    <ListView       android:layout_width="wrap_content"      android:layout_height="0dp"      android:layout_marginTop="3dp"      android:id="@+id/listview"      android:dividerHeight="1dp"      android:layout_weight="1">  </ListView></LinearLayout></span>




好了,現在靜態介面都已經準備好了,下面開始最關鍵的主Activity的代碼講解

MainActivity.java

<span style="font-size:18px;">package com.example.filemanagerdemo;import java.io.File;import java.io.IOException;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;import android.app.Activity;import android.os.Bundle;import android.os.Environment;import android.view.View;import android.view.View.OnClickListener;import android.widget.AdapterView;import android.widget.Button;import android.widget.ListView;import android.widget.SimpleAdapter;import android.widget.TextView;import android.widget.Toast;public class MainActivity extends Activity {private Button parent ;private ListView listview ;private TextView textview ;private File currentParent ;    //記錄當前檔案的父資料夾private File[] currentFiles ;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);init() ;}/* * 初始化控制項 */private void init() {// TODO Auto-generated method stubthis.parent = (Button)findViewById(R.id.parent) ;this.listview = (ListView)findViewById(R.id.listview) ;this.textview = (TextView)findViewById(R.id.textview) ;File root = new File(Environment.getExternalStorageDirectory().getPath()) ;if(root.exists()) {currentParent = root ;currentFiles = root.listFiles() ;inflateListView(currentFiles) ;}this.parent.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {// TODO Auto-generated method stubtry {if(!currentParent.getCanonicalFile().equals(Environment.getExternalStorageDirectory().getPath())) {currentParent = currentParent.getParentFile() ; //擷取上級目錄currentFiles = currentParent.listFiles() ;             //取得當前層所有檔案inflateListView(currentFiles);                             //更新列表}} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}});this.listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {@Overridepublic void onItemClick(AdapterView<?> parent, View view,int position, long id) {// TODO Auto-generated method stub//如果點擊的是檔案,不做任何處理if(currentFiles[position].isFile()) {return ;}File[] temp = currentFiles[position].listFiles() ;if(temp == null || temp.length == 0) {Toast.makeText(getApplicationContext(), "此檔案夾不可用或者檔案夾為空白",Toast.LENGTH_LONG).show(); ;return ;}currentParent = currentFiles[position] ;currentFiles = temp ;inflateListView(currentFiles);}}) ;}/* * 更新listview */private void inflateListView(File[] files) {// TODO Auto-generated method stubList<Map<String,Object>> list = new ArrayList<Map<String, Object>>() ;for(int i = 0 ;i<files.length ;i++) {Map<String,Object> item= new HashMap<String, Object>() ;if(files[i].isDirectory()) {item.put("icon", R.drawable.folder) ;}else {item.put("icon", R.drawable.file) ; //這裡可以寫成根據具體的檔案類型添加不同的表徵圖}item.put("name", files[i].getName()) ;list.add(item) ;}SimpleAdapter adapter = new SimpleAdapter(getApplicationContext(),list,R.layout.listview_item, new String[]{"icon","name"}, new int[]{R.id.icon,R.id.name}) ;this.listview.setAdapter(adapter);try{  textview.setText(currentParent.getCanonicalPath());  }catch (IOException e){  e.printStackTrace();  }  }}</span>

   從onCreate開始閱讀,代碼應該不難。Ok,一個仿安卓原生系統的檔案管理工具demo就開發完畢,這個demo為了能讓大家看的清晰,我用了很不規範的書寫習慣,比如字串應該放進string.xml  ,顏色也最好不要直接使用RGB編碼,應該在value下建立color.xml給這個RGB編碼指定一個名字。這裡為了原理講解清晰,我全部使用了寫入程式碼,還有就是我擷取SD卡根目錄的方式太簡單粗暴,正常應該專門寫一個方法來判斷各種異常情況,比如判斷有沒有SD卡或當前SD卡是否可用,否則一旦使用者執行一點異常操作,我們的程式就崩了,這就是正式項目與demo的區別吧。


OK that is all,有錯誤之處,希望瀏覽者能不吝指正。

覺得還不錯的話歡迎點一下下面的

頂!


d=====( ̄▽ ̄*)b


或者



 oooO ┏━┓ Oooo
 ( 踩)→┃你┃ ←(死 )
  \ ( →┃√┃ ← ) /
  \_)┗━┛ (_/

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.