我自用的好些应用都要选择文件,但总不能每次写个Activity或ListView去浏览文件吧。所以写个Adapter,这样以后再有用到文件浏览或文件选择的话直接把这个Adapter放进ListView或GridView就行了,这样比较方便。
Adapter:
public class KFileAdapter extends BaseAdapter{ private File mFile; private List<File> mList; private Context mContext; private FileSelectListener mListener; public KFileAdapter(Context context,FileSelectListener listener){ mContext=context; mListener=listener; mFile=Environment.getExternalStorageDirectory(); mList=new ArrayList<File>(); try { getFileList(mList, mFile); } catch (Exception e) { // TODO Auto-generated catch block Toast.makeText(mContext, "请检查SD卡", Toast.LENGTH_SHORT).show(); } } @Override public int getCount() { // TODO Auto-generated method stub return mList.size(); } private void getFileList(List<File> list,File dir) throws Exception { File[] array=dir.listFiles(); list.clear(); if(!dir.equals(Environment.getExternalStorageDirectory())) list.add(dir.getParentFile()); for (File file : array) { list.add(file); } } private void listFile() throws Exception{ getFileList(mList, mFile); notifyDataSetChanged(); } @Override public File getItem(int arg0) { // TODO Auto-generated method stub return mList.get(arg0); } @Override public long getItemId(int position) { // TODO Auto-generated method stub return 0; } @Override public View getView(int position, View convertView, ViewGroup parent) { // TODO Auto-generated method stub if(convertView==null){ FrameLayout fl=new FrameLayout(mContext); convertView=fl; fl.setPadding(8, 8, 8, 8); fl.setBackgroundColor(0xffdddddd); TextView tmp=new TextView(mContext); fl.setTag(new ViewTag(getItem(position), tmp)); fl.addView(tmp, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT); convertView.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub File tag=((ViewTag)v.getTag()).getFile(); if(tag.isDirectory()){ mFile=tag; try { listFile(); mFile=tag; } catch (Exception e) { // TODO Auto-generated catch block Toast.makeText(mContext, "无法打开", Toast.LENGTH_SHORT).show(); } }else mListener.fileSelect(tag); } }); } ViewTag tag=(ViewTag)convertView.getTag(); tag.setFile(getItem(position)); TextView tmp=tag.getTextView(); if(!mFile.equals(Environment.getExternalStorageDirectory())&&position==0){ tmp.setText("..."); }else{ tmp.setText(getItem(position).getName()); } return convertView; } public interface FileSelectListener{ public abstract void fileSelect(File file); } private class ViewTag{ private File file; private TextView textView; public ViewTag(File file,TextView textView){ this.file=file; this.textView=textView; } public File getFile() { return file; } public void setFile(File file) { this.file = file; } public TextView getTextView() { return textView; } public void setTextView(TextView textView) { this.textView = textView; } } }
构造函数里的监听器里的代码在用户点击文件(非文件夹)时执行,想要点击文件夹执行的话改下
convertView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//添加你的代码
});
测试用的Activity:
public class MainActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ListView v=(ListView)findViewById(R.id.listView1); v.setAdapter(new KFileAdapter(this, new FileSelectListener() { @Override public void fileSelect(File file) { // TODO Auto-generated method stub Log.i("kagami", file.getName()); } })); GridView g=(GridView)findViewById(R.id.gridView1); g.setAdapter(new KFileAdapter(this, new FileSelectListener() { @Override public void fileSelect(File file) { // TODO Auto-generated method stub Log.i("kagami", file.getName()); } })); } }
上半是ListView,下半是GridView: