仿原生安卓文件管理器

来源:互联网 发布:离散数学公开课 知乎 编辑:程序博客网 时间:2024/04/30 20:48

仿照安卓原生自带的文件管理器开发,这里只是简单写了个demo,依据现有代码可以很轻松实现后续开发,效果图如下:





首先新建一个listview_item,学过适配器的同学应该都知道一会要这是干什么的,就是为了绘制每个列表项的界面,这里采用图标+文件名

listview_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/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>



然后开始着手写我们的主界面布局,两层LinearLayout的嵌套

activity_main.xml

<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>




好了,现在静态的界面都已经准备好了,下面开始最关键的主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
 ( 踩)→┃你┃ ←(死 )
  \ ( →┃√┃ ← ) /
  \_)↗┗━┛ ↖(_/

3 1
原创粉丝点击