在用户配置的地方就有可能需要用户选择文件夹的功能,自己做了一个小的实例。
发上来给自己做一个简单的记录!
先上功能截图:
主要代码献上(在这里不再累赘的写主活动的内容,只讲解实现部分):
首先是XML的布局:
<RelativeLayout 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:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="com.accumulation.opensystemfileordir.app.MainActivity">
<TextView
android:id="@+id/fileText"
android:text="@string/hello_world"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="@+id/button"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
选择界面布局:
ListVIew用于显示列表,LinearLayout中的用于显示当前选择以及确定和向上按钮(向上按钮融合在TextView中)
其次是ListView中中没一行的布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#00000000">
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/listview_selected"
android:padding="6px">
<ImageView
android:id="@+id/icon"
android:layout_width="30dip"
android:layout_height="30dip"></ImageView>
<TextView
android:id="@+id/text"
android:layout_gravity="center_horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="@drawable/black"></TextView>
</LinearLayout>
</LinearLayout>
然后是代码部分,主要为一个继承ListViewActivity的类和一个适配器类:
/**
* Created by Qiujuer on 14-3-6.
*/
public class FileDirListAdapter extends BaseAdapter {
private LayoutInflater mInflater;
private Bitmap mIcon;
private List<String> paths;
public FileDirListAdapter(Context context, List<String> paths) {
this.mInflater = LayoutInflater.from(context);
this.paths = paths;
this.mIcon = BitmapFactory.decodeResource(context.getResources(), R.drawable.folder);
}
public int getCount() {
return paths.size();
}
public Object getItem(int position) {
return paths.get(position);
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.file_row, null);
holder = new ViewHolder();
holder.text = (TextView) convertView.findViewById(R.id.text);
holder.icon = (ImageView) convertView.findViewById(R.id.icon);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.icon.setImageBitmap(mIcon);
holder.text.setText(new File(paths.get(position).toString()).getName());
return convertView;
}
private class ViewHolder {
TextView text;
ImageView icon;
}
}
Activity实现的代码如下:
/**
* Created by Qiujuer on 14-3-6.
*/
public class FileDirManagerMainActivity extends ListActivity {
private List<String> paths = null;
private String rootPath = "/";
private String curPath = "/";
private TextView mPath;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_file_manager_main);
mPath = (TextView) findViewById(R.id.mPath);
mPath.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
getFileDir(new File(curPath).getParent());
}
});
mPath.setEnabled(true);
Button buttonConfirm = (Button) findViewById(R.id.buttonConfirm);
buttonConfirm.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent data = new Intent(FileDirManagerMainActivity.this, MainActivity.class);
Bundle bundle = new Bundle();
bundle.putString("file", curPath);
data.putExtras(bundle);
setResult(2, data);
finish();
}
});
paths = new ArrayList<String>();
getFileDir(rootPath);
}
private void getFileDir(String filePath) {
paths.clear();
mPath.setText(curPath = filePath);
//设置向上是否可用
if (filePath.equals(rootPath))
mPath.setEnabled(false);
else
mPath.setEnabled(true);
File f = new File(filePath);
File[] files = f.listFiles();
//判断当前下是否有文件夹
if (files.length <= 0)
return;
for (int i = 0; i < files.length; i++) {
//过滤一遍
//1.是否为文件夹
//2.是否可访问
if (files[i].isDirectory() && files[i].listFiles() != null) {
paths.add(files[i].getPath());
}
}
setListAdapter(new FileDirListAdapter(this, paths));
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
this.getFileDir(paths.get(position));
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.file_manager_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
String sdDir = "/sdcard/";
switch (id) {
case R.id.action_dir_camera:
this.getFileDir(sdDir + "camera");
break;
case R.id.action_dir_download:
this.getFileDir(sdDir + "download");
break;
case R.id.action_dir_music:
this.getFileDir(sdDir + "music");
break;
case R.id.action_dir_photo:
this.getFileDir(sdDir + "photo");
break;
case R.id.action_dir_video:
this.getFileDir(sdDir + "video");
break;
}
return super.onOptionsItemSelected(item);
}
}
OK,完工。因为我只需要选择文件夹的功能,所以在Activity的类中我过滤了文件的列表显示。
当然如果你是选择文件,那么你可以添加上,并添加上对文件的打开操作即可。
代码献上(Android Studio,用Eclipse中的无法导入,但是可以自行添加进去即可。)
转载请注明出处!