<!--StartFragment-->
我們可以在一個(gè)普通的 ListView中列出指定目錄下的所有文件,每個(gè)文件列出該文件的文件名和文件圖標(biāo),在每個(gè)文件名前面有一個(gè)checkbox按鈕,用戶可對該文件進(jìn)行選擇(支持多選),并實(shí)現(xiàn)某些操作(如打開、刪除功能):
<!--StartFragment-->
實(shí)現(xiàn)步驟如下。
1、新建類FileInfo
package ydtf.listview.filebrowser;
public class FileInfo {
public String path ; // 文件路徑
public String fileName ; // 文件名
public String type ; // 文件類型
public boolean checked ; // 是否選中
public int imageId ; // 圖片資源 id
public FileInfo(String path,String fileName,String type, boolean checked, int imageId) {
this . path = path;
this . fileName = fileName;
this . type =type;
this . checked = checked;
this . imageId =imageId;
}
}
這是一個(gè) pojo類,除了我們定制的構(gòu)造器外。它只用于表示ListView中列出的一個(gè)文件對象,為簡便起見沒有使用getter/setter方法。
2、新建一個(gè)layout文件file_info.xml :
< LinearLayout xmlns:android = "http://schemas.android.com/apk/res/android"
android:layout_width = "fill_parent" android:layout_height = "wrap_content"
android:orientation = "horizontal" android:minHeight = "40px"
android:layout_gravity = "center_vertical" >
< CheckBox android:id = "@+file_info/chk_check" android:focusable = "false"
android:layout_width = "wrap_content" android:layout_height = "wrap_content"
android:layout_marginLeft = "35px" android:checked = "false" />
< ImageView android:id = "@+file_info/iv_icon" android:layout_width = "wrap_content"
android:layout_height = "wrap_content" android:layout_gravity = "center_vertical" />
< TextView android:id = "@+file_info/tv_filename" android:layout_width = "wrap_content"
android:layout_height = "wrap_content" android:textColor = "?android:attr/textColorPrimary"
android:paddingLeft = "3px" android:layout_gravity = "center_vertical" />
</ LinearLayout >
僅包含了 3個(gè)widgets,分別用于顯示FileInfo對象的部分屬性,作為概念演示程序,向用戶展示文件的3個(gè)屬性就足夠了。
3、實(shí)現(xiàn)adapter類FileInfoAdaper:
package ydtf.listview.filebrowser;
import java.util.List;
import com.ydtf.android.R;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;
public class FileInfoAdapter extends BaseAdapter{
private LayoutInflater layoutInflater ; // 用于從 xml 文件加載 listview 的 layout
private Context ctx ; // 容器/ activity
private List<FileInfo> fileInfoList ; // 所有 item
public FileInfoAdapter(Context ctx,List<FileInfo> list){
this . ctx =ctx;
fileInfoList =list;
layoutInflater =LayoutInflater. from (ctx);
}
@Override
public int getCount() {
return fileInfoList .size();
}
@Override
public Object getItem( int position) {
return fileInfoList .get(position);
}
@Override
public long getItemId( int position) {
return position;
}
@Override
public View getView( int position, View convertView, ViewGroup parent) {
ViewHolder holder= null ; // 清空臨時(shí)變量
if (convertView == null ) { // 若行未初始化
convertView = layoutInflater .inflate(
R.layout. file_info , null ); // 通過 flater 初始化行視圖
holder = new ViewHolder(); // 并將行視圖的 3 個(gè)子視圖引用放到 tag 中
holder. itemIcon = (ImageView) convertView.findViewById(R.file_info. iv_icon );
holder. itemText = (TextView) convertView.findViewById(R.file_info. tv_filename );
holder. itemCheckBox = (CheckBox) convertView.findViewById(R.file_info. chk_check );
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag(); // 若行已初始化,直接從 tag 屬性獲得子視圖的引用
}
FileInfo info = fileInfoList .get(position); // 獲得行數(shù)據(jù)(模型)
if (info != null ) { // 根據(jù)模型數(shù)據(jù),設(shè)置行視圖的控件值
holder. itemText .setText(info. fileName );
Drawable draw = this . ctx .getResources().getDrawable(
info. imageId );
holder. itemIcon .setImageDrawable(draw);
holder. itemCheckBox .setChecked(info. checked );
}
return convertView;
}
static class ViewHolder{
CheckBox itemCheckBox ;
TextView itemText ;
ImageView itemIcon ;
}
}
在 android中,采用了所謂適配器的概念。這有點(diǎn)像mvc。一個(gè)組件,需要由mvc三部分構(gòu)成才能正常工作,在這里,m是FileInfo-提供了要展示的(一行)數(shù)據(jù),v是file_info.xml-提供了要顯示的(一行)布局,c則是適配器(整個(gè)ListView,多行),把數(shù)據(jù)m通過一定的邏輯進(jìn)行轉(zhuǎn)換并有選擇地提供給視圖v展示。
BaseAdapter是一個(gè)抽象類,我們需要繼承它,實(shí)現(xiàn)它的4個(gè)方法,以便向ListView提供數(shù)據(jù)。
當(dāng)然,最關(guān)鍵的方法是 getView方法。我們需要在其中構(gòu)建ListView的一行View,初始化這個(gè)View中需要展示的所有數(shù)據(jù),并返回這個(gè)View。
4、接下來,在一個(gè)Activity中應(yīng)用上面所有的3個(gè)東西:m、v和c。
這需要做兩件事,一個(gè)xml,一個(gè)Activity。
首先做第 1件事,新建一個(gè)布局文件downloadfilelist.xml,你可以用DroidDraw來干這個(gè)或者手工編寫 :
< LinearLayout xmlns:android = "http://schemas.android.com/apk/res/android"
android:layout_width = "fill_parent" android:layout_height = "fill_parent"
android:orientation = "vertical" >
<!-- 頂部工具欄 對齊頂部 -->
< RelativeLayout
xmlns:Android = "http://schemas.android.com/apk/res/android"
Android:layout_width = "fill_parent"
Android:layout_height = "wrap_content"
Android:layout_alignParentTop = "true" >
< include layout = "@+layout/filebrowser_top" />
</ RelativeLayout >
<!-- 下部的列表 對齊到工具欄下方 -->
< ListView
android:id = "@+file_browser/lvFileList"
android:layout_width = "fill_parent"
android:layout_height = "fill_parent"
/>
</ LinearLayout >
這個(gè) layout里,我們又引用了另外一個(gè)布局文件filebrowser_top.xml。這是一個(gè)工具欄,上邊放了兩個(gè)按鈕:
< RelativeLayout
xmlns:Android = "http://schemas.android.com/apk/res/android"
Android:background = "@drawable/top"
Android:layout_width = "fill_parent"
Android:layout_height = "wrap_content"
>< ImageView
Android:id = "@+file_browser/imgCheck"
Android:layout_toLeftOf = "@+file_browser/imgClose"
Android:layout_width = "wrap_content"
Android:layout_height = "wrap_content"
Android:layout_marginRight = "10px"
Android:src = "@drawable/checked"
Android:layout_centerVertical = "true"
>
</ ImageView >
< ImageView
Android:id = "@+file_browser/imgClose"
Android:layout_alignParentRight = "true"
Android:layout_width = "wrap_content"
Android:layout_height = "wrap_content"
Android:layout_marginRight = "10px"
Android:src = "@drawable/close"
Android:layout_centerVertical = "true"
>
</ ImageView >
</ RelativeLayout >
接下來,我們需要做第 2件事情,新建一個(gè)Activity,在其中放上我們的各個(gè)布局:
public class ScanDownloadFile extends Activity{
private static final String tag = "ScanDownloadFile" ;
private ArrayList<FileInfo> listItem ;
private String dir =Environment. getExternalStorageDirectory ()+ "/downloads/" ;
@Override
public void onCreate(Bundle savedInstanceState) {
super .onCreate(savedInstanceState);
// 獲取配置文件 , 檢查用戶是否已登錄
showView();
}
private void showView() {
setContentView(R.layout. downloadfilelist );
// 獲取 intent 的參數(shù)
Bundle bundle = getIntent().getExtras();
String itemtitle = bundle.getString( "data" );
setTitle(itemtitle);
// 構(gòu)造 ListView
ListView listview = (ListView) findViewById(R.file_browser. lvFileList );
// 生成動態(tài)數(shù)組,加入數(shù)據(jù)
listItem = getFiles();
// 構(gòu)造適配器 , 指明數(shù)據(jù)源字段與 listitem 中子控件的對應(yīng)關(guān)系
FileInfoAdapter adapter = new FileInfoAdapter( this , listItem );
// 添加適配器 , 進(jìn)行綁定
listview.setAdapter(adapter);
// 添加監(jiān)聽器,處理 item 點(diǎn)擊事件
listview.setOnItemClickListener( new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
Intent intent = new Intent(Intent. ACTION_GET_CONTENT );
intent.setAction(android.content.Intent. ACTION_VIEW );
FileInfo info= listItem .get(arg2);
File file = new File( dir +info. fileName );
String type=typeof(info. fileName );
if (type!= null ){
intent.setDataAndType(Uri. fromFile (file), info. type );
Log. d ( tag , "uri:" +Uri. fromFile (file)+ ",type:" +info. type );
startActivity(Intent. createChooser (intent, " 選擇一個(gè)應(yīng)用程序去打開它 " ));
} else
Toast. makeText (QxtScanDownloadFile. this , " 不能識別的文件類型 " , Toast. LENGTH_SHORT );
}
});
}
private String typeof(String file){
String[] arr;
// 文本文件
arr= this .getResources().getStringArray(
R.array. text );
for (String each:arr){
if (file.toLowerCase().endsWith(each)){
Log. i ( tag , "each:" +each);
return "txt/*" ;
}
}
// 圖片文件
arr= this .getResources().getStringArray(
R.array. image );
for (String each:arr){
if (file.toLowerCase().endsWith(each)){
return "image/*" ;
}
}
// 視頻文件
arr= this .getResources().getStringArray(
R.array. video );
for (String each:arr){
if (file.toLowerCase().endsWith(each)){
return "video/*" ;
}
}
// 音頻文件
arr= this .getResources().getStringArray(
R.array. audio );
for (String each:arr){
if (file.toLowerCase().endsWith(each)){
return "audio/*" ;
}
}
//word 文件
arr= this .getResources().getStringArray(
R.array. word );
for (String each:arr){
if (file.toLowerCase().endsWith(each)){
return "application/msword" ;
}
}
return null ;
}
private int iconof(String type){
return R.drawable. icon ;
}
private ArrayList<FileInfo> getFiles(){
ArrayList<FileInfo> array= new ArrayList<FileInfo>();
File file= new File( dir );
File[] files=file.listFiles();
for ( int i=0;i<files. length ;i++){
if (files[i].isFile()){ // 過濾目錄,只顯示文件
String name=files[i].getName();
String type=typeof(name);
FileInfo info= new FileInfo( dir ,name,type, false ,iconof(type));
array.add(info);
}
}
return array;
}
}
其中,我們加載了布局,通過 getFiles方法讀取了sd卡某個(gè)目錄下的內(nèi)容,對ListView應(yīng)用了我們前面定義的適配器FileInfoAdapter,用typeof方法對每個(gè)文件的類型進(jìn)行了識別,并響應(yīng)了ListView的OnItemClick事件,選擇合適的程序打開每個(gè)文件。
程序運(yùn)行的效果如下:
<!--StartFragment-->
5、現(xiàn)在,程序還沒有響應(yīng)任何特殊的事情。需要做一件事情:當(dāng)checkbox被改變時(shí),我們需要同時(shí)改變對應(yīng)的FileInfo對象的checked屬性。這需要修改適配器的getView方法,在以下代碼處進(jìn)行如下修改:
if (info != null ) { // 根據(jù)模型數(shù)據(jù),設(shè)置行視圖的控件值
??(省略部分代碼)
// 在 checkbox 中保存一個(gè) FileInfo 對象的引用
holder. itemCheckBox .setTag(info);
// 為 checkbox 增加 onClick 事件的處理
holder. itemCheckBox .setOnClickListener( new OnClickListener(){
@Override
public void onClick(View v) {
// 從 tag 中取出 FileInfo 的引用 FileInfo fi=(FileInfo)v.getTag();
// 設(shè)置 FileInfo 的 checked 屬性 fi. checked =((CheckBox)v).isChecked();
}
});
}
6、此外,我們還要為工具欄按鈕增加響應(yīng)的動作。打開ScanDownloadFile.java,在showView方法中增加代碼:
/ /check 按鈕的事件處理
btnChecked =(ImageView)findViewById(R.file_browser. imgCheck );
btnChecked .setOnTouchListener( new OnTouchListener(){
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()){
case MotionEvent. ACTION_DOWN :
// Log.i(getClass()+"","down");
btnChecked .setBackgroundDrawable(getResources().getDrawable(R.drawable. sendsms_bk ));
break ;
case MotionEvent. ACTION_UP :
// Log.i(getClass()+"","up");
btnChecked .setBackgroundDrawable(getResources().getDrawable(R.drawable. sendsms_bk_clear ));
showDialog(0);
break ;
}
return true ; // 必須返回 true ,否則 down 事件后不會觸發(fā)給 up 和 move 事件
}
});
因?yàn)? check按鈕實(shí)際上是一個(gè)imageView,我們只有通過在觸摸事件中切換兩張不同的圖片來模擬按鈕被按下、彈起的動作。同時(shí)我們在彈起事件中通過showDialog()觸發(fā)一個(gè)彈出式對話框:
// 創(chuàng)建 activity 托管對話框
protected Dialog onCreateDialog( int id) {
return new AlertDialog.Builder( this ).setMessage( " 確定要刪除所選文件嗎?文件刪除后將不能被恢復(fù)! " )
.setPositiveButton( " 是 " ,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
deleteFiles();
}
}).setNegativeButton( " 不 " , new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dismissDialog(0); //removeDialog(0); 移除對話框
}
}
).create();
}
當(dāng)用戶點(diǎn)擊“是”,才觸發(fā)真正的刪除文件方法 deleteFiles():
private void deleteFiles(){
for (FileInfo each: listItem ){
// 遍歷 FileInfo 對象的 checked 屬性
if (each. checked ){
Log. i ( tag , ”刪除文件:”+ each. fileName );
}
}
}
現(xiàn)在從文件列表中選擇一些文件,點(diǎn)擊
按鈕,后臺將打印出所選擇的文件列表(模擬刪除文件操作)。
<!--EndFragment-->
<!--EndFragment-->
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯(lián)系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長非常感激您!手機(jī)微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
