android示例之音乐播放器第三天

来源:互联网 发布:淘宝注册会员名大全 编辑:程序博客网 时间:2024/06/02 17:48

今天要完成的功能是网络上的文件条目和本地文件条目分别在两个标签页中显示。

完成的效果图如下:


显示本地的文件列表:


首先要显示出图片中标签页的效果,需要三个Activity。一个整体的Activity,两个标签页分别是两个Activity。

整体的Activity的作用是把两个标签页的Activity整合起来。

整体Activity的布局文件是:

<?xml version="1.0" encoding="utf-8"?><TabHost     xmlns:android="http://schemas.android.com/apk/res/android"    android:id="@android:id/tabhost"    android:layout_width="fill_parent"    android:layout_height="fill_parent">    <LinearLayout         android:orientation="vertical"        android:layout_width="fill_parent"        android:layout_height="fill_parent"        android:padding="5dp">                <TabWidget             android:id="@android:id/tabs"            android:layout_width="fill_parent"            android:layout_height="wrap_content"/>        <FrameLayout             android:id="@android:id/tabcontent"            android:layout_width="fill_parent"            android:layout_height="fill_parent"            android:padding="5dp"/>    </LinearLayout></TabHost>

目前我还没有学习布局,不做详细解释。

整体的Activity代码:(我是用的Android版本是4.0,这个版本已经不推荐使用TabActivity了)

public class MainActivity extends TabActivity{@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);//对TabActivity的操纵,一般都是通过TabHost对象完成的TabHost tabHost=getTabHost();//得到启动标签的IntentIntent remoteIntent=new Intent();remoteIntent.setClass(this, RemoteActivity.class);//得到标签的头部对象,并设置头部内容TabHost.TabSpec remoteSpec=tabHost.newTabSpec("remote");//得到一个资源对象Resources res=getResources();//设置头部名称和图标remoteSpec.setIndicator("remote", res.getDrawable(R.drawable.ic_launcher));//设置标签的内容remoteSpec.setContent(remoteIntent);tabHost.addTab(remoteSpec);//设置启动另一个标签的ActivityIntent localIntent=new Intent();localIntent.setClass(this, LocalActivity.class);TabHost.TabSpec localSpec=tabHost.newTabSpec("local");localSpec.setIndicator("local", res.getDrawable(R.drawable.ic_launcher));localSpec.setContent(localIntent);tabHost.addTab(localSpec);}}

显示本地文件,需要在工具类中编写一个遍历本地文件的方法:

/** * 遍历指定路径的指定文件后缀的所有文件并返回 * @param path * @return */public List<Mp3Info> getLocalList(String path){List<Mp3Info> mp3Infos=new ArrayList<Mp3Info>();File file=new File(this.SDPATH+File.separator+path);File files[]=file.listFiles();for(int i=0;i<files.length;i++){if(files[i].getName().endsWith("mp3")){Mp3Info mp3Info=new Mp3Info();mp3Info.setMp3Name(files[i].getName());mp3Info.setMp3Size(files[i].length()+"");mp3Infos.add(mp3Info);}}return mp3Infos;}

本地标签的Activity的代码:

public class LocalActivity extends ListActivity{@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.local);showList();}//每次点击本地标签页都可以重新加载本地文件的列表@Overrideprotected void onResume() {super.onResume();showList();}//和RemoteActivity中的显示列表的方法一样,可以考虑这两个方法抽象为一个方法public void showList(){List<Mp3Info> mp3Infos=new FileUtil().getLocalList("mp3/");List<HashMap<String,String>> list=new ArrayList<HashMap<String,String>> ();for(Iterator iter=mp3Infos.iterator();iter.hasNext();){Mp3Info mp3Info=(Mp3Info) iter.next();HashMap<String,String> map=new HashMap<String,String>();map.put("mp3Name", mp3Info.getMp3Name());map.put("mp3Size", mp3Info.getMp3Size());list.add(map);}SimpleAdapter simpleAdapter=new SimpleAdapter(LocalActivity.this, list, R.layout.list, new String[]{"mp3Name","mp3Size"}, new int[]{R.id.mp3Name,R.id.mp3Size});setListAdapter(simpleAdapter);}}


0 0