Android 成长笔记

Android SD 卡文件浏览器

2017-04-20  本文已影响26人  赵者也

本文参考文献:《疯狂Android讲义 : 第2版

主布局文件:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/container"
    android:background="@color/colorGray"
    >

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/path"
        android:gravity="center_horizontal"
        android:layout_alignParentTop="true"
        />

    <ListView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/list"
        android:divider="#000"
        android:dividerHeight="1px"
        android:layout_below="@+id/path"
        />

    <Button
        android:id="@+id/parent"
        android:layout_width="38dp"
        android:layout_height="34dp"
        android:background="@drawable/back"
        android:layout_centerHorizontal="true"
        android:layout_alignParentBottom="true"
        />

</RelativeLayout>

列表项布局文件 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="match_parent"
    android:layout_height="match_parent">

    <ImageView
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:id="@+id/icon"
        />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/file_name"
        android:layout_marginStart="20dp"
        />

</LinearLayout>

主程序文件的内容如下:

package com.toby.personal.testlistview;

import android.os.Bundle;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
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 {

    final private static String TAG = "Toby_Test";

    ListView listView;
    TextView textView;
    File currentParent;
    File[] currentFiles;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        listView = (ListView) findViewById(R.id.list);
        textView = (TextView) findViewById(R.id.path);

        Log.d(TAG, Environment.getExternalStorageDirectory().getPath());
        File root = new File("/mnt/");

        if (root.exists()) {
            currentParent = root;
            currentFiles = root.listFiles();
            inflateListView(currentFiles);
        }

        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(MainActivity.this, "Current path is null.",
                            Toast.LENGTH_SHORT).show();
                } else {
                    currentParent = currentFiles[position];
                    currentFiles = tmp;
                    inflateListView(currentFiles);
                }
            }
        });

        Button parent = (Button) findViewById(R.id.parent);
        parent.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                try {
                    if (!currentParent.getCanonicalPath().equals("/mnt/")) {
                        currentParent = currentParent.getParentFile();
                        currentFiles = currentParent.listFiles();
                        inflateListView(currentFiles);
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        });
    }

    private void inflateListView(File[] files) {

        if (files == null || files.length <= 0) {
            return;
        }

        List<Map<String, Object>> listItems = new ArrayList<>();

        for (File file : files) {
            Map<String, Object> listItem = new HashMap<>();
            listItem.put("icon", file.isDirectory() ? R.drawable.folder : R.drawable.file);
            listItem.put("fileName", file.getName());
            listItems.add(listItem);
        }

        SimpleAdapter simpleAdapter = new SimpleAdapter(this, listItems, R.layout.line,
                new String[]{"icon", "fileName"}, new int[]{R.id.icon, R.id.file_name});

        listView.setAdapter(simpleAdapter);

        try {
            textView.setText(String.format("Current Path: %s", currentParent.getCanonicalPath()));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}

运行效果:

显示效果
上一篇 下一篇

猜你喜欢

热点阅读