一、主要思路
主要是通过ListView实现。
考虑到以后会添加长按修改功能,所以好几个地方都是用的FramLayout,而且CheckBox初始状态是被隐藏的。给ListView添加OnItemClickedListener,在识别点击的Item是文件夹时做出响应,把ListView的资源来源改成该文件夹。同时顶部的TextView也要修改成对应文件夹目录。
目前我没有对返回键设置监听器,所以返回上一级菜单是通过顶部的按钮实现的。在根目录下该按钮隐藏,等到进入下一级目录的时候按钮就会显示出来。
二、FileBean
FileBean是文件/文件夹的抽象类,包含文件所需的一些必要属性。
package com.example.vcloud_3_25.bean;
import java.util.Date;
/*
* 包含文件的通用属性和方法
*/
public class FileBean {
public static final int ROOT = 0x7fffffff;
private int id;
private int pId;
private String name;
private String type;
private String info;
private Date date;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getpId() {
return pId;
}
public void setpId(int pId) {
this.pId = pId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getInfo() {
return info;
}
public void setInfo(String info) {
this.info = info;
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
public FileBean(int id, int pId, String name, String type, Date date) {
super();
this.id = id;
this.pId = pId;
this.name = name;
this.type = type;
this.date = date;
}
}
三、FileHelper
我是这样考虑的,要把一个目录下的所有文件加载出来,就需要设置pId-->FileBeanList的对应关系,所以最方便的就是通过Map实现。在编写过程中程序提示我可以使用SparseArray来代替HashMap,可以提升效率,所以我就使用SparseArray来实现了。至于初始数据如何得到我还没有考虑,所以就在构造函数里自定义了一些数据,这些FileBean是没有预先排好序列,也不具备我预想的pId-->FileBeanList结构,所以我在FileHelper这个类中编写了基础的排序以及结构化的函数。
package com.example.vcloud_3_25.utils;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
import android.util.SparseArray;
import com.example.vcloud_3_25.bean.FileBean;
public class FileHelper {
private List<FileBean> mList;
public FileHelper(){
FileBean f1 = new FileBean(1, FileBean.ROOT, "folder1", "folder",
new Date());
FileBean f2 = new FileBean(2, FileBean.ROOT, "avi1", "avi", new Date());
FileBean f3 = new FileBean(3, FileBean.ROOT, "folder2", "folder",
new Date());
FileBean f4 = new FileBean(4, 1, "txt1", "txt", new Date());
FileBean f5 = new FileBean(5, 1, "txt2", "txt", new Date());
FileBean f6 = new FileBean(6, 1, "txt3", "txt", new Date());
FileBean f7 = new FileBean(7, 1, "txt4", "txt", new Date());
FileBean f8 = new FileBean(8, FileBean.ROOT, "txt5", "txt", new Date());
FileBean f9 = new FileBean(9, FileBean.ROOT, "txt6", "txt", new Date());
mList = new ArrayList<FileBean>();
mList.add(f1);
mList.add(f2);
mList.add(f3);
mList.add(f4);
mList.add(f5);
mList.add(f6);
mList.add(f7);
mList.add(f8);
mList.add(f9);
}
public SparseArray<List<FileBean>> getResMap() {
Collections.sort(mList, new MyComparator());
SparseArray<List<FileBean>> map = new SparseArray<List<FileBean>>();
for (FileBean f : mList) {
if (getChildren(f.getId()) != null)
map.put(f.getId(), getChildren(f.getId()));
else
map.put(f.getId(), new ArrayList());
}
return map;
}
public List<FileBean> getChildren(int id) {
List<FileBean> list = new ArrayList<FileBean>();
for (FileBean f : mList) {
if (f.getpId() == id)
list.add(f);
}
Collections.sort(list, new MyComparator());
return list;
}
class MyComparator implements Comparator<FileBean> {
@Override
public int compare(FileBean f1, FileBean f2) {
if (f1.getType() == f2.getType()) {
return f1.getName().compareTo(f2.getName());
} else if (f1.getType().equals("folder")) {
return -1;
} else if (f2.getType().equals("folder")) {
return 1;
}
return f1.getType().compareTo(f2.getType());
}
}
}
四、HomeFragment
这个就稍微臃肿了一点,我在考虑后期重构这段代码。
有一个需要注意的点,就是如何刷新ListView的问题。这在网络上能很容易的找到答案,就是先clear一下List,然后用addAll函数添加数据,最后使用Adapter的notifyDataSetChanged()方法更新数据。因为常用所以我把它抽取为了一个函数。
package com.example.vcloud_3_25;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.annotation.SuppressLint;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v4.widget.SwipeRefreshLayout.OnRefreshListener;
import android.util.Log;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import com.example.vcloud_3_25.bean.FileBean;
import com.example.vcloud_3_25.utils.FileHelper;
public class HomeFragment extends Fragment {
private ListView mListView = null;
private FileHelper mFileHelper = null;
// Map,每一个int型id对应一个FileBean列表。用于展现文件夹包含关系。
private SparseArray<List<FileBean>> mResMap = null;
private List<Map<String, Object>> mResList;
private int recentPid;
private View view;
private SwipeRefreshLayout refresh;
private List<Integer> mDirList;
private List<String> mDirStrList;
private SimpleAdapter adapter;
private String mDirString;
private TextView mDirTextView;
private ImageButton mBackButton;
private ImageButton mAddButton;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
initView(inflater, container);
initData();
return view;
}
private void initData() {
mFileHelper = new FileHelper();
mResMap = mFileHelper.getResMap();
recentPid = FileBean.ROOT;
mResList = getResList(recentPid);
mDirList = new ArrayList<Integer>();
mDirStrList = new ArrayList<String>();
mDirList.add(FileBean.ROOT);
mDirStrList.add("root");
mDirString = "root/";
mBackButton.setVisibility(view.INVISIBLE);
adapter = new SimpleAdapter(this.getActivity(), mResList,
R.layout.file_item, new String[] { "fileName", "fileDate",
"fileImag", "fileCheckBox" }, new int[] {
R.id.item_name, R.id.item_date, R.id.file_imag,
R.id.file_checkbox });
mListView.setAdapter(adapter);
mListView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View item,
int position, long id) {
ListView lv = (ListView) parent;
@SuppressWarnings("unchecked")
HashMap<String, Object> mMap = (HashMap<String, Object>) lv
.getItemAtPosition(position);
String type = (String) mMap.get("fileType");
// 如果点击的是文件夹,则打开该文件夹,并且修改当前目录,修改目录列表;否则不作反应。
if (type.equals("folder")) {
recentPid = (Integer) mMap.get("fileId");
mDirList.add(recentPid);
mDirStrList.add((String) mMap.get("fileName"));
setDirString();
refreshListView();
}
mBackButton.setVisibility(view.VISIBLE);
}
});
refresh.setOnRefreshListener(new OnRefreshListener() {
@Override
public void onRefresh() {
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
refreshListView();
refresh.setRefreshing(false);
}
}, 1000);
}
});
mBackButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
mDirStrList.remove(mDirStrList.size()-1);
mDirList.remove(mDirList.size()-1);
recentPid = mDirList.get(mDirList.size()-1);
setDirString();
refreshListView();
if(mDirList.size()==1)
mBackButton.setVisibility(view.INVISIBLE);
}
});
}
private void initView(LayoutInflater inflater, ViewGroup container) {
view = inflater.inflate(R.layout.main_tab_1, container, false);
mListView = (ListView) view.findViewById(R.id.folder_content_list);
refresh = (SwipeRefreshLayout) view
.findViewById(R.id.id_refresh_layout);
mDirTextView = (TextView) view.findViewById(R.id.folder_name);
mBackButton = (ImageButton) view.findViewById(R.id.id_back_button);
mAddButton = (ImageButton) view.findViewById(R.id.upload_imag);
}
// 该List用于和ListView绑定
@SuppressLint("SimpleDateFormat")
private List<Map<String, Object>> getResList(int id) {
List<Map<String, Object>> mMaps = new ArrayList<Map<String, Object>>();
Map<String, Object> map;
for (FileBean f : mFileHelper.getChildren(id)) {
SimpleDateFormat formatter = new SimpleDateFormat(
"yyyy-MM-dd HH:mm:ss");
map = new HashMap<String, Object>();
map.put("fileName", f.getName());
map.put("fileDate", formatter.format(new Date()));
map.put("fileImag", getFileImag(f.getType()));
map.put("fileId", f.getId());
map.put("fileCheckBox", R.id.file_checkbox);
map.put("fileType", f.getType());
mMaps.add(map);
Log.d("map.put", f.getName());
}
return mMaps;
}
// 根据String类型的文件类型找出对应的图片ID
private int getFileImag(String type) {
if (type.equals("avi"))
return R.drawable.avi;
else if (type.equals("doc"))
return R.drawable.doc;
else if (type.equals("exe"))
return R.drawable.exe;
else if (type.equals("fla"))
return R.drawable.fla;
else if (type.equals("folder"))
return R.drawable.folder;
else if (type.equals("html"))
return R.drawable.html;
else if (type.equals("mp3"))
return R.drawable.mp3;
else if (type.equals("mp4"))
return R.drawable.mp4;
else if (type.equals("pdf"))
return R.drawable.pdf;
else if (type.equals("png"))
return R.drawable.png;
else if (type.equals("ppt"))
return R.drawable.ppt;
else if (type.equals("xml"))
return R.drawable.xml;
else if (type.equals("zip"))
return R.drawable.zip;
return R.drawable.other;
}
private void setDirString(){
String str = "";
for(String s:mDirStrList){
str = str+s+"/";
}
mDirTextView.setText(str);
}
private void refreshListView(){
mResList.clear();
mResList.addAll(getResList(recentPid));
adapter.notifyDataSetChanged();
}
}