ExpandableListView应用Demo

2024-04-17 03:18

本文主要是介绍ExpandableListView应用Demo,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

直接上代码:

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这个文件不用我说了,都知道的。

这篇关于ExpandableListView应用Demo的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/910660

相关文章

PostgreSQL的扩展dict_int应用案例解析

《PostgreSQL的扩展dict_int应用案例解析》dict_int扩展为PostgreSQL提供了专业的整数文本处理能力,特别适合需要精确处理数字内容的搜索场景,本文给大家介绍PostgreS... 目录PostgreSQL的扩展dict_int一、扩展概述二、核心功能三、安装与启用四、字典配置方法

Python中re模块结合正则表达式的实际应用案例

《Python中re模块结合正则表达式的实际应用案例》Python中的re模块是用于处理正则表达式的强大工具,正则表达式是一种用来匹配字符串的模式,它可以在文本中搜索和匹配特定的字符串模式,这篇文章主... 目录前言re模块常用函数一、查看文本中是否包含 A 或 B 字符串二、替换多个关键词为统一格式三、提

Java MQTT实战应用

《JavaMQTT实战应用》本文详解MQTT协议,涵盖其发布/订阅机制、低功耗高效特性、三种服务质量等级(QoS0/1/2),以及客户端、代理、主题的核心概念,最后提供Linux部署教程、Sprin... 目录一、MQTT协议二、MQTT优点三、三种服务质量等级四、客户端、代理、主题1. 客户端(Clien

CSS中的Static、Relative、Absolute、Fixed、Sticky的应用与详细对比

《CSS中的Static、Relative、Absolute、Fixed、Sticky的应用与详细对比》CSS中的position属性用于控制元素的定位方式,不同的定位方式会影响元素在页面中的布... css 中的 position 属性用于控制元素的定位方式,不同的定位方式会影响元素在页面中的布局和层叠关

SpringBoot3应用中集成和使用Spring Retry的实践记录

《SpringBoot3应用中集成和使用SpringRetry的实践记录》SpringRetry为SpringBoot3提供重试机制,支持注解和编程式两种方式,可配置重试策略与监听器,适用于临时性故... 目录1. 简介2. 环境准备3. 使用方式3.1 注解方式 基础使用自定义重试策略失败恢复机制注意事项

Python使用Tkinter打造一个完整的桌面应用

《Python使用Tkinter打造一个完整的桌面应用》在Python生态中,Tkinter就像一把瑞士军刀,它没有花哨的特效,却能快速搭建出实用的图形界面,作为Python自带的标准库,无需安装即可... 目录一、界面搭建:像搭积木一样组合控件二、菜单系统:给应用装上“控制中枢”三、事件驱动:让界面“活”

如何确定哪些软件是Mac系统自带的? Mac系统内置应用查看技巧

《如何确定哪些软件是Mac系统自带的?Mac系统内置应用查看技巧》如何确定哪些软件是Mac系统自带的?mac系统中有很多自带的应用,想要看看哪些是系统自带,该怎么查看呢?下面我们就来看看Mac系统内... 在MAC电脑上,可以使用以下方法来确定哪些软件是系统自带的:1.应用程序文件夹打开应用程序文件夹

Python Flask 库及应用场景

《PythonFlask库及应用场景》Flask是Python生态中​轻量级且高度灵活的Web开发框架,基于WerkzeugWSGI工具库和Jinja2模板引擎构建,下面给大家介绍PythonFl... 目录一、Flask 库简介二、核心组件与架构三、常用函数与核心操作 ​1. 基础应用搭建​2. 路由与参

Spring Boot中的YML配置列表及应用小结

《SpringBoot中的YML配置列表及应用小结》在SpringBoot中使用YAML进行列表的配置不仅简洁明了,还能提高代码的可读性和可维护性,:本文主要介绍SpringBoot中的YML配... 目录YAML列表的基础语法在Spring Boot中的应用从YAML读取列表列表中的复杂对象其他注意事项总

电脑系统Hosts文件原理和应用分享

《电脑系统Hosts文件原理和应用分享》Hosts是一个没有扩展名的系统文件,当用户在浏览器中输入一个需要登录的网址时,系统会首先自动从Hosts文件中寻找对应的IP地址,一旦找到,系统会立即打开对应... Hosts是一个没有扩展名的系统文件,可以用记事本等工具打开,其作用就是将一些常用的网址域名与其对应