Copy native Android File Manager

Source: Internet
Author: User

Modeled after the Android native File Manager, this is a simple demo that can be easily followed up by existing code, as follows:


First create a new Listview_item, learn the adapter students should know what to do this is to draw the interface of each list item, here with the icon + file name

Listview_item.xml

<span style= "FONT-SIZE:18PX;" ><?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/ Res/android "    android:layout_width=" match_parent "    android:layout_height=" match_parent "    android:o rientation= "Horizontal" >        <imageview        android:id= "@+id/icon" android:layout_width= "Wrap_        Content "        android:layout_height=" wrap_content "/>        <textview         android:id=" @+id/name "        Android : textsize= "25sp"        android:layout_marginleft= "10DP"        android:layout_gravity= "center_vertical"        Android:textcolor= "#080808"        android:layout_width= "wrap_content"        android:layout_height= "Wrap_content" />            </LinearLayout></span>

And then we started writing about our main interface layout, the two-tiered linearlayout nesting

Activity_main.xml

<span style= "FONT-SIZE:18PX;" ><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:orien tation= "vertical" tools:context= "com.example.filemanagerdemo.MainActivity" > <linearlayout android:lay Out_width= "Fill_parent" android:layout_height= "wrap_content" android:orientation= "Horizontal" > &lt ; Button android:id= "@+id/parent" android:text= "return" android:background= "#00EE76" Androi D:layout_width= "Wrap_content" android:layout_height= "Wrap_content"/> <textview Android : id= "@+id/textview" android:layout_width= "wrap_content" android:layout_height= "Wrap_content" an droid:layout_weight= "1"/> </LinearLayout> <listview android:layout_width= "Wrap_content" an Droid:layout_height= "0DP" android:layout_margintop= "3DP" android:id= "@+id/listview" android:dividerheight= "1DP" android:layout_weight= "1" > </ListView></LinearLayout></span>

Well, now that the static interface is ready, here's the key to the main activity code to explain

Mainactivity.java

<span style= "FONT-SIZE:18PX;" >package com.example.filemanagerdemo;import java.io.file;import Java.io.ioexception;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.view.view;import Android.view.View.OnClickListener ; Import Android.widget.adapterview;import Android.widget.button;import Android.widget.listview;import Android.widget.simpleadapter;import Android.widget.textview;import Android.widget.toast;public class MainActivity Extends Activity {Private Button parent;p rivate listview ListView;p rivate TextView TextView;p rivate File currentparent    ; Records the parent folder of the current file private file[] currentfiles; @Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main); init ();} /* * Initialize control */private void init () {//TODO auto-generated method stubthis.parent = (Button) Findviewbyid (R.ID.Parent); This.listview = (ListView) Findviewbyid (R.id.listview); This.textview = (TextView) Findviewbyid (R.id.textview) ; File root = new file (Environment.getexternalstoragedirectory (). GetPath ()); if (Root.exists ()) {currentparent = root; Currentfiles = Root.listfiles (); Inflatelistview (currentfiles);} This.parent.setOnClickListener (New Onclicklistener () {@Overridepublic void OnClick (View v) {//TODO auto-generated Method Stubtry {if (!currentparent.getcanonicalfile (). Equals (Environment.getexternalstoragedirectory (). GetPath ())             {currentparent = Currentparent.getparentfile ();//Get parent Directory currentfiles = Currentparent.listfiles ();                             Get all the files in the current layer Inflatelistview (currentfiles); Update list}} catch (IOException e) {//TODO auto-generated catch Blocke.printstacktrace ();}}); This.listview.setOnItemClickListener (New Adapterview.onitemclicklistener () {@Overridepublic void Onitemclick ( Adapterview<?> parent, View view,int position, long ID) {//TODO auto-generated MethOd stub//If clicked is a file, do not do any processing if (Currentfiles[position].isfile ()) {return;} file[] temp = currentfiles[position].listfiles (); if (temp = = NULL | | temp.length = = 0) {Toast.maketext (Getapplicationcont Ext (), "This folder is unavailable or the folder is empty", Toast.length_long). Show ();; return;} Currentparent = Currentfiles[position]; currentfiles = temp; Inflatelistview (currentfiles);}) ;} /* * Update listview */private void Inflatelistview (file[] files) {//TODO auto-generated method Stublist<map<string,obj ect>> list = new arraylist<map<string, object>> (); for (int i = 0; i<files.length; i++) {map<string ,object> item= New hashmap<string, object> (), if (Files[i].isdirectory ()) {item.put ("icon", R.drawable.folder ) ;} else {item.put ("icon", r.drawable.file);//This can be written to add different icon}item.put ("name", Files[i].getname ()) According to the specific file type; List.add ( Item);} Simpleadapter adapter = new Simpleadapter (Getapplicationcontext (), List,r.layout.listview_item, new string[]{"icon", " Name "}, new Int[]{r.id.icon,r.id.name}); This.lisTview.setadapter (adapter); try{Textview.settext (Currentparent.getcanonicalpath ());  }catch (IOException e) {e.printstacktrace (); }}}</span>


The code should be easy to read from OnCreate. Ok, a copy of the Android native System File Manager demo on the development completed, this demo in order to let everyone see clearly, I used a very irregular writing habits, such as string should be put into string.xml, color also best not directly use RGB encoding, A new color.xml should be created under value to specify a name for this RGB encoding. Here in order to explain the principle of clarity, I have all used hard code, there is the way I get the root directory of the SD card is too simple rough, normal should write a method to judge all kinds of anomalies, such as determine whether there is an SD card or the current SD card is available, otherwise, once the user performs a bit of abnormal operation, our program will be This is the difference between a formal project and a demo.

OK that's all, there are errors, I hope the visitors can correct me.

I think it's a good thing.

Top!

d===== ( ̄▽ ̄*) b

Or

Oooo┏━┓oooo
(stepping) →┃ you ┃← (dead)
\ (→┃√┃←)/
\_) ┗━┛ (_/

Copy native Android File Manager

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.