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

相关文章

5分钟获取deepseek api并搭建简易问答应用

《5分钟获取deepseekapi并搭建简易问答应用》本文主要介绍了5分钟获取deepseekapi并搭建简易问答应用,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需... 目录1、获取api2、获取base_url和chat_model3、配置模型参数方法一:终端中临时将加

JavaScript中的isTrusted属性及其应用场景详解

《JavaScript中的isTrusted属性及其应用场景详解》在现代Web开发中,JavaScript是构建交互式应用的核心语言,随着前端技术的不断发展,开发者需要处理越来越多的复杂场景,例如事件... 目录引言一、问题背景二、isTrusted 属性的来源与作用1. isTrusted 的定义2. 为

Python调用另一个py文件并传递参数常见的方法及其应用场景

《Python调用另一个py文件并传递参数常见的方法及其应用场景》:本文主要介绍在Python中调用另一个py文件并传递参数的几种常见方法,包括使用import语句、exec函数、subproce... 目录前言1. 使用import语句1.1 基本用法1.2 导入特定函数1.3 处理文件路径2. 使用ex

将Python应用部署到生产环境的小技巧分享

《将Python应用部署到生产环境的小技巧分享》文章主要讲述了在将Python应用程序部署到生产环境之前,需要进行的准备工作和最佳实践,包括心态调整、代码审查、测试覆盖率提升、配置文件优化、日志记录完... 目录部署前夜:从开发到生产的心理准备与检查清单环境搭建:打造稳固的应用运行平台自动化流水线:让部署像

Linux中Curl参数详解实践应用

《Linux中Curl参数详解实践应用》在现代网络开发和运维工作中,curl命令是一个不可或缺的工具,它是一个利用URL语法在命令行下工作的文件传输工具,支持多种协议,如HTTP、HTTPS、FTP等... 目录引言一、基础请求参数1. -X 或 --request2. -d 或 --data3. -H 或

在Ubuntu上部署SpringBoot应用的操作步骤

《在Ubuntu上部署SpringBoot应用的操作步骤》随着云计算和容器化技术的普及,Linux服务器已成为部署Web应用程序的主流平台之一,Java作为一种跨平台的编程语言,具有广泛的应用场景,本... 目录一、部署准备二、安装 Java 环境1. 安装 JDK2. 验证 Java 安装三、安装 mys

Python中构建终端应用界面利器Blessed模块的使用

《Python中构建终端应用界面利器Blessed模块的使用》Blessed库作为一个轻量级且功能强大的解决方案,开始在开发者中赢得口碑,今天,我们就一起来探索一下它是如何让终端UI开发变得轻松而高... 目录一、安装与配置:简单、快速、无障碍二、基本功能:从彩色文本到动态交互1. 显示基本内容2. 创建链

Node.js 中 http 模块的深度剖析与实战应用小结

《Node.js中http模块的深度剖析与实战应用小结》本文详细介绍了Node.js中的http模块,从创建HTTP服务器、处理请求与响应,到获取请求参数,每个环节都通过代码示例进行解析,旨在帮... 目录Node.js 中 http 模块的深度剖析与实战应用一、引言二、创建 HTTP 服务器:基石搭建(一

java中VO PO DTO POJO BO DO对象的应用场景及使用方式

《java中VOPODTOPOJOBODO对象的应用场景及使用方式》文章介绍了Java开发中常用的几种对象类型及其应用场景,包括VO、PO、DTO、POJO、BO和DO等,并通过示例说明了它... 目录Java中VO PO DTO POJO BO DO对象的应用VO (View Object) - 视图对象

Go信号处理如何优雅地关闭你的应用

《Go信号处理如何优雅地关闭你的应用》Go中的优雅关闭机制使得在应用程序接收到终止信号时,能够进行平滑的资源清理,通过使用context来管理goroutine的生命周期,结合signal... 目录1. 什么是信号处理?2. 如何优雅地关闭 Go 应用?3. 代码实现3.1 基本的信号捕获和优雅关闭3.2