|
工作后很多東西都是現(xiàn)用現(xiàn)學(xué),不用就忘,如果不記錄一下估計(jì)有一天就歸零了。
從沒(méi)系統(tǒng)的學(xué)過(guò)android,都是偶爾看看書(shū)寫(xiě)點(diǎn)Demo。最近項(xiàng)目上可能需要用到手持設(shè)備,所以復(fù)習(xí)一下。項(xiàng)目中設(shè)計(jì)到文件上傳的功能,這樣文件資源瀏覽器是必不可少的控件。先百度了一下找到一位CSDN猴子的代碼(http://blog.csdn.net/x605940745/article/details/12580367)照著模仿下基本的功能出來(lái)了。但是有些不完善的地方?此膌ayout與后臺(tái)代碼的ID都沒(méi)有對(duì)應(yīng)上,想必這兩段的代碼非出自一個(gè)Demo吧!
首先說(shuō)第一個(gè)問(wèn)題:加載出來(lái)的子文件夾單擊沒(méi)有相應(yīng)。實(shí)現(xiàn)使用ListView加載更多的xml布局。所有的觸發(fā)事件都有,加斷點(diǎn)就是不進(jìn)啊。。。百度查了一下,后來(lái)找到了原因。問(wèn)題出在ListView子控件上有ImageButton元素,這種情況下單擊ListView時(shí)子控件會(huì)首先獲得focus。所以導(dǎo)致單擊ListView無(wú)效。解決辦法吧ImageButton換成ImageView就OK了
第二個(gè)問(wèn)題:只有返回home沒(méi)有返回上級(jí)菜單。這個(gè)比較簡(jiǎn)單,只需要用File.getParentFile();就OK了,但要注意的是判斷是否為跟節(jié)點(diǎn),因根節(jié)點(diǎn)在獲取夫級(jí)元素就會(huì)拋異常。
- Layout(activity_documents_browser.xml)源碼:
- <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:layout_gravity="center_horizontal"
- android:background="#000000"
- android:orientation="vertical"
- tools:context="com.tonfun.smapp.DocumentsBrowserActivity" >
- <LinearLayout
- android:layout_width="wrap_content"
- android:layout_height="32dp" >
- <ImageView
- android:id="@+id/imageBt1"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:focusable="false"
- android:padding="3dp"
- android:src="@drawable/img_back" />
- <TextView
- android:id="@+id/txt1"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:focusable="false"
- android:padding="3dp"
- android:textColor="#fff" />
- </LinearLayout>
- <View
- android:layout_width="fill_parent"
- android:layout_height="3dp"
- android:background="#218fff" />
- <ListView
- android:id="@+id/listFile"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:descendantFocusability="blocksDescendants" >
- </ListView>
- </LinearLayout>
- Layout(activity_folder.xml)源碼
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:orientation="vertical" >
- <LinearLayout
- android:layout_width="fill_parent"
- android:layout_height="30dp" >
- <ImageView
- android:id="@+id/imageBt1"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:focusable="false"
- android:padding="2dp"
- android:src="@drawable/img_home" />
- <TextView
- android:id="@+id/txt1"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:focusable="false"
- android:padding="2dp"
- android:textColor="#fff" />
- </LinearLayout>
- <View
- android:layout_width="fill_parent"
- android:layout_height="1dp"
- android:background="#fff" />
- </LinearLayout>
- 后臺(tái)(JAVA)代碼 :
- package com.tonfun.smapp;
- import java.io.File;
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.List;
- import java.util.Map;
- import android.app.Activity;
- import android.os.Bundle;
- import android.os.Environment;
- import android.provider.MediaStore.Images;
- import android.view.Menu;
- import android.view.MenuItem;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.AdapterView;
- import android.widget.AdapterView.OnItemClickListener;
- import android.widget.ImageButton;
- import android.widget.ImageView;
- import android.widget.ListView;
- import android.widget.SimpleAdapter;
- import android.widget.TextView;
- import android.widget.Toast;
- public class DocumentsBrowserActivity extends Activity {
- private ListView listfile;
- // 當(dāng)前文件目錄
- private String currentpath;
- private TextView txt1;
- private ImageView images;
- private TextView textview;
- private ImageView imagebt1;
- private File fBeforePath;
- private int[] img = { R.drawable.img_files, R.drawable.img_folder,
- R.drawable.img_home };
- private File[] files;
- private SimpleAdapter simple;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_documents_browser);
- listfile = (ListView) findViewById(R.id.listFile);
- txt1 = (TextView) findViewById(R.id.txt1);
- imagebt1 = (ImageView) findViewById(R.id.imageBt1);
- init(Environment.getExternalStorageDirectory());
- fBeforePath = Environment.getExternalStorageDirectory().getParentFile();
- listfile.setOnItemClickListener(new OnItemClickListener() {
- @Override
- public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
- long arg3) {
- // TODO Auto-generated method stub
- // 獲取單擊的文件或文件夾的名稱(chēng)
- String folder = ((TextView) arg1.findViewById(R.id.txt1))
- .getText().toString();
- try {
- File filef = new File(currentpath + '/' + folder);
- if (!filef.isFile()) {
- init(filef);
- } else {
- if (filef.getName().lastIndexOf('.') >= 0) {
- Toast.makeText(
- DocumentsBrowserActivity.this,
- "擴(kuò)展名:"
- + filef.getName().substring(
- filef.getName()
- .lastIndexOf('.'),
- filef.getName().length())
- + ";絕對(duì)路徑:"
- + filef.getAbsolutePath(),
- Toast.LENGTH_SHORT).show();
- }
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- });
- // 回根目錄
- imagebt1.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View v) {
- // init(Environment.getExternalStorageDirectory());
- if (!fBeforePath.getAbsolutePath().equals("/")) {
- init(fBeforePath);
- } else {
- Toast.makeText(DocumentsBrowserActivity.this, "提示:已為頂級(jí)目錄!",
- Toast.LENGTH_SHORT).show();
- }
- }
- });
- }
- // 界面初始化
- public void init(File f) {
- if (Environment.getExternalStorageState().equals(
- Environment.MEDIA_MOUNTED)) {
- // 獲取SDcard目錄下所有文件名
- fBeforePath = f.getParentFile();
- files = f.listFiles();
- if (!files.equals(null)) {
- currentpath = f.getPath();
- try {
- txt1.setText("當(dāng)前目錄為:" + f.getPath());
- } catch (Exception ex) {
- }
- List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
- for (int i = 0; i < files.length; i++) {
- Map<String, Object> maps = new HashMap<String, Object>();
- if (files[i].isFile())
- maps.put("image", img[0]);
- else
- maps.put("image", img[1]);
- maps.put("filenames", files[i].getName());
- list.add(maps);
- }
- simple = new SimpleAdapter(this, list,
- R.layout.activity_folder, new String[] { "image",
- "filenames" }, new int[] { R.id.imageBt1,
- R.id.txt1 });
- listfile.setAdapter(simple);
- }
- } else {
- System.out.println("該文件為空");
- }
- }
- }
復(fù)制代碼
不貼下載連接了,著急會(huì)宿舍睡覺(jué) (~﹃~)~zZ
忘記貼效果圖了(/ □ \)。。。


|
評(píng)分
-
查看全部評(píng)分
|