本文内容摘自《疯狂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<Map<String, Object>> listItems = new ArrayList<Map<String, Object>>(); for (int i = 0; i < files.length; i++ ) { Map<String, Object> listItem = new HashMap<String, Object>(); //如果当前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(); } } }