关闭

android 基础-自定义listView的实现 简单的文件浏览器

标签: androidlistview布局xml浏览器
519人阅读 评论(0) 收藏 举报
分类:
*文件浏览器 浏览SD卡文件 ListView 点击事件 更新适配器数据, 刷新ListView视图

* 判断根目录,连点两次返回键退出程序


public class MainActivity extends Activity {
private TextView mFilePath;
private ListView mFileList;
private File mRootFile;
private File[] listFiles;
MyAdapter adapter;
private long time;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mFilePath = (TextView) findViewById(R.id.address);
mFileList = (ListView) findViewById(R.id.list);
// 得到SD卡路径
mRootFile = Environment.getExternalStorageDirectory();
// 列出当前目录下的所有文件
// listFiles=new File[mRootFile.listFiles().length];
listFiles = mRootFile.listFiles();
setCurrPath();
adapter = new MyAdapter(MainActivity.this, listFiles);
mFileList.setAdapter(adapter);
mFileList.setOnItemClickListener(new OnItemClickListener() {

@Override
// 参数:arg0 —— arg1——当前的view position——点击条目
public void onItemClick(AdapterView<?> arg0, View arg1,
int position, long arg3) {
// TODO Auto-generated method stub
// item点击操作
// 进入下一级目录
File sFile = listFiles[position];
// 判断是不是目录
if (sFile.isDirectory()) {
// 判断是否是空文件夹
File[] files = sFile.listFiles();
if (null == files || files.length == 0) {
Toast.makeText(MainActivity.this, "这是空文件夹",
Toast.LENGTH_SHORT).show();
} else {
listFiles = files;
setCurrPath();
adapter.setData(listFiles);
}
}
}
});
}
public void setCurrPath(){
mFilePath.setText(listFiles[0].getParent());
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

@Override
public void onBackPressed() {
// TODO Auto-generated method stub
//super.onBackPressed();
File parentFile = listFiles[0].getParentFile().getParentFile();
if(parentFile.getPath().endsWith((mRootFile.getParent()))){
if(System.currentTimeMillis()-time<500){
// Toast.makeText(MainActivity.this, "再按一次退出程序", Toast.LENGTH_SHORT);
super.onBackPressed();
}
else{
time=System.currentTimeMillis();
Toast.makeText(MainActivity.this, "再按一次退出程序", Toast.LENGTH_SHORT).show();
}
}
else{
listFiles=parentFile.listFiles();
adapter.setData(listFiles);
setCurrPath();
}
}
}



public class MyAdapter extends BaseAdapter{
private Context context;
private File[]data;
private LayoutInflater inflater;
public MyAdapter(Context context,File[]data){
super();
this.context=context;
this.data=data;
inflater = LayoutInflater.from(context);
}
//重新设置adapter的数据
public void setData(File[]data){
this.data=data;
//刷新数据
notifyDataSetChanged();
}
@Override
public View getView(int position, View arg1, ViewGroup arg2) {
// TODO Auto-generated method stub
LinearLayout layout = (LinearLayout)inflater.inflate(R.layout.listitem, null);
ImageView icon=(ImageView)layout.findViewById(R.id.img);
TextView name=(TextView)layout.findViewById(R.id.name);
File file=data[position];
name.setText(file.getName());
if(file.isFile()){
icon.setImageResource(R.drawable.format_unkown);
if(file.getName().endsWith(".doc")){
icon.setImageResource(R.drawable.format_word);
}
}
else{
icon.setImageResource(R.drawable.format_folder);
}
return layout;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return data.length;
}

@Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return null;
}

@Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return 0;
}
}


activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/LinearLayout1"
    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" >


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


    <ListView
        android:id="@+id/list"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        />
</LinearLayout>


listitem.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >
    <ImageView 
        android:id="@+id/img"
        android:layout_width="55dp"
        android:layout_height="55dp"
        
        />
    <TextView
        android:id="@+id/name"
        android:layout_width="match_parent"
        android:layout_height="fill_parent"
        android:gravity="center_vertical"
        android:text="kfdjsalf"
    />
</LinearLayout>


0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:24984次
    • 积分:638
    • 等级:
    • 排名:千里之外
    • 原创:45篇
    • 转载:0篇
    • 译文:0篇
    • 评论:1条