Android 文件瀏覽器總結

文件瀏覽器用來讀取android系統中的全部文件和文件夾。具體說明以下:
java

  1. 最上面顯示當前的文件路徑。若是是根目錄,則顯示「根目錄」;android

  2. 第二行是返回上一級按鈕。若是當前處於根目錄下,則該行不顯示;瀏覽器

  3. 文件顯示使用listView控件,全部文件一次性加載完畢;若當前是文件夾,則可點擊,進入下一級目錄,如果文件,則點擊,默認爲選中該文件。返回文件名,並關閉文件瀏覽器。ide

例:this

文件瀏覽器Activity
spa

public class FileManagerActivity extends Activity {
	private TextView mCurrentPath;
	private TextView mReturn;
	private ListView mList;
	private View mPathLine;
	private String mReturnPath = null;
	private FileManagerAdapter adapter;
	private ArrayList<Map<String, Object>> infos = null;

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

	private void initView() {
		mCurrentPath = (TextView) findViewById(R.id.file_path);
		mPathLine = findViewById(R.id.file_path_line);
		mReturn = (TextView) findViewById(R.id.file_return);
		mList = (ListView) findViewById(R.id.file_list);

		mList.setOnItemClickListener(clickListener);
		mReturn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				String returnStr = mReturn.getText().toString();
				if (mReturnPath.length() > 0 && returnStr.equals("返回上一級")) {
					initList(mReturnPath); 
				}

			}
		});
		
		initList("/");   //初始化從根目錄開始
	}

	private void initList(String path) {
		File file = new File(path);
		File[] fileList = file.listFiles();
		infos = new ArrayList<Map<String, Object>>();
		Map<String, Object> item = new HashMap<String, Object>();
		Drawable drawable;
		if (path.equals("/")) {   //若是當前爲根目錄,返回上一級按鈕,不顯示
			drawable = getResources().getDrawable(R.drawable.versionup);
			drawable.setBounds(0, 0, drawable.getMinimumWidth(),
					drawable.getMinimumHeight());
			mCurrentPath.setCompoundDrawablePadding(10);
			mCurrentPath.setCompoundDrawables(drawable, null, null, null);
			mCurrentPath.setText("根目錄列表");
			mReturnh.setVisibility(View.GONE);
			mPathLine.setVisibility(View.GONE);
		} else {
			drawable = getResources().getDrawable(R.drawable.versionup);
			drawable.setBounds(0, 0, drawable.getMinimumWidth(),
					drawable.getMinimumHeight());
			mReturn.setCompoundDrawables(drawable, null, null, null);
			mReturn.setText("返回上一級");
			mReturnPath = file.getParent();  //保存該級目錄的上一級路徑
			mCurrentPath.setVisibility(View.VISIBLE);
			mPathLine.setVisibility(View.VISIBLE);
			mCurrentPath.setText(file.getPath());
		}

		try {
			for (int i = 0; i < fileList.length; i++) {
				item = new HashMap<String, Object>();
				File fileItem = fileList[i];
				if (fileItem.isDirectory()) {  //若是當前文件爲文件夾,設置文件夾的圖標
					item.put("icon", R.drawable.icon_one);
				} else
					item.put("icon", R.drawable.icon_two);
				item.put("name", fileItem.getName());
				item.put("path", fileItem.getAbsolutePath());
				infos.add(item);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

		adapter = new FileManagerAdapter(this);
		adapter.setFileListInfo(infos);
		mList.setAdapter(adapter);
	}

	private OnItemClickListener clickListener = new OnItemClickListener() {

		@Override
		public void onItemClick(AdapterView<?> arg0, View arg1, int position,
				long arg3) {
			File file = new File((String) (infos.get(position).get("path")));
			if (file.isDirectory()) {  //若點擊文件夾,則進入下一級目錄
				String nextPath = (String) (infos.get(position).get("path"));
				initList(nextPath);
			} else {            //若點擊文件,則將文件名發送至調用文件瀏覽器的主界面
				Intent intent = new Intent();
				intent.setClass(FileManagerActivity.this,
						A.class);
				intent.putExtra("fileName",
						(String) (infos.get(position).get("name")));
				intent.putExtra("path", (String) (infos.get(position).get("path")));
				setResult(RESULT_OK, intent);
				finish();
			}

		}
	};

}

文件瀏覽器的adapter3d

public class FileManagerAdapter extends BaseAdapter{
	private Context mContext;
	private List<Map<String, Object>> list = new ArrayList<Map<String,Object>>();
	

	public FileManagerAdapter(Context context) {
		super();
		mContext = context;
	}

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

	@Override
	public Object getItem(int position) {
		return position;
	}

	@Override
	public long getItemId(int arg0) {
		return arg0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup arg2) {
		FileMangerHolder holder;
		if(null == convertView){
			holder = new FileMangerHolder();
			convertView = LayoutInflater.from(mContext).inflate(R.layout.file_item, null);
		holder.icon = (ImageView) convertView.findViewById(R.id.file_item_icon);
		holder.name = (TextView) convertView.findViewById(R.id.file_item_name);
		convertView.setTag(holder);
		}else{
			holder = (FileMangerHolder) convertView.getTag();
		}
		
		holder.icon.setImageResource((Integer)(list.get(position).get("icon")));
		holder.name.setText((String)(list.get(position).get("name")));
		
		return convertView;
	}
	
	public class FileMangerHolder{
		public ImageView icon;
		public TextView name;
	}
	
	public void setFileListInfo(List<Map<String, Object>> infos){
		list.clear();
		list.addAll(infos);
		notifyDataSetChanged();
	}

}

file_list.layout
code

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/black"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/file_path"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="5dp"
        android:textColor="@color/white"
        android:textSize="12sp" />

    <View
        android:id="@+id/file_path_line"
        android:layout_width="match_parent"
        android:layout_height="0.3dp"
        android:background="@color/gray_dark" />

    <TextView
        android:id="@+id/file_return"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="5dp"
        android:textColor="@color/white"
        android:textSize="12sp" 
        />

    <View
        android:layout_width="match_parent"
        android:layout_height="0.3dp"
        android:background="@color/gray_dark" />

    <ListView
        android:id="@+id/file_list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:cacheColorHint="@color/transparent"
        android:divider="@color/gray_dark"
        android:dividerHeight="0.3dp"
        android:listSelector="@null"
        android:scrollbars="none" />

</LinearLayout>

file_item.layout
xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center_vertical"
    android:orientation="horizontal" >

    <ImageView
        android:id="@+id/file_item_icon"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="5dp" />

    <TextView
        android:id="@+id/file_item_name"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_margin="5dp"
        android:layout_weight="1"
        android:textColor="@color/white"
        android:textSize="12sp" />

</LinearLayout>
相關文章
相關標籤/搜索