本文内容摘自《疯狂Android讲义 第3版》李刚 著
创建SimpleAdapter时指定的layout布局文件 line.xml

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="horizontal"    android:layout_width="fill_parent"    android:layout_height="fill_parent">    <ImageView        android:id="@+id/icon"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:paddingLeft="10dp"/>    <TextView        android:id="@+id/file_name"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:textSize="16pt"        android:paddingLeft="10dp"        android:paddingTop="10dp"        android:paddingBottom="10dp"/>LinearLayout>

Layout布局文件:

<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:app="http://schemas.android.com/apk/res-auto"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    tools:context="shortcut.song.com.myapplication.SDFileExplorerActivity">    <TextView        android:id="@+id/tv_file_path"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_centerHorizontal="true"        android:layout_alignParentTop="true"/>    <ListView        android:id="@+id/filelist"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:divider="#0000"        android:dividerHeight="1px"        android:layout_below="@id/tv_file_path"/>    <Button        android:id="@+id/btn_home"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:background="@drawable/home"        android:layout_centerHorizontal="true"        android:layout_alignParentBottom="true"/>RelativeLayout>
package shortcut.song.com.myapplication;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.support.v7.widget.RecyclerView;import android.view.View;import android.widget.AdapterView;import android.widget.Button;import android.widget.ListView;import android.widget.SimpleAdapter;import android.widget.TextView;import android.widget.Toast;import org.w3c.dom.Text;import java.io.File;import java.io.IOException;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;import java.util.Objects;public class SDFileExplorerActivity extends AppCompatActivity {    ListView listView;    TextView textView;    //记录当前父文件夹    File currentParent;    //记录当前路经下的所有文件    File[] currentFiles;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_sdfile_explorer);        listView = (ListView)findViewById(R.id.filelist);        textView = (TextView)findViewById(R.id.tv_file_path);        //获取系统SD卡目录        File root = new File("/mnt/sdcard");        if(root.exists()){            currentParent = root;            currentFiles = root.listFiles();            //使用当前目录下的全部文件,来填充ListView            inflateListView(currentFiles);        }        //为ListView的列表项单击事件绑定监听器        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {            @Override            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {                //用户单击了文件,直接返回                if (currentFiles[position].isFile()) return;;                //获取单击文件夹下的所有文件                File[] tmp = currentFiles[position].listFiles();                if (tmp == null || tmp.length == 0) {                    Toast.makeText(SDFileExplorerActivity.this, "Not File" ,Toast.LENGTH_LONG).show();                } else {                    //获取用户单击的列表项对应的文件夹,设为当前父文件夹                    currentParent = currentFiles[position];                    //保存当前父文件夹内的所有文件和文件夹                    currentFiles = tmp;                    //再次更新ListView                    inflateListView(currentFiles);                }            }        });        Button  parent = (Button)findViewById(R.id.btn_home);        parent.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                try {                    if (!currentParent.getCanonicalPath().equals("/mnt/sdcard")) {                        //获取上一目录                        currentParent = currentParent.getParentFile();                        //列出当前目录下的所有文件                        currentFiles = currentParent.listFiles();                        //再次更新ListView                        inflateListView(currentFiles);                    }                } catch (IOException e) {                    e.printStackTrace();                }            }        });    }    private void inflateListView(File[] files) {        //创建List集合,元素是Map        List> listItems = new ArrayList>();        for (int i = 0; i < files.length; i++ ) {            Map listItem = new HashMap();            //如果当前File是文件夹,使用文件夹图标,其它使用文件图标            if (files[i].isDirectory()) {                listItem.put("icon", R.drawable.folder);            } else {                listItem.put("icon", R.drawable.file);            }            listItem.put("file", files[i].getName());            //添加List项            listItems.add(listItem);        }        //创建        SimpleAdapter simpleAdapter = new SimpleAdapter(this            ,listItems ,R.layout.line            ,new String[]{"icon", "file"}            ,new int[] {R.id.icon, R.id.file_name});        //为ListView设置Adapter        listView.setAdapter(simpleAdapter);        try {            textView.setText("CurrentPath:" + currentParent.getCanonicalPath());        } catch (IOException e) {            e.printStackTrace();        }    }}

运行效果:

Andorid 文件浏览器简易实现_第1张图片

更多相关文章

  1. Android的读写文件及权限设置
  2. Android /system/build.prop 文件
  3. android中选择文件,部分手机找不到文件路径问题的解决
  4. android 获取配置文件 相对路径
  5. Android关于读取临时文件
  6. Android下载并打开pdf文件
  7. Android HTTP GET 小文件下载
  8. Android中播放mp3文件

随机推荐

  1. Android(安卓)自动编译、打包生成apk文件
  2. BitRaser for Mobile iOS和Android移动设
  3. Android智能硬件开发心得总结(一)
  4. 详解Android广播机制
  5. Android基础入门知识
  6. android 资源文件学习
  7. 丢失Android系统库或者Conversion to Dal
  8. Android(安卓)Bmob后端云—数据库、服务
  9. 刚进入Android终端即可使用busybox的命令
  10. 当我们按下电源键,Android(安卓)究竟做了