android文件管理器,通知界面修改;4.4以后读取sd卡也需要权限;ctrl+shift+f注意在英文下;取消屏幕闪烁

来源:互联网 发布:战舰世界 睦月 数据 编辑:程序博客网 时间:2024/05/21 12:41

主界面

<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=".MainActivity" >
    <TextView
        android:id="@+id/title_text"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:text="当前位置:/mnt/sdcard" />

    <!-- android:cacheColorHint="#00000000"防止屏幕闪烁 -->
    <ListView
        android:id="@+id/list"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="8"
        android:cacheColorHint="#00000000" >
    </ListView>

</LinearLayout>

每行元素的界面

<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="wrap_content"
    android:orientation="horizontal" >
    <TextView 
        android:layout_height="wrap_content" 
        android:layout_width="0dp" 
        android:layout_weight="1" 
        android:id="@+id/file_img"/>
<TextView 
   android:layout_height="wrap_content" 
   android:layout_width="0dp" 
   android:layout_weight="5" 
   android:id="@+id/file_name"/>
</LinearLayout>


Mainactivity:

package com.kane.filemanager;


import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


import com.kane.filemanager.adapter.FileAdapter;
import com.kane.filemanager.util.Globals;


import android.os.Bundle;
import android.os.Environment;


import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.view.KeyEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.ListView;
import android.widget.TextView;

@SuppressLint("DefaultLocale")//忽略某些警告
public class MainActivity extends Activity {
private TextView titleText;
private ListView list;
private List<Map<String, Object>> allValues=new ArrayList<Map<String,Object>>();
private FileAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

Globals.init(this);

setContentView(R.layout.activity_main);
titleText=(TextView)findViewById(R.id.title_text);
list=(ListView)findViewById(R.id.list);
//读取SD卡不要权限,写入需要权限,但4.4读取也需要权限
File root =Environment.getExternalStorageDirectory();
loadFileData(root);
//建立adapter
adapter=new FileAdapter(this, allValues);
list.setAdapter(adapter);
//加入监听,每行点击事件
list.setOnItemClickListener(new OnItemClickListener() {

@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
//取得当前操作行对象
Map<String,Object> map=allValues.get(arg2);
//判断点的是是不是文件夹
boolean dirFlag=(Boolean)map.get("dirFlag");//封装,然后自动拆箱
if (dirFlag) {
//读取绝对路径
String fullPath=(String)map.get("fullPath");
//建立该文件对象
File dir=new File(fullPath);
//将该文件下所有文件读入到list,先得清空
allValues.clear();
String rootPath=Environment.getExternalStorageDirectory().getAbsolutePath().toString();
//判断当前是哪个目录,如果是SD卡根目录,就不应该在界面显示“返回上一级”;这是点进一个文件夹后,第一行显示的数据
if (!rootPath.equals(fullPath)) {

//加入可以点击返回上一级的按钮
Map<String,Object> parentData=new HashMap<String, Object>();
parentData.put("fileName", "返回上一级");
parentData.put("extName", "dir_open");
parentData.put("dirFlag", true);
parentData.put("fullPath", dir.getParent());
//表示当前是 真的“返回上一级”、以防有人自己建立“返回上一级”文件夹
parentData.put("real","TRUE");
allValues.add(parentData);

}
//添加数据
loadFileData(dir);
// 如果只修改数据,界面不会得到改变的通知,当用户再进行操作时,会出现错误.
// 这里就需要通知界面进行修改.
adapter.notifyDataSetChanged();

}
else {
//点的是文件
File file = new File(map.get("fullPath").toString());
//想弹出一个对话框
Builder alert=new Builder(MainActivity.this);
//对话框标题
alert.setTitle("文件详情");
//内容
alert.setMessage("文件大小:"+file.length()+"\r\n"+"文件名:"+file.getName());
//消极按钮,一般有三种按钮
alert.setNegativeButton("关闭", new OnClickListener() {

@Override
public void onClick(DialogInterface dialog, int which) {
//该事件自带一个功能,关闭对话框

}
});
//显示对话框
alert.create().show();
}
}
});
list.setOnItemLongClickListener(new OnItemLongClickListener() {
/**
* 参数arg1是整个行用什么界面,arg2是行号
*/
@Override
public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
final int arg2, long arg3) {
// 取得要删除的数据
Map<String, Object> map = allValues.get(arg2);
//取得文件路径
final String path=(String)map.get("fullPath");
boolean dirFlag=(Boolean)map.get("dirFlag");
if (!dirFlag) {
//文件才可以删除
Builder notify=new Builder(MainActivity.this);
notify.setMessage("确定要删除该文件(" + map.get("fileName")+ ")吗?");
notify.setPositiveButton("确定", new OnClickListener() {

@Override
public void onClick(DialogInterface dialog, int which) {
//将文件夹中的文件真正删除,要在manifest中获得权限 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
File f= new File(path);
if (f.exists()) {
f.delete();
}

// 列表中删除这条数据
allValues.remove(arg2);
//通知界面修改
adapter.notifyDataSetChanged();

}
});
notify.setNegativeButton("取消", new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {


}
});
notify.create().show();
}
return false;
}
});
}

