- 1. import java.io.File;
-
2. import java.util.*;
-
3.
-
4. import android.app.Activity;
-
5. import android.content.Context;
-
6. import android.os.*;
-
7. import android.view.*;
-
8. import android.widget.*;
-
9. import android.widget.AdapterView.OnItemClickListener;
-
10. import android.widget.ImageView.ScaleType;
-
11.
-
12. public class FileBrowser extends Activity {
-
13.
-
14. private ListView mainListView=null;
-
15. private List<Map<String,Object>> list=null;
-
16.
-
17. public void onCreate(Bundle savedInstanceState) {
-
18. super.onCreate(savedInstanceState);
-
19. this.setTitle("文件浏览器");
-
20. mainListView=new ListView(this);
-
21. setContentView(mainListView);
-
22.
-
23. File file=Environment.getRootDirectory();
-
24. String pathx=file.getAbsolutePath();
-
25. this.setTitle(pathx);
-
26. //android的总目录就是"/"
-
27. list_init("/");
-
28. }
-
29.
-
30. void list_init(String path){
-
31. File file=new File(path);
-
32. File[] fileList=file.listFiles();
-
33. list=new ArrayList<Map<String,Object>>();
-
34. Map<String,Object> item;
-
35. item=new HashMap<String,Object>();
-
36. if(path.equals("/")){
-
37. item.put("ico",R.drawable.home);
-
38. item.put("name","总目录列表");
-
39. item.put("path","/");
-
40. list.add(item);
-
41. }else{
-
42. item.put("ico",R.drawable.back);
-
43. item.put("name","返回上一级");
-
44. item.put("path",file.getParent());
-
45. list.add(item);
-
46. }
-
47. for(int i=0;i<fileList.length;i++){
-
48. item=new HashMap<String,Object>();
-
49. if(fileList[i].isDirectory()){
-
50. if(fileList[i].list().length<1){
-
51. item.put("ico",R.drawable.file1);
-
52. }else{
-
53. item.put("ico",R.drawable.file2);
-
54. }
-
55. }else{
-
56. item.put("ico",R.drawable.content);
-
57. }
-
58. item.put("name",fileList[i].getName());
-
59. item.put("path",fileList[i].getAbsolutePath());
-
60. list.add(item);
-
61. }
-
62. MyAdapter ma=new MyAdapter(this,list);
-
63. //mainListView=new ListView(this);
-
64. mainListView.setAdapter(ma);
-
65. mainListView.setOnItemClickListener(new OnItemClickListener(){
-
66. public void onItemClick(AdapterView<?> arg0, View arg1,int arg2, long arg3) {
-
67. if(arg2>0 && (Integer)(list.get(arg2).get("ico"))==R.drawable.content){
-
68. //非文件夹图标,点击无效
-
69. }else{
-
70. //打开下一级文件目录列表
-
71. list_init((String)(list.get(arg2).get("path")));
-
72. }
-
73. }
-
74. });
-
75. this.setTitle(path);
-
76. }
-
77.
-
78. public class MyAdapter extends BaseAdapter{
-
79.
-
80. Context context=null;
-
81. List<Map<String,Object>> list=null;
-
82.
-
83. MyAdapter(Context context,List<Map<String,Object>> list){
-
84. this.context=context;
-
85. this.list=list;
-
86. }
-
87. public int getCount() {return list.size();}
-
88. public Object getItem(int position) {return position;}
-
89. public long getItemId(int position) {return position;}
-
90.
-
91. public View getView(int position, View convertView, ViewGroup parent) {
-
92. LinearLayout returnView=new LinearLayout(context);
-
93. returnView.setLayoutParams(new ListView.LayoutParams(-1,-2));//注意:ListView.LayoutParams
-
94. //图标
-
95. ImageView iv=new ImageView(context);
-
96. LinearLayout.LayoutParams lp_iv=new LinearLayout.LayoutParams(-2,-2);
-
97. lp_iv.rightMargin=10;
-
98. iv.setLayoutParams(lp_iv);
-
99. iv.setScaleType(ScaleType.CENTER_INSIDE);
-
100. iv.setImageResource((Integer)((list.get(position)).get("ico")));
-
101. returnView.addView(iv);
-
102. //文件名
-
103. TextView name=new TextView(context);
-
104. LinearLayout.LayoutParams lp_tv=new LinearLayout.LayoutParams(-2,-2);
-
105. name.setLayoutParams(lp_tv);
-
106. name.setTextSize(name.getTextSize()+10);
-
107. name.setText((String)(list.get(position).get("name")));
-
108. returnView.addView(name);
-
109. //
-
110. return returnView;
-
111. }
-
112.
-
113. }
-
114. }
阅读(3530) | 评论(0) | 转发(0) |