實例:SD卡瀏覽器

SDFileExplorer

item_file.xml(列表項佈局文件)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    android:minHeight="40dp">

    <ImageView
        android:id="@+id/ivItemFile"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <TextView
        android:id="@+id/tvItemFile"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />


</LinearLayout>

activity_main.xml(主界面)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity"
    android:weightSum="1">

    <TextView
        android:id="@+id/tvPath"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <ListView
        android:id="@+id/lvFiles"
        android:layout_width="match_parent"
        android:layout_height="245dp"
        android:divider="#000"
        android:dividerHeight="1px"
        android:layout_weight="0.72" />

    <Button
        android:id="@+id/btnParent"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="返回上一級"
        android:layout_gravity="center_horizontal" />

</LinearLayout>

MainActivity

package com.jackie.sdfileexplore;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
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 java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MainActivity extends AppCompatActivity {
    ListView listView;
    TextView textView;
    File currParent;
    File[] currFiles;
    Button button;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        listView = (ListView) findViewById(R.id.lvFiles);
        textView = (TextView) findViewById(R.id.tvPath);
        button = (Button) findViewById(R.id.btnParent);
        File root = new File("/mnt/sdcard/");
        if (root.exists()) {
            currParent = root;
            currFiles = root.listFiles();
            //如果這裏報空指針錯誤,檢查權限是否配置
            Log.d("jackie", "  " + currFiles);
            inflateFiles(currFiles);
        }
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (currFiles[position].isFile()) return;
                File[] children = currFiles[position].listFiles();
                if (children == null || children.length == 0) {
                    Toast.makeText(MainActivity.this, "該目錄下沒有文件", Toast.LENGTH_SHORT).show();
                }
                currParent = currFiles[position];
                currFiles = children;
                inflateFiles(currFiles);
            }
        });
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                try {
                    if (!currParent.getCanonicalPath().equals("/mnt/sdcard/")) {
                        currParent = currParent.getParentFile();
                        currFiles = currParent.listFiles();
                        inflateFiles(currFiles);
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        });
    }

    private void inflateFiles(File[] files) {
        List<Map<String, Object>> listFiles = new ArrayList<Map<String, Object>>();
        for (int i = 0; i < files.length; i++) {
            Map<String, Object> itemFile = new HashMap<String, Object>();
            if (files[i].isDirectory()) {
                itemFile.put("icon", R.drawable.folder);
            } else {
                itemFile.put("icon", R.drawable.file);
            }
            itemFile.put("fileName", files[i].getName());
            listFiles.add(itemFile);
        }
        SimpleAdapter simpleAdapter = new SimpleAdapter(
                this,
                listFiles,
                R.layout.item_file,
                new String[]{"icon", "fileName"},
                new int[]{R.id.ivItemFile, R.id.tvItemFile});
        listView.setAdapter(simpleAdapter);
        try {
            textView.setText("當前路徑是" + currParent.getCanonicalPath());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}

使用的圖標:

文件夾圖標

文件圖標


關於SimpleAdapter的構造方法

參考源碼和本例代碼,

        SimpleAdapter simpleAdapter = new SimpleAdapter(
                this,//上下文對象
                listFiles,//列表資源,每一項都是一個map,map裏有一個圖片項(drawble)和文字項(String)
                R.layout.item_file,//列表項佈局文件,包含一個imageview和一個textview
                new String[]{"icon", "fileName"},//佈局文件中使用的資源文件,對應着key爲icon的value(drawble),key爲fileName的value(String)
                new int[]{R.id.ivItemFile, R.id.tvItemFile});//需要使用上述資源的列表項佈局的控件,順序與上面的key對應的資源一致,imageview對應drawble,textview對應String
/**
     * Constructor
     *
     * @param context The context where the View associated with this SimpleAdapter is running
     * @param data A List of Maps. Each entry in the List corresponds to one row in the list. The
     *        Maps contain the data for each row, and should include all the entries specified in
     *        "from"
     * @param resource Resource identifier of a view layout that defines the views for this list
     *        item. The layout file should include at least those named views defined in "to"
     * @param from A list of column names that will be added to the Map associated with each
     *        item.
     * @param to The views that should display column in the "from" parameter. These should all be
     *        TextViews. The first N views in this list are given the values of the first N columns
     *        in the from parameter.
     */
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章