2011年8月28日 星期日

Android - FileDialog

Android沒有提供預設的檔案選取 只好自己寫了...(淚
列表顯示也要自己弄

先來看個成果
半透明霧化背景、返回鍵移至上層








先講解ListView客製化:
class FileDialog 內有2個inner class :
Cell 、 CellArrayAdapter
Cell 單純的儲存要顯示的 text跟 icon的 resource id

CellArrayAdapter 要弄個List<Cell>儲存資料

然後要@override  getView() 讓接上他的ListView可以取得每格的View

LayoutInflater inflater = (LayoutInflater) mContext
		.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(mViewResourceId, parent, false);

↑getView()內將listview_item.xml轉成view

Cell cell = getItem(position);
mIcon = (ImageView) view.findViewById(R.id.listViewIcon);
mText = (TextView) view.findViewById(R.id.listViewText);
mText.setText(cell.getText());
mIcon.setImageResource(cell.getIconId());
return view;

↑設定圖片 文字資訊 回傳出去
剩下的要補上一些add() clear()之類的方便增加刪除 有用到的自己加


再來是FileDialog
核心是updateList()
將current path 下所有檔案 列表顯示在 ListView 上面

File[] files = new File(mCurrentPath).listFiles();

↑取得檔案列表

for (File currentFile : files) {
	if (currentFile.isDirectory())
		iconId = R.drawable.folder;
	else
		iconId = R.drawable.file;
	mAdapter.add(new Cell(currentFile.getName(), iconId));
}

↑取出每個檔案 設定圖示 文字加至Adapter內

使用方式:
模仿成.net的openFileDialog
final FileDialog f = new FileDialog(this);
f.setOnFileOkListener(new OnFileOkListener() {
	@Override
	public void onFileDialog_FileOk() {
		//f.FileName 取得所選的完整路徑+檔名
	}
});
f.show();


原始碼

FileDialog.java
public class FileDialog extends Dialog {
	private Context mContext;
	private ListView mListView;
	private TextView mTxtPath;
	private String mCurrentPath;
	private CellArrayAdapter mAdapter;
	private OnFileOkListener mOnFileOkListener;

	public String FileName = null;

	public FileDialog(Context context) {
		super(context, android.R.style.Theme_Translucent_NoTitleBar);
		mContext = context;
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND,
				WindowManager.LayoutParams.FLAG_BLUR_BEHIND);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		super.onCreate(savedInstanceState);
		setContentView(R.layout.filedialog);
		mListView = (ListView) findViewById(R.id.lstDir_fileDialog);
		mTxtPath = (TextView) findViewById(R.id.txtPath_fileDialog);
		mAdapter = new CellArrayAdapter(mContext, R.layout.listview_item);
		mCurrentPath = Environment.getExternalStorageDirectory().toString();
		mListView.setOnItemClickListener(mItemClickListener);
		mListView.setAdapter(mAdapter);
		updateList();
	}

	@Override
	public boolean onKeyUp(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK)
			if (!mCurrentPath.equals(Environment.getExternalStorageDirectory()
					.toString())) {
				int index = mCurrentPath.lastIndexOf("/");
				if (index != -1)
					mCurrentPath = mCurrentPath.substring(0, index);
				updateList();
				return true;
			}
		return super.onKeyUp(keyCode, event);
	}

	private OnItemClickListener mItemClickListener = new OnItemClickListener() {
		@Override
		public void onItemClick(AdapterView<?> parent, View view, int position,
				long id) {
			String filename = ((TextView) view.findViewById(R.id.listViewText))
					.getText().toString();
			File file = new File(mCurrentPath + "//" + filename);
			if (file.isDirectory()) {
				mCurrentPath += "/" + filename;
				updateList();
			} else {
				FileName = mCurrentPath + "/" + filename;
				mOnFileOkListener.onFileDialog_FileOk();
				FileDialog.this.dismiss();
			}
		}

	};

	public void setOnFileOkListener(OnFileOkListener l) {
		mOnFileOkListener = l;
	}

	private void updateList() {
		mAdapter.clear();
		File[] files = new File(mCurrentPath).listFiles();
		int iconId;
		for (File currentFile : files) {
			if (currentFile.isDirectory())
				iconId = R.drawable.folder;
			else
				iconId = R.drawable.file;
			mAdapter.add(new Cell(currentFile.getName(), iconId));
		}
		mTxtPath.setText(mCurrentPath);
	}
	public class Cell {
		private String mText;
		private int mIconId;

		public Cell(String text, int iconId) {
			mText = text;
			mIconId = iconId;
		}

		public String getText() {
			return mText;
		}

		public int getIconId() {
			return mIconId;
		}
	}
	public class CellArrayAdapter extends ArrayAdapter<Cell> {
		private ImageView mIcon;
		private TextView mText;
		private Context mContext;
		private List<Cell> mData = new ArrayList<Cell>();
		private int mViewResourceId;

		public CellArrayAdapter(Context context, int textViewResourceId) {
			super(context, textViewResourceId);
			mContext = context;
			mViewResourceId = textViewResourceId;
		}

		@Override
		public void add(Cell object) {
			super.add(object);
			mData.add(object);
		}

		@Override
		public void clear() {
			super.clear();
			mData.clear();
		}

		@Override
		public int getCount() {
			return mData.size();
		}

		@Override
		public Cell getItem(int position) {
			return mData.get(position);
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			View view = convertView;
			if (view == null) {
				LayoutInflater inflater = (LayoutInflater) mContext
						.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
				view = inflater.inflate(mViewResourceId, parent, false);
			}
			Cell cell = getItem(position);
			mIcon = (ImageView) view.findViewById(R.id.listViewIcon);
			mText = (TextView) view.findViewById(R.id.listViewText);
			mText.setText(cell.getText());
			mIcon.setImageResource(cell.getIconId());
			return view;
		}

	}

}

OnFileOkListener.java
public interface OnFileOkListener {
	void onFileDialog_FileOk();
}


filedialog.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_height="fill_parent"
  android:orientation="vertical"
  android:layout_width="fill_parent"
  android:background="#7000">
  <TextView
    android:text="TextView"
    android:layout_height="wrap_content"
    android:id="@+id/txtPath_fileDialog"
    android:textColor="#fff"
    android:textSize="20sp"
    android:layout_margin="10dp"
    android:layout_width="fill_parent"
    android:background="#000"></TextView>
  <ListView
    android:id="@+id/lstDir_fileDialog"
    android:layout_margin="10dp"
    android:layout_height="fill_parent"
    android:layout_width="fill_parent"></ListView>
</LinearLayout>

listview_item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:orientation="horizontal"
  android:layout_height="wrap_content">
  <ImageView
    android:layout_gravity="left"
    android:src="@drawable/icon"
    android:id="@+id/listViewIcon"
    android:layout_width="64dp"
    android:layout_margin="5dp"
    android:layout_height="50dp"></ImageView>
  <TextView
    android:text="TextView"
    android:layout_gravity="center"
    android:id="@+id/listViewText"
    android:textColor="#fff"
    android:textSize="20sp"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_margin="5dp"></TextView>
</LinearLayout>

res/drawable/
file.png
folder.png



以上兩個icon版權歸http://icons.mysitemyway.com/所有


參考至
http://w2davids.wordpress.com/android-listview-with-iconsimages-and-sharks-with-lasers/
http://www.cnblogs.com/fly_binbin/archive/2011/01/10/1932421.html

沒有留言:

張貼留言