选择器activity :
public class FileChooserActivity extends Activity { private ListView mFileListView = null; private File[] mFileList = null; private FileAdapter mFileAdapter = null; private String mCurPath = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_file_chooser); setViews(); initData(); }private void initData() { switch (FileUtil.getPid()) { case XXXX: File file = new File(XXX); if(file.exists()) { mFileList =file.listFiles(); mCurPath = XXX; } else { Toast.makeText(FileChooserActivity.this,getString(R.string.device_not_found),Toast.LENGTH_SHORT).show(); } break; } if(mFileList != null) { mFileAdapter = new FileAdapter(FileChooserActivity.this,mFileList); mFileListView.setAdapter(mFileAdapter); } }private void setViews() { mFileListView = (ListView) findViewById(R.id.file_list); mFileListView.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView> adapterView, View view, int position, long l) { mCurPath = mFileList[position].getPath(); if(mFileList[position].isDirectory()) { mFileList = mFileList[position].listFiles(); mFileAdapter.notifyData(mFileList); } else if(FileUtil.getFileExtension(mFileList[position].getName()).equalsIgnoreCase("m3u") || FileUtil.getFileExtension(mFileList[position].getName()).equalsIgnoreCase("m3u8") ){ /** * 是M3U文件,传给MAinActivity */ finishWithResult(mFileList[position]); } else { Toast.makeText(FileChooserActivity.this,getString(R.string.not_m3u),Toast.LENGTH_SHORT).show(); }} }); }private void finishWithResult(File file) { Uri startDir = Uri.fromFile(file); Intent intent = new Intent(); // 以intent的方式将结果返回调用类 intent.setDataAndType(startDir, "vnd.android.cursor.dir/lysesoft.andexplorer.file"); setResult(RESULT_OK, intent); finish(); }@Override public boolean onKeyDown(int keyCode, KeyEvent event) { if(event.getAction() == KeyEvent.ACTION_DOWN) { switch (keyCode) { case KeyEvent.KEYCODE_BACK: ViseLog.d("mCurPath : "+mCurPath); if(mCurPath.equals(XXX)) { finish(); } else { File file = new File(mCurPath); if(file.isDirectory()) { mCurPath = file.getParentFile().getPath(); mFileList = file.getParentFile().listFiles(); mFileAdapter.notifyData(mFileList); } } return true; }} return super.onKeyDown(keyCode, event); } }
activity_file_chooser
FileAdapter:
public class FileAdapter extends BaseAdapter { private LayoutInflater mInflater; private Context mContext; private File[] mFileList; public FileAdapter(Context context, File[] data ) { this.mContext = context; mInflater = LayoutInflater.from(mContext); this.mFileList = data; }public void notifyData(File[] data) { mFileList = data; notifyDataSetChanged(); }public int getCount() { // TODO Auto-generated method stub return mFileList.length; }public Object getItem(int position) { // TODO Auto-generated method stub return position; }public long getItemId(int position) { // TODO Auto-generated method stub return position; }@SuppressWarnings("deprecation") public View getView(int position, View convertView, ViewGroup parent) { // TODO Auto-generated method stub final ViewHolder viewHolder; if (convertView == null) { viewHolder = new ViewHolder(); if (viewHolder != null) { convertView = mInflater.inflate(R.layout.list_item, null); viewHolder.mFileName = (TextView) convertView.findViewById(R.id.text_file); viewHolder.mFileImg = (ImageView) convertView.findViewById(R.id.img_file); convertView.setTag(viewHolder); } } else { viewHolder = (ViewHolder) convertView.getTag(); }viewHolder.mFileName.setText(mFileList[position].getName()); return convertView; }private static class ViewHolder { private TextView mFileName; private ImageView mFileImg; }
list_item.xml:
【移动开发|基于Android TV端的文件选择器(UI比较丑,主要看逻辑)】