IconifiedText<----IconifiedTextView<----IconifiedTextListAdapter<-----MainActivity
上述为四个类,后者依赖前者- package com.example.huajunfilebrowser;
- import android.graphics.drawable.Drawable;
- public class IconifiedText implements Comparable<IconifiedText> {
- private String text="";
- private Drawable icon=null;
- private String path;
- private boolean selectable=true;
- public IconifiedText() {
- }
- public IconifiedText(String text, Drawable icon,String path) {
- super();
- this.text = text;
- this.icon = icon;
- this.path=path;
- }
- public void setPath(String path) {
- this.path = path;
- }
- public String getPath() {
- return path;
- }
- public String getText() {
- return text;
- }
- public void setText(String text) {
- this.text = text;
- }
- public Drawable getIcon() {
- return icon;
- }
- public void setIcon(Drawable icon) {
- this.icon = icon;
- }
- public boolean isSelectable() {
- return selectable;
- }
- public void setSelectable(boolean selectable) {
- this.selectable = selectable;
- }
- @Override
- public int compareTo(IconifiedText another) {
- if(this.text!=null){
- return this.text.compareTo(another.getText());
- }else {
- throw new IllegalArgumentException();
- }
- }
- }
复制代码- public class IconifiedTextView extends LinearLayout {
- private TextView mTextView=null;
- private ImageView mImageView=null;
- public IconifiedTextView(Context context, IconifiedText aIconifiedText) {
- super(context);
- this.setOrientation(HORIZONTAL);
- mImageView=new ImageView(context);
- mImageView.setImageDrawable(aIconifiedText.getIcon());
- mImageView.setPadding(8, 12, 6, 12);
- addView(mImageView);
-
- mTextView=new TextView(context);
- mTextView.setText(aIconifiedText.getText());
- mTextView.setPadding(8, 6, 6, 10);
- mTextView.setTextSize(26);
- addView(mTextView);
-
- }
- public void setTextView(String words) {
- mTextView.setText(words);
- }
- public void setImageView(Drawable bullet) {
- mImageView.setImageDrawable(bullet);
- }
- }
复制代码- /**
- * 自定义文件列表适配器
- * @author 老衲玩IT
- */
- public class IconifiedTextListAdapter extends BaseAdapter {
- private Context context;
- private List<IconifiedText> items=new ArrayList<IconifiedText>();
- public IconifiedTextListAdapter(Context context){
- this.context=context;
- }
- public IconifiedTextListAdapter(Context context,List<IconifiedText> lit){
- this.context=context;
- this.items=lit;
- }
- public void addItem(IconifiedText it) {
- this.items.add(it);
- }
- public void setListItems(List<IconifiedText> lit) {
- this.items=lit;
- }
- public boolean areAllItemsSelectable() {
- return false;
- }
- public boolean isSelectable(int position) {
- return items.get(position).isSelectable();
- }
- @Override
- public int getCount() {
- return this.items.size();
- }
- @Override
- public Object getItem(int position) {
- return this.items.get(position);
- }
- @Override
- public long getItemId(int position) {
- // TODO Auto-generated method stub
- return position;
- }
- @Override
- public View getView(int position, View convertView, ViewGroup parent) {
- IconifiedTextView itv;
- if(convertView==null){
- itv=new IconifiedTextView(context, this.items.get(position));
- }else {
- itv=(IconifiedTextView) convertView;
- itv.setTextView(this.items.get(position).getText());
- itv.setImageView(this.items.get(position).getIcon());
- }
- return itv;
- }
- }
复制代码- /**
- * 程序入口
- * @author 老衲玩IT
- *
- */
- public class MainActivity extends Activity {
- //文件列表
- ListView listView;
- ImageButton imbtnBack;
- TextView tvCurrent;
- String currentDir="sdcard";
- File fileDir;
- File childFile;
- //List<IconifiedText> listIfts=new ArrayList<IconifiedText>();
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- listView=(ListView) findViewById(R.id.listView);
- fileDir=new File(currentDir);
- imbtnBack=(ImageButton) findViewById(R.id.imbtnback);
- tvCurrent=(TextView) findViewById(R.id.dir);
- tvCurrent.setText(fileDir.getName());
- //返回上级目录的按钮
- imbtnBack.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- toParent();
- }
- });
- listView.setOnItemLongClickListener(new OnItemLongClickListener() {
- @Override
- public boolean onItemLongClick(AdapterView<?> parent, View view,
- int position, long id) {
- childFile=new File(((IconifiedText)parent.getItemAtPosition(position)).getPath());
- rename();
- return false;
- }
- });
- listView.setOnItemClickListener(new OnItemClickListener() {
- @Override
- public void onItemClick(AdapterView<?> parent, View view,
- int position, long id) {
- String path=((IconifiedText)parent.getItemAtPosition(position)).getPath();
- fileDir=new File(path);
- browseTo(fileDir);
- }
- });
- browseTo(fileDir);
- }
- /**
- * 返回上级目录
- */
- private void toParent() {
- String fileParent=fileDir.getParent();
- if (fileParent!=null) {
- browseTo(new File(fileParent));
- }else {
- finish();
- }
- }
- /**
- * 打开指定的文件
- * @param afile
- */
- public void openFile(File afile) {
- Intent intent=new Intent();
- intent.setAction(android.content.Intent.ACTION_VIEW);
- File file=new File(afile.getAbsolutePath());
- String fileName=file.getName();
- if(checkEndWithInStringArray(fileName,getResources().getStringArray(R.array.fileEndingImage))){
- intent.setDataAndType(Uri.fromFile(file), "image/*");
- }else if(checkEndWithInStringArray(fileName,getResources().getStringArray(R.array.fileEndingAudio))){
- intent.setDataAndType(Uri.fromFile(file), "audio/*");
- }else if(checkEndWithInStringArray(fileName,getResources().getStringArray(R.array.fileEndingVideo))){
- intent.setDataAndType(Uri.fromFile(file), "video/*");
- }else {
- //getMIMEType(file);
- intent.setDataAndType(Uri.fromFile(file), "*/*");
- }
- startActivity(intent);
- }
- /**
- * 访问指定文件目录
- * @param file 需要访问的文件目录
- */
- private void browseTo(File file) {
- if (file.isDirectory()) {
- this.fileDir=file;
- String fileName=file.getName();
- if (fileName==null) {
- fileName="";
- }
- tvCurrent.setText(fileName);
- //IconifiedText ift=new IconifiedText();
- File[] files=file.listFiles();
- Resources res=getResources();
- Drawable drawableDir=res.getDrawable(R.drawable.folder);
- //Drawable drawableFile=res.getDrawable(android.R.drawable.ic_menu_gallery);
- IconifiedTextListAdapter adapter=new IconifiedTextListAdapter(this);
- //List<File> fileList=new ArrayList<File>();
- //fileList.addAll(files);
- for (int i = 0; i < files.length; i++) {
- for (int j = 1; j < i-1; j++) {
- String si=files[i].getName();
- String sj=files[j].getName();
- if (si.compareTo(sj)<0) {
- File tempFile=files[i];
- files[i]=files[j];
- files[j]=tempFile;
- }
- }
- }
- for (int i = 0; i < files.length; i++) {
- if (files[i].isDirectory()) {
- if (files[i].getName().startsWith(".")) {
- continue;
- }
- adapter.addItem(new IconifiedText(files[i].getName(), drawableDir,files[i].getPath()));
- }
- }
- for (int i = 0; i < files.length; i++) {
- Resources resources=getResources();
- if(files[i].isFile()){
- IconifiedText ift=new IconifiedText();
- if(checkEndWithInStringArray(files[i].getName(),resources.getStringArray(R.array.fileEndingImage))){
- ift.setIcon(res.getDrawable(R.drawable.image));
- }
- else if(checkEndWithInStringArray(files[i].getName(),resources.getStringArray(R.array.fileEndingAudio))){
- ift.setIcon(res.getDrawable(R.drawable.audio));
- }
- else if(checkEndWithInStringArray(files[i].getName(),resources.getStringArray(R.array.fileEndingVideo))){
- ift.setIcon(res.getDrawable(R.drawable.vedio));
- }
- else {
- ift.setIcon(res.getDrawable(R.drawable.otherfile));
- }
- String name=files[i].getName();
- if (name.length()>20) {
- name=name.substring(0, 20);
- }
- ift.setText(name);
- ift.setPath(files[i].getPath());
- adapter.addItem(ift);
- }
- }
- if (listView!=null) {
- listView.setAdapter(adapter);
- }
- }else if (file.isFile()) {
- openFile(file);
- }
- }
- public void updateAdapter() {
- }
- @Override
- public boolean onKeyDown(int keyCode, KeyEvent event) {
- if (keyCode==KeyEvent.KEYCODE_BACK) {
- toParent();
- }
- return true;
- }
复制代码 |