您的位置:首页 > 移动开发 > Android开发

实例:SD卡浏览器

2015-11-16 17:33 561 查看

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,
be51
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.
*/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息