生活随笔
收集整理的這篇文章主要介紹了
Android入门之文件系统操作(一)简单的文件浏览器
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
版權聲明:本文為博主原創文章,未經博主允許不得轉載。
?????
[java] view plaincopy
import?java.io.File;??import?java.util.*;????import?android.app.Activity;??import?android.content.Context;??import?android.os.*;??import?android.view.*;??import?android.widget.*;??import?android.widget.AdapterView.OnItemClickListener;??import?android.widget.ImageView.ScaleType;????public?class?FileBrowser?extends?Activity?{????????private?ListView?mainListView=null;??????private?List<Map<String,Object>>?list=null;????????????public?void?onCreate(Bundle?savedInstanceState)?{??????????super.onCreate(savedInstanceState);??????????this.setTitle("文件瀏覽器");??????????mainListView=new?ListView(this);??????????setContentView(mainListView);????????????????????File?file=Environment.getRootDirectory();??????????String?pathx=file.getAbsolutePath();??????????this.setTitle(pathx);??????????????????list_init("/");??????}????????????void?list_init(String?path){??????????File?file=new?File(path);??????????File[]?fileList=file.listFiles();??????????list=new?ArrayList<Map<String,Object>>();??????????Map<String,Object>?item;????????????item=new?HashMap<String,Object>();???????????if(path.equals("/")){??????????????item.put("ico",R.drawable.home);???????????????item.put("name","總目錄列表");????????????????item.put("path","/");????????????????list.add(item);??????????}else{??????????????item.put("ico",R.drawable.back);???????????????item.put("name","返回上一級");????????????????item.put("path",file.getParent());????????????????list.add(item);??????????}??????????for(int?i=0;i<fileList.length;i++){??????????????item=new?HashMap<String,Object>();???????????????if(fileList[i].isDirectory()){??????????????????if(fileList[i].list().length<1){??????????????????????item.put("ico",R.drawable.file1);??????????????????}else{??????????????????????item.put("ico",R.drawable.file2);??????????????????}??????????????}else{??????????????????item.put("ico",R.drawable.content);???????????????}??????????????item.put("name",fileList[i].getName());????????????????item.put("path",fileList[i].getAbsolutePath());????????????????list.add(item);????????????}??????????MyAdapter?ma=new?MyAdapter(this,list);???????????????????mainListView.setAdapter(ma);???????????mainListView.setOnItemClickListener(new?OnItemClickListener(){??????????????public?void?onItemClick(AdapterView<?>?arg0,?View?arg1,int?arg2,?long?arg3)?{??????????????????if(arg2>0?&&?(Integer)(list.get(arg2).get("ico"))==R.drawable.content){??????????????????????????????????????}else{??????????????????????????????????????????list_init((String)(list.get(arg2).get("path")));??????????????????}??????????????}??????????});??????????this.setTitle(path);??????}????????????public?class?MyAdapter?extends?BaseAdapter{????????????????????????Context?context=null;????????????List<Map<String,Object>>?list=null;????????????????????????MyAdapter(Context?context,List<Map<String,Object>>?list){????????????????this.context=context;????????????????this.list=list;??????????}????????????public?int?getCount()?{return?list.size();}????????????public?Object?getItem(int?position)?{return?position;}????????????public?long?getItemId(int?position)?{return?position;}????????????????????public?View?getView(int?position,?View?convertView,?ViewGroup?parent)?{????????????????LinearLayout?returnView=new?LinearLayout(context);????????????????returnView.setLayoutParams(new?ListView.LayoutParams(-1,-2));????????????????????????ImageView?iv=new?ImageView(context);????????????????LinearLayout.LayoutParams?lp_iv=new?LinearLayout.LayoutParams(-2,-2);??????????????????lp_iv.rightMargin=10;??????????????iv.setLayoutParams(lp_iv);???????????????iv.setScaleType(ScaleType.CENTER_INSIDE);????????????????iv.setImageResource((Integer)((list.get(position)).get("ico")));????????????????returnView.addView(iv);??????????????????????????TextView?name=new?TextView(context);????????????????LinearLayout.LayoutParams?lp_tv=new?LinearLayout.LayoutParams(-2,-2);???????????????name.setLayoutParams(lp_tv);????????????????name.setTextSize(name.getTextSize()+10);????????????????name.setText((String)(list.get(position).get("name")));??????????????returnView.addView(name);??????????????????????????return?returnView;????????????}????????????????}???}??
轉載于:https://www.cnblogs.com/Free-Thinker/p/7463959.html
總結
以上是生活随笔為你收集整理的Android入门之文件系统操作(一)简单的文件浏览器的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。