ExpandableListView和Tobhost的简单用法

来源:互联网 发布:象棋旋风软件价格 编辑:程序博客网 时间:2024/06/11 11:53
         微笑生气 初次写博文,只是将自己和朋友平时的一点积累  给大家 供大家来学习 ,此次的学习目的主要是针对ExpandableListView和Tobhost的学习。实现一个简单的音乐播放器界面。

     吐舌头偷笑   ExpandableListView的里面实现主要是遍历本地SD卡的里面的歌曲,然后根据“歌手”歌名等显示在可扩展的LIstview里面。

 大笑抓狂遇到的难点:1.getChildView和getChildrenCount方法的执行顺序,同学们可以测试一下,弄不清这两个的关系将导致数组越界等错误。      2.getChildView和getGroupview返回View对应的显示。

ExpandableListView

public class Test extends Activity {ExpandableListView e;private List<String> parentTest;private List<Integer> parentLogo;List<List<String>> chird;TextView showMusicName;File file = null;File[] allMusicFile = null;public int m;// 界面里面的ExpandableListView的图标int[] logos = new int[] { R.drawable.local_singer,R.drawable.local_allsongs, R.drawable.local_file,R.drawable.local_custom, R.drawable.local_custom_like, };// 界面里面的ExpandableListView的文字String[] armTypes = new String[] { "歌手", "所有歌曲", "文件", "播放列表", "最近播放" };// 打开扩展后的 内容 此处只是 定义了一个数组 里面的内容处理一些逻辑 供测试用String[][] arms = new String[][] { { "a", "b" }, { "c", "d", "e" },{ "g", "h", "i", "j" }, { "k", "l", "m", "n", "o" },{ "p", "q", "r", "s", "t", "u" } };// 添加信息public void addInfo(int[] p1, String[] p2, String[][] c) {for (int i = 0; i < p1.length; i++) {parentLogo.add(p1[i]);}for (int i = 0; i < p2.length; i++) {parentTest.add(p2[i]);}List<String> item = new ArrayList<String>();for (int i = 0; i < c.length; i++) {item.add(c[i][i]);}chird.add(item);}@Overrideprotected void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.onCreate(savedInstanceState);setContentView(R.layout.test);/* * 下面的两行是查找文件用的,之情将其定义在了方法的外面,会出现崩溃的现象,主要的原因在于,在生成界面的时候没有出事话导致的。 */file = new File("/mnt/sdcard/yinyue");allMusicFile = file.listFiles();e = (ExpandableListView) findViewById(R.id.expandableNotive);parentLogo = new ArrayList<Integer>();parentTest = new ArrayList<String>();chird = new ArrayList<List<String>>();showMusicName = (TextView) findViewById(R.id.music);this.addInfo(logos, armTypes, arms);ExpandableListAdapter adapter = new BaseExpandableListAdapter() {@Overridepublic boolean isChildSelectable(int groupPosition,int childPosition) {// TODO Auto-generated method stubreturn true;}@Overridepublic boolean hasStableIds() {// TODO Auto-generated method stubreturn true;}//该方法决定每个组选项的外观@Overridepublic View getGroupView(int groupPosition, boolean isExpanded,View convertView, ViewGroup parent) {// TODO Auto-generated method stubString string = Test.this.parentTest.get(groupPosition);int i = parentLogo.get(groupPosition);String t = parentTest.get(groupPosition);return getView(t, i);}@Overridepublic long getGroupId(int groupPosition) {// TODO Auto-generated method stubreturn groupPosition;}@Overridepublic int getGroupCount() {// TODO Auto-generated method stubreturn armTypes.length;}@Overridepublic Object getGroup(int groupPosition) {// TODO Auto-generated method stubreturn armTypes[groupPosition];}@Overridepublic int getChildrenCount(int groupPosition) {if (groupPosition == 1) {return allMusicFile.length;}if (groupPosition == 0) {String people = null;String peopleName = null;m = allMusicFile.length - 1;for (int j = 0; j < allMusicFile.length; j++) {people = allMusicFile[j].getName().toString();if (people.contains("-")) {peopleName = people.substring(0,people.indexOf("-", 0));} else {m--;}// return getTextView(peopleName);}return m;}return arms[groupPosition].length;}public View getView(String te, int im) {/* * 上面的方式为在本界面中定义了得到系统的自带的linearLayout,以及imageview,和textview, * 的方式添加到getview中。 下面的方式为得到我在布局文件中定义好的布局,将其添加到view中。。。 */LayoutInflater inflate = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);LinearLayout mLinearLayout = (LinearLayout) inflate.inflate(R.layout.test1, null);TextView mText = (TextView) (mLinearLayout.findViewById(R.id.shuoming));ImageView mImage = (ImageView) (mLinearLayout.findViewById(R.id.tupian));mLinearLayout.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);mText.setText(te);mImage.setImageResource(im);mImage.setPadding(30, 5, 10, 5);mText.setPadding(5, 10, 0, 10);mText.setTextColor(Color.RED);mLinearLayout.setBackgroundColor(Color.GRAY);return mLinearLayout;}private TextView getTextView(String title) {AbsListView.LayoutParams lp = new AbsListView.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, 64);final TextView textview = new TextView(Test.this);textview.setLayoutParams(lp);textview.setText(title);textview.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {// TODO Auto-generated method stubif (textview.getText().toString().equals("a")) {Intent intent = new Intent();intent.setAction(Intent.ACTION_GET_CONTENT);intent.setType("vnd.android.cursor.item/phone");startActivity(intent);}if (textview.getText().toString().equals("b")) {Intent web = new Intent();Uri uri = Uri.parse("http:www.baidu.com");web.setAction(Intent.ACTION_VIEW);web.setData(uri);startActivity(web);}if (textview.getText().toString().equals("d")) {Toast.makeText(Test.this, "未实现的功能",Toast.LENGTH_SHORT).show();}}});textview.setBackgroundColor(Color.WHITE);textview.setTextColor(Color.BLACK);return textview;}//子选项的外观@Overridepublic View getChildView(int groupPosition, int childPosition,boolean isLastChild, View convertView, ViewGroup parent) {// TODO Auto-generated method stubif (file.exists()) {if (groupPosition == 1) {String name = null;for (int i = 0; i < allMusicFile.length; i++) {name = allMusicFile[childPosition].getName().toString();showMusicName.setText(name);return getTextView(name);}}if (groupPosition == 0) {String people = null;String peopleName = null;m = allMusicFile.length;for (int i = 0; i < allMusicFile.length; i++) {people = allMusicFile[childPosition].getName().toString();if (people.contains("-")) {peopleName = people.substring(0,people.indexOf("-", 0));} else {peopleName = people.substring(0,people.indexOf(".", 0));}return getTextView(peopleName);}}} else {return null;}return getTextView(arms[groupPosition][childPosition]);}@Overridepublic long getChildId(int groupPosition, int childPosition) {// TODO Auto-generated method stubreturn childPosition;}//获取指定的数组位置,显示子列表数据@Overridepublic Object getChild(int groupPosition, int childPosition) {// TODO Auto-generated method stubreturn arms[groupPosition][childPosition];}};e.setAdapter(adapter);}}


 

Tobhost

public class ToaHostPage extends TabActivity {private TabHost tab;@Overrideprotected void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.onCreate(savedInstanceState);tab=getTabHost();LayoutInflater.from(this).inflate(R.layout.desktop,tab.getTabContentView(),true);Intent notiveIntent=new Intent(ToaHostPage.this,Test.class);tab.addTab(tab.newTabSpec("notivateMusice").setIndicator("本地音乐").setContent(notiveIntent));tab.addTab(tab.newTabSpec("intenteMusice").setIndicator("网络音乐").setContent(R.id.IntenteMusice));tab.addTab(tab.newTabSpec("other").setIndicator("其他").setContent(R.id.other));}}