ExpandableListView应用Demo

来源:互联网 发布:网络招聘系统 编辑:程序博客网 时间:2024/05/02 11:28

直接上代码:

Java代码如下:

 

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.ExpandableListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ExpandableListView;
import android.widget.SimpleExpandableListAdapter;

/**
 * 继承ExpandableListActivity类
 */
public class DireCCTV extends ExpandableListActivity {

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  // TODO Auto-generated method stub
  super.onCreate(savedInstanceState);
  setContentView(R.layout.direcctv);
  // 创建一级条目
  List<Map<String, String>> groups = new ArrayList<Map<String, String>>();
  // 创建两个一级条目标题
  Map<String, String> group1 = new HashMap<String, String>();
  group1.put("group", "频道一");
  Map<String, String> group2 = new HashMap<String, String>();
  group2.put("group", "频道二");
  groups.add(group1);
  groups.add(group2);
  // 创建一级条目下的的二级条目
  List<Map<String, String>> child1 = new ArrayList<Map<String, String>>();
  // 同样是在一级条目目录下创建两个对应的二级条目目录
  Map<String, String> childdata1 = new HashMap<String, String>();
  childdata1.put("child", "通道一");
  Map<String, String> childdata2 = new HashMap<String, String>();
  childdata2.put("child", "通道二");
  child1.add(childdata1);
  child1.add(childdata2);
  // 同上
  List<Map<String, String>> child2 = new ArrayList<Map<String, String>>();
  Map<String, String> childdata3 = new HashMap<String, String>();
  childdata3.put("child", "通道三");
  Map<String, String> childdata4 = new HashMap<String, String>();
  childdata4.put("child", "通道四");
  child2.add(childdata3);
  child2.add(childdata4);
  // 将二级条目放在一个集合里,供显示时使用
  List<List<Map<String, String>>> childs = new ArrayList<List<Map<String, String>>>();
  childs.add(child1);
  childs.add(child2);
  /**
   * 使用SimpleExpandableListAdapter显示ExpandableListView 参数1.上下文对象Context
   * 参数2.一级条目目录集合 参数3.一级条目对应的布局文件 参数4.fromto,就是map中的key,指定要显示的对象
   * 参数5.与参数4对应,指定要显示在groups中的id 参数6.二级条目目录集合 参数7.二级条目对应的布局文件
   * 参数8.fromto,就是map中的key,指定要显示的对象 参数9.与参数8对应,指定要显示在childs中的id
   */
  SimpleExpandableListAdapter adapter = new SimpleExpandableListAdapter(
    this, groups, R.layout.groups, new String[] { "group" },
    new int[] { R.id.group }, childs, R.layout.childs,
    new String[] { "child" }, new int[] { R.id.child });
  setListAdapter(adapter);

 }

 /**
  * 设置哪个二级目录被默认选中
  */
 @Override
 public boolean setSelectedChild(int groupPosition, int childPosition,
   boolean shouldExpandGroup) {
  // do something
  return super.setSelectedChild(groupPosition, childPosition,
    shouldExpandGroup);
 }

 /**
  * 设置哪个一级目录被默认选中
  */
 @Override
 public void setSelectedGroup(int groupPosition) {
  // do something
  super.setSelectedGroup(groupPosition);
 }

 /**
  * 当二级条目被点击时响应
  */
 @Override
 public boolean onChildClick(ExpandableListView parent, View v,
   int groupPosition, int childPosition, long id) {
  // do something
  switch (groupPosition) {
  case 0:
   switch (childPosition) {
   case 0:
    Intent it = new Intent(DireCCTV.this, Video.class);
    it.putExtra("location",
      "rtsp://218.204.223.237:554/live/1/66251FC11353191F/e7ooqwcfbqjoo80j.sdp");
    startActivity(it);
    break;
   case 1:
    Intent it1 = new Intent(DireCCTV.this, Video.class);
    it1.putExtra("location",
      "rtsp://218.204.223.237:554/live/1/66251FC11353191F/e7ooqwcfbqjoo80j.sdp");
    startActivity(it1);
    break;
   }
   break;
  case 1:
   switch (childPosition) {
   case 0:
    Intent it = new Intent(DireCCTV.this, Video.class);
    it.putExtra("location",
      "rtsp://218.204.223.237:554/live/1/66251FC11353191F/e7ooqwcfbqjoo80j.sdp");
    startActivity(it);
    break;
   case 1:
    Intent it1 = new Intent(DireCCTV.this, Video.class);
    it1.putExtra("location",
      "rtsp://218.204.223.237:554/live/1/66251FC11353191F/e7ooqwcfbqjoo80j.sdp");
    startActivity(it1);
    break;
   }
   break;
  }
  return super.onChildClick(parent, v, groupPosition, childPosition, id);
 }

}

 

 

XMl代码如下:

主XML文件:

direcctv.xml

<?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:background="#FFFFFF"
    android:orientation="vertical" >

    <ExpandableListView
        android:id="@id/android:list"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:drawSelectorOnTop="false" />

   

    <TextView
        android:id="@id/android:empty"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:textColor="@color/textc"
        android:text="No Data" />

</LinearLayout>

 

 

一级目录的xml代码

groups.xml

<?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:orientation="vertical" >

    <TextView
        android:id="@+id/group"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:paddingBottom="10px"
        android:paddingLeft="35px"
        android:paddingRight="5px"
        android:paddingTop="10px"
        android:textColor="@color/textc"
        android:text="No Data"
        android:textSize="25sp" />

</LinearLayout>

 

 

二级目录的xml代码

 

childs.xml

 

<?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:orientation="horizontal" >
    <ImageView android:layout_width="wrap_content"
        android:layout_height="40px"
        android:src="@drawable/portrait"/>

    <TextView
        android:id="@+id/child"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:paddingBottom="10px"
        android:paddingLeft="25px"
        android:paddingRight="5px"
        android:paddingTop="10px"
        android:textColor="@color/textc"
        android:text="No Data"
        android:textSize="15sp" />

</LinearLayout>

 

 

粘贴上面的所有代码就可运行看到效果:当然有个Video。class这个是我自己调用android自带的播放器。也贴出来吧。

 

Video.class代码如下:

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.widget.MediaController;
import android.widget.Toast;
import android.widget.VideoView;

public class Video extends Activity {

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  // TODO Auto-generated method stub
  super.onCreate(savedInstanceState);
  setContentView(R.layout.video);
  VideoView videoview = (VideoView)findViewById(R.id.videoView1);
        MediaController mediaController = new MediaController(this);
        mediaController.setAnchorView(videoview);
        Intent intent=getIntent();        
        String value=intent.getStringExtra("location");
        Uri video = Uri.parse(value);
//        Uri video = Uri.parse("rtsp://218.204.223.237:554/live/1/66251FC11353191F/e7ooqwcfbqjoo80j.sdp");
        Toast.makeText(this, "正在加载视频,请稍等。。。。。。", Toast.LENGTH_LONG);
        videoview.setMediaController(mediaController);
        videoview.setVideoURI(video);
        videoview.start();
 }
}

 

 

当然AndroidManifest.xml这个文件不用我说了,都知道的。

原创粉丝点击