/**
* 界面按键事件,现在都是触屏了,主要针对返回键
*/
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode==KeyEvent.KEYCODE_BACK) {//是返回键
//判断当前List集合第一条数据是否是“返回上一级”
//如果是真的有返回上一级
if ("TRUE".equals(allValues.get(0).get("real"))) {
list.performItemClick(list.getChildAt(0), 0, list.getChildAt(0).getId());//执行整个列表的第一行的功能即返回上一级
}
else {
//没有返回上一级,是真的退出
Builder builder = new Builder(this);
builder.setTitle("提示");
builder.setMessage("亲,真的要退出吗?");
builder.setPositiveButton("真的", new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// 结束当前Activity程序
finish();
}
});
builder.setNegativeButton("暂不", new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});


builder.create().show();
}
//取消返回键原有作用
return false;
}
//保留一般的返回键功能,默认关闭activity
return super.onKeyDown(keyCode, event);
}




/**
* 根据父文件夹确定接下来的列表
* @param parentFile
*/
public void loadFileData(File parentFile) {
//加入修改当前位置的提示信息
titleText.setText("当前位置:"+parentFile.getAbsolutePath());
//列出该目录下的所有文件
File[] allFiles=parentFile.listFiles();
if (allFiles!=null) {
for (int i = 0; i < allFiles.length; i++) {
File file=allFiles[i];
Map<String,Object> map=new HashMap<String, Object>();
//保存文件或文件夹名称
map.put("fileName", file.getName());
//保存文件的整体路径
map.put("fullPath", file.getAbsolutePath());
String extName="";
//判断是文件还是文件夹
if (file.isDirectory()) {
//文件夹
extName="dir_close";

map.put("dirFlag",true);

}
else {
//文件
extName=file.getName().substring(file.getName().lastIndexOf(".")+1)
.toLowerCase().toString();

map.put("dirFlag",false);
}
map.put("extName",extName);
//将map集合加入到list
allValues.add(map);
}
}
}

}

FileAdapter类

package com.kane.filemanager.adapter;


import java.util.ArrayList;
import java.util.List;
import java.util.Map;


import com.kane.filemanager.R;
import com.kane.filemanager.util.Globals;




import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView.LayoutParams;
import android.widget.BaseAdapter;
import android.widget.TextView;


public class FileAdapter extends BaseAdapter {
private Context ctx;
private List<Map<String,Object>> allValues =new ArrayList<Map<String,Object>>();
//传入参数,其他配置(simpleadapter需要四个参数)在此类设置
public FileAdapter(Context ctx,List<Map<String,Object>> allValues){
this.ctx=ctx;
this.allValues=allValues;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return allValues.size();
}
/**
* 获得对应行元素
*/
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return allValues.get(position);
}
/**
* 获得对应行的ID
*/
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
/**
* convertView是为了提高效率,循环利用组件;position对应行号
*/
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if(convertView==null){
//需要从xml中读取组件,因为是自定义的
convertView=LayoutInflater.from(ctx).inflate(R.layout.file_line, null);
//需要动态设置高度,不同手机,界面大小不一样
convertView.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
Globals.SCREEN_HEIGHT/10));

}
//处理内容的设置
TextView fileImg=(TextView)convertView.findViewById(R.id.file_img);
TextView fileName=(TextView)convertView.findViewById(R.id.file_name);
//取得数据
Map<String,Object> map=allValues.get(position);
fileName.setText(map.get("fileName").toString());
//设置图片,根据传入的扩展名,对应什么照片
String extName=map.get("extName").toString();
fileImg.setBackgroundResource(Globals.allImgsMap.get(extName));
return convertView;
}
}

公共方法,全局界面参数

package com.kane.filemanager.util;


import java.util.HashMap;
import java.util.Map;


import com.kane.filemanager.R;


import android.app.Activity;


public class Globals {


public static int SCREEN_WIDTH;
public static int SCREEN_HEIGHT;
//为了服务根据文件扩展名,查找照片
public static Map<String, Integer> allImgsMap = new HashMap<String, Integer>();


public static void init(Activity a) {
// 初始化屏幕的宽度和高度
SCREEN_WIDTH = a.getWindowManager().getDefaultDisplay().getWidth();
SCREEN_HEIGHT = a.getWindowManager().getDefaultDisplay().getHeight();


// 初始化所有的图片名称和图片ID.
allImgsMap.put("txt", R.drawable.txt_file);
allImgsMap.put("mp3", R.drawable.mp3_file);
allImgsMap.put("mp4", R.drawable.mp4_file);
allImgsMap.put("bmp", R.drawable.image_file);
allImgsMap.put("jpg", R.drawable.image_file);
allImgsMap.put("gif", R.drawable.image_file);
allImgsMap.put("png", R.drawable.image_file);
allImgsMap.put("dir_open", R.drawable.open_dir);
allImgsMap.put("dir_close", R.drawable.close_dir);
}
}


14 0
原创粉丝点击