activity_main.xml, 增加剪头, 和空文件夹提示
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/mainLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="${relativePackage}.${activityClass}" >
<RelativeLayout
android:id="@+id/relative_layout"
android:layout_width="fill_parent"
android:layout_height="45dip"
android:background="@drawable/list_item_background_normal" >
<RelativeLayout
android:id="@+id/current_path_pane"
android:layout_width="fill_parent"
android:layout_height="40dip"
android:layout_alignParentLeft="true"
android:layout_marginRight="48dip"
android:background="@drawable/path_pane_bg"
android:layout_marginTop="1dip" >
<ImageView
android:id="@+id/img_arrow"
android:layout_width="30dip"
android:layout_height="16dip"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:contentDescription="@string/img_des"
/>
<TextView
android:id="@+id/txt_current_path"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_toLeftOf="@id/img_arrow"
android:ellipsize="start"
android:singleLine="true"
android:textSize="12sp" />
</RelativeLayout>
<ImageView
android:id="@+id/img_up_level"
android:layout_width="40dip"
android:layout_height="40dip"
android:layout_alignParentRight="true"
android:contentDescription="@string/img_des"
android:src="@drawable/img_back_btn_press" />
</RelativeLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@id/relative_layout"
android:layout_marginBottom="5dip" >
<ListView
android:id="@+id/fileListView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#FFFFFF"
android:cacheColorHint="#00000000" >
</ListView>
</LinearLayout>
<LinearLayout
android:id="@+id/layout_empty"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:orientation="vertical"
android:visibility="visible" >
<ImageView
android:id="@+id/img_no_sub_file"
android:layout_width="80dip"
android:layout_height="80dip"
android:background="@drawable/empty_icon"
android:contentDescription="@string/img_des" />
<TextView
android:id="@+id/txtEmpty"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:text="@string/txt_empty"
android:textColor="#9D9D9D"
android:textSize="20sp" />
</LinearLayout>
</RelativeLayout>
pop_window.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<ListView
android:id="@+id/pathListView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#FFFFFF"
android:cacheColorHint="#00000000"
android:divider="#00000000" >
</ListView>
</LinearLayout>
抽出MainActivity中文件处理代码成FileUtil类。
package com.lxm.filebrowser;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import android.util.Log;
/**
*
* @author lxmgfd
*
*/
public class FileUtil
{
/**
* 实际使用的数据
* @param files
* @return
*/
public static List<String> getPath(String[] files)
{
List<String> list = new ArrayList<String>();
int len = files.length - 1;
for (int i = 0; i < len; i++)
{
list.add(files[i]);
}
return list;
}
/**
* 用于界面显示的数据, 树状
* @param files
* @return
*/
public static List<String> getTree(String[] files)
{
List<String> list = new ArrayList<String>();
int len = files.length - 1;
String temp = "";
for (int i = 0; i < len; i++)
{
temp = getSpaceByCount(i)+files[i];
list.add(temp);
Log.v("test", "i="+i+" temp="+temp);
}
return list;
}
/**
* 设置树形前缀
* 如:
* └─XXX
* @param count
* @return
*/
public static String getSpaceByCount(int count)
{
StringBuffer sbSpace = new StringBuffer();
String space = " ";
String line = "└─";
count = count + 1;
for (int i = 0; i < count; i++)
{
sbSpace.append(space);
}
sbSpace.append(line);
return sbSpace.toString();
}
/**
* 根据当前点击的目录获得当前目录
* @param list
* @param index
* @return
*/
public static String getCurPath(List<String> list, int index)
{
StringBuffer path = new StringBuffer();
int len = list.size();
String temp = "";
for (int i = 0; i < len; i++)
{
if (i > index)
{
break;
}
if (i == 0) // 代表根节点
{
temp = list.get(i);
}
else
{
temp = File.separator+list.get(i);
}
path.append(temp);
}
return path.toString();
}
/**
* 获得指定路径下面的文件名或目录名
* @param path
* @return
*/
public static List<String> getFilesByPath(String path)
{
List<String> list = new ArrayList<String>();
File[] files = new File(path).listFiles();
String name = "";
for (File file : files)
{
name = file.getName();
list.add(name);
}
return list;
}
}
MainActivity修改其中的弹窗为成员变量, 更好的控制弹窗无需重绘多次。
package com.lxm.filebrowser;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.os.Environment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;
/**
*
* @author lxmgfd
*
*/
public class MainActivity extends Activity
{
private RelativeLayout mCurrentPathPane; // 文件路径布局
private LinearLayout mEmptyFile; // 没有文件布局
private TextView mTxtCurrentPath; // 当前路径
private ImageView mImgUpLevel; // 放回上一级图片按钮
private ImageView mImgArrow; // 剪头图片按钮
private PopupWindow mPopWindow; // 弹窗
private ListView mListView; // 显示当前目录下的listview
private StringArrayAdapter mAdapter;
private ListView mPopListView; // 弹窗listview
private StringArrayAdapter mPopAdapter;
private String root; // SDcard根目录
private String curPath;
private List<String> subPathList; // 当前目录下的文件集合
private List<String> listPath; // 弹窗使用的目录集合
private String SDCARD = "SD卡";
private int ARROW_UP = R.drawable.arrow_up;
private int ARROW_DOWN = R.drawable.arrow_down;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initUI();
}
private void initUI()
{
root = Environment.getExternalStorageDirectory().toString();
curPath = root;
mCurrentPathPane = (RelativeLayout) findViewById(R.id.current_path_pane);
mEmptyFile = (LinearLayout) findViewById(R.id.layout_empty);
mTxtCurrentPath = (TextView) findViewById(R.id.txt_current_path);
mImgUpLevel = (ImageView) findViewById(R.id.img_up_level);
mImgArrow = (ImageView) findViewById(R.id.img_arrow);
mListView = (ListView) findViewById(R.id.fileListView);
initPopWindow();
changePath();
mCurrentPathPane.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (curPath.equals(root))
{
return;
}
showPopWindow();
}
});
mImgUpLevel.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
curPath = new File(curPath).getParentFile().toString();
changePath();
closePopWindow();
}
});
mListView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
String temp = curPath + File.separator + subPathList.get(position);
File file = new File(temp);
if (file.isDirectory()) // 当前文件为目录则进入该目录。
{
curPath = temp;
changePath();
}
}
});
}
/**
* 当前目录发生变化时调用函数, 修改当前目录位置, 设置返回按钮是否显示, 设置当前目录下的所有文件
*/
private void changePath()
{
setCurrentPath(mTxtCurrentPath, curPath.replace(root, SDCARD));
showImgView();
setListViewValue();
showEmptyFileView();
}
/**
* 设置按钮上面的文字
* @param txtView
* @param strText
*/
private void setCurrentPath(TextView txtView, String strText){
txtView.setText(strText);
}
/**
* 显示返回按钮和剪头
*/
private void showImgView()
{
int flag = View.VISIBLE;
if (curPath.equals(root))
{
flag = View.INVISIBLE;
}
mImgArrow.setVisibility(flag);
setArrowDirection(ARROW_DOWN);
mImgUpLevel.setVisibility(flag);
}
private void setArrowDirection(int dir)
{
mImgArrow.setBackgroundResource(dir);
}
/**
* 列出当前文件目录下所有文件
*/
private void setListViewValue()
{
subPathList = FileUtil.getFilesByPath(curPath);
if (null == mAdapter)
{
mAdapter = new StringArrayAdapter(getApplicationContext(), subPathList, 1);
}
mAdapter.setList(subPathList);
mListView.setAdapter(mAdapter);
}
/**
* 是否显示没有文件
*/
private void showEmptyFileView()
{
int flag = View.GONE;
if (subPathList.size() < 1)
{
flag = View.VISIBLE;
}
mEmptyFile.setVisibility(flag);
}
/**
* 初始化弹窗
*/
@SuppressWarnings("deprecation")
@SuppressLint("InflateParams")
private void initPopWindow()
{
LayoutInflater inflater = LayoutInflater.from(getApplicationContext());
View view = inflater.inflate(R.layout.pop_window, null);
mPopWindow = new PopupWindow(findViewById(R.id.mainLayout), LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
mPopWindow.setContentView(view);
mPopWindow.setFocusable(false);
mPopWindow.setBackgroundDrawable(new BitmapDrawable());
mPopListView = (ListView) view.findViewById(R.id.pathListView);
mPopListView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
clickPopItem(position);
closePopWindow();
}
} );
}
/**
* 处理点击位置
* @param position
*/
private void clickPopItem(int position)
{
if (curPath.endsWith(listPath.get(position)) == false)
{
curPath = FileUtil.getCurPath(listPath, position);
changePath();
}
}
/**
* 显示弹窗
* @param view
*/
private void showPopWindow()
{
if (mPopWindow.isShowing() == false)
{
setPopListViewValue();
mPopWindow.showAsDropDown(findViewById(R.id.relative_layout));
mPopWindow.setOutsideTouchable(false);
setArrowDirection(ARROW_UP);
}
else
{
closePopWindow();
}
}
/**
* 关闭
*/
private void closePopWindow()
{
mPopWindow.dismiss();
setArrowDirection(ARROW_DOWN);
}
/**
* 设置弹窗数据
*/
private void setPopListViewValue()
{
if (curPath.equals(root))
{
return ;
}
String path = curPath.replace(root + File.separator, "");
String[] files = path.split(File.separator);
List<String> listTree = new ArrayList<String>(); // UI显示数据
listPath = new ArrayList<String>(); // 实际使用数据
listPath.add(root); // 添加根节点
listPath.addAll(FileUtil.getPath(files));
listTree.add(SDCARD); // 添加根节点
listTree.addAll(FileUtil.getTree(files));
if (null == mPopAdapter)
{
mPopAdapter = new StringArrayAdapter(this, listTree, 2);
}
mPopAdapter.setList(listTree);
mPopListView.setAdapter(mPopAdapter);
}
}