仿原生安卓文件管理器

仿原生安卓文件管理器

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


仿原生安卓文件管理器

 

仿原生安卓文件管理器

 

 

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

listview_item.xml

<span style="font-size:18px;"><?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:andro
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" >
    
    <ImageView
        android:
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
    
    <TextView 
        android:
        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:andro
    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:
          android:text="返回"
          android:background="#00EE76"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"/>
      
      <TextView 
          android:
          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:
      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 ;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		init() ;
	}


	/*
	 * 初始化控件
	 */
	private void init() {
		// TODO Auto-generated method stub
		this.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() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				try {
					if(!currentParent.getCanonicalFile().equals(Environment.getExternalStorageDirectory().getPath())) {
						currentParent = currentParent.getParentFile() ; //获取上级目录
						currentFiles = currentParent.listFiles() ;             //取得当前层所有文件
						inflateListView(currentFiles);                             //更新列表
					}
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		});

		this.listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {

			@Override
			public 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 stub
		List<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
 ( 踩)→┃你┃ ←(死 )
  ( →┃√┃ ← ) /
  \_)↗┗━┛ ↖(_/