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

相关文章

PHP应用中处理限流和API节流的最佳实践

《PHP应用中处理限流和API节流的最佳实践》限流和API节流对于确保Web应用程序的可靠性、安全性和可扩展性至关重要,本文将详细介绍PHP应用中处理限流和API节流的最佳实践,下面就来和小编一起学习... 目录限流的重要性在 php 中实施限流的最佳实践使用集中式存储进行状态管理(如 Redis)采用滑动

深入浅出Spring中的@Autowired自动注入的工作原理及实践应用

《深入浅出Spring中的@Autowired自动注入的工作原理及实践应用》在Spring框架的学习旅程中,@Autowired无疑是一个高频出现却又让初学者头疼的注解,它看似简单,却蕴含着Sprin... 目录深入浅出Spring中的@Autowired:自动注入的奥秘什么是依赖注入?@Autowired

PostgreSQL简介及实战应用

《PostgreSQL简介及实战应用》PostgreSQL是一种功能强大的开源关系型数据库管理系统,以其稳定性、高性能、扩展性和复杂查询能力在众多项目中得到广泛应用,本文将从基础概念讲起,逐步深入到高... 目录前言1. PostgreSQL基础1.1 PostgreSQL简介1.2 基础语法1.3 数据库

Python中的filter() 函数的工作原理及应用技巧

《Python中的filter()函数的工作原理及应用技巧》Python的filter()函数用于筛选序列元素,返回迭代器,适合函数式编程,相比列表推导式,内存更优,尤其适用于大数据集,结合lamb... 目录前言一、基本概念基本语法二、使用方式1. 使用 lambda 函数2. 使用普通函数3. 使用 N

Python中yield的用法和实际应用示例

《Python中yield的用法和实际应用示例》在Python中,yield关键字主要用于生成器函数(generatorfunctions)中,其目的是使函数能够像迭代器一样工作,即可以被遍历,但不会... 目录python中yield的用法详解一、引言二、yield的基本用法1、yield与生成器2、yi

Python多线程应用中的卡死问题优化方案指南

《Python多线程应用中的卡死问题优化方案指南》在利用Python语言开发某查询软件时,遇到了点击搜索按钮后软件卡死的问题,本文将简单分析一下出现的原因以及对应的优化方案,希望对大家有所帮助... 目录问题描述优化方案1. 网络请求优化2. 多线程架构优化3. 全局异常处理4. 配置管理优化优化效果1.

从基础到高阶详解Python多态实战应用指南

《从基础到高阶详解Python多态实战应用指南》这篇文章主要从基础到高阶为大家详细介绍Python中多态的相关应用与技巧,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录一、多态的本质:python的“鸭子类型”哲学二、多态的三大实战场景场景1:数据处理管道——统一处理不同数据格式

Java Stream 的 Collectors.toMap高级应用与最佳实践

《JavaStream的Collectors.toMap高级应用与最佳实践》文章讲解JavaStreamAPI中Collectors.toMap的使用,涵盖基础语法、键冲突处理、自定义Map... 目录一、基础用法回顾二、处理键冲突三、自定义 Map 实现类型四、处理 null 值五、复杂值类型转换六、处理

分布式锁在Spring Boot应用中的实现过程

《分布式锁在SpringBoot应用中的实现过程》文章介绍在SpringBoot中通过自定义Lock注解、LockAspect切面和RedisLockUtils工具类实现分布式锁,确保多实例并发操作... 目录Lock注解LockASPect切面RedisLockUtils工具类总结在现代微服务架构中,分布

Python标准库之数据压缩和存档的应用详解

《Python标准库之数据压缩和存档的应用详解》在数据处理与存储领域,压缩和存档是提升效率的关键技术,Python标准库提供了一套完整的工具链,下面小编就来和大家简单介绍一下吧... 目录一、核心模块架构与设计哲学二、关键模块深度解析1.tarfile:专业级归档工具2.zipfile:跨平台归档首选3.