ExpandableListView==>可展开的列表组件

==》

ExpandableListView是ListView的子类,对其进行了扩展,其将应用中的列表项分为几组,每组中又包含多个列表项;

ExpandableListView的用法和ListView非常像,只是其所显示的列表项应该由ExpandableListAdapter提供;

ExpandableListView支持的额外属性:

android:childDivider 指定各组内各子列表项之间的分隔条
android:childIndicator 显示在子列表项旁的Drawable对象
android:groupIndicator 显示在组列表项旁的Drawable对象

实例:

布局文件==》
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" > <ExpandableListView
android:id="@+id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#ffffff"
android:cacheColorHint="#00000000"
android:listSelector="#00000000" >
</ExpandableListView> </LinearLayout> 实现代码==》
package com.example.myexpandablelistview; import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Gravity;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.AbsListView;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnChildClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast; public class MainActivity extends Activity
{
// 设置组视图的图片
private int[] logos = new int[]
{ R.drawable.one, R.drawable.two, R.drawable.three };
// 设置组视图的显示文字
private String[] generalsTypes = new String[]
{ "One", "Two", "Three" };
// 子视图显示文字
private String[][] generals = new String[][]
{
{ "西瓜", "樱桃", "草莓" },
{ "葡萄", "梨子", "青苹果" },
{ "香蕉", "橙子", "芒果" } }; // 子视图图片
public int[][] generallogos = new int[][]
{
{ R.drawable.one, R.drawable.one, R.drawable.one },
{ R.drawable.two, R.drawable.two, R.drawable.two, },
{ R.drawable.three, R.drawable.three, R.drawable.three } }; @Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
final ExpandableListAdapter adapter = new BaseExpandableListAdapter()
{
// 自己定义一个获得文字信息的方法
TextView getTextView()
{
@SuppressWarnings("deprecation")
AbsListView.LayoutParams lp = new AbsListView.LayoutParams(
ViewGroup.LayoutParams.FILL_PARENT, 64);
TextView textView = new TextView(MainActivity.this);
textView.setLayoutParams(lp);
textView.setGravity(Gravity.CENTER_VERTICAL);
textView.setPadding(36, 0, 0, 0);
textView.setTextSize(20);
textView.setTextColor(Color.BLACK);
return textView;
} // 重写ExpandableListAdapter中的各个方法
@Override
public int getGroupCount()
{
return generalsTypes.length;
} @Override
public Object getGroup(int groupPosition)
{
return generalsTypes[groupPosition];
} @Override
public long getGroupId(int groupPosition)
{
return groupPosition;
} @Override
public int getChildrenCount(int groupPosition)
{
return generals[groupPosition].length;
} @Override
public Object getChild(int groupPosition, int childPosition)
{
return generals[groupPosition][childPosition];
} @Override
public long getChildId(int groupPosition, int childPosition)
{
return childPosition;
} @Override
public boolean hasStableIds()
{
return true;
} @Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView,
ViewGroup parent)
{
LinearLayout ll = new LinearLayout(MainActivity.this);
ll.setOrientation(0);
ImageView logo = new ImageView(MainActivity.this);
logo.setImageResource(logos[groupPosition]);
logo.setPadding(50, 0, 0, 0);
ll.addView(logo);
TextView textView = getTextView();
textView.setTextColor(Color.BLACK);
textView.setText(getGroup(groupPosition).toString());
ll.addView(textView); return ll;
} @Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild,
View convertView, ViewGroup parent)
{
LinearLayout ll = new LinearLayout(MainActivity.this);
ll.setOrientation(0);
ImageView generallogo = new ImageView(MainActivity.this);
generallogo.setImageResource(generallogos[groupPosition][childPosition]);
ll.addView(generallogo);
TextView textView = getTextView();
textView.setText(getChild(groupPosition, childPosition).toString());
ll.addView(textView);
return ll;
} @Override
public boolean isChildSelectable(int groupPosition, int childPosition)
{
return true;
}
}; ExpandableListView expandableListView = (ExpandableListView) findViewById(R.id.list);
expandableListView.setAdapter(adapter); // 设置item点击的监听器
expandableListView.setOnChildClickListener(new OnChildClickListener()
{
@Override
public boolean onChildClick(ExpandableListView parent, View v, int groupPosition,
int childPosition, long id)
{ Toast.makeText(MainActivity.this,
"你点击了" + adapter.getChild(groupPosition, childPosition), Toast.LENGTH_SHORT)
.show(); return false;
}
});
} @Override
public boolean onCreateOptionsMenu(Menu menu)
{
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}

实现效果:

android学习笔记13——ExpandableListView的更多相关文章

  1. 【转】Pro Android学习笔记(二五):用户界面和控制(13):LinearLayout和TableLayout

    目录(?)[-] 布局Layout 线性布局LinearLayout 表格布局TableLayout 布局Layout Layout是容器,用于对所包含的view进行布局.layout是view的子类 ...

  2. Android学习笔记之JSON数据解析

    转载:Android学习笔记44:JSON数据解析 JSON(JavaScript Object Notation)是一种轻量级的数据交换格式,采用完全独立于语言的文本格式,为Web应用开发提供了一种 ...

  3. Android学习笔记36:使用SQLite方式存储数据

    在Android中一共提供了5种数据存储方式,分别为: (1)Files:通过FileInputStream和FileOutputStream对文件进行操作.具体使用方法可以参阅博文<Andro ...

  4. 【转】Pro Android学习笔记(四):了解Android资源(下)

    处理任意的XML文件 自定义的xml文件放置在res/xml/下,可以通过R.xml.file_name来获取一个XMLResourceParser对象.下面是xml文件的例子: <rootna ...

  5. Android 学习笔记之Volley(七)实现Json数据加载和解析...

    学习内容: 1.使用Volley实现异步加载Json数据...   Volley的第二大请求就是通过发送请求异步实现Json数据信息的加载,加载Json数据有两种方式,一种是通过获取Json对象,然后 ...

  6. Android学习笔记进阶之在图片上涂鸦(能清屏)

    Android学习笔记进阶之在图片上涂鸦(能清屏) 2013-11-19 10:52 117人阅读 评论(0) 收藏 举报 HandWritingActivity.java package xiaos ...

  7. android学习笔记36——使用原始XML文件

    XML文件 android中使用XML文件,需要开发者手动创建res/xml文件夹. 实例如下: book.xml==> <?xml version="1.0" enc ...

  8. Ext.Net学习笔记13:Ext.Net GridPanel Sorter用法

    Ext.Net学习笔记13:Ext.Net GridPanel Sorter用法 这篇笔记将介绍如何使用Ext.Net GridPanel 中使用Sorter. 默认情况下,Ext.Net GridP ...

  9. udacity android 学习笔记: lesson 4 part b

    udacity android 学习笔记: lesson 4 part b 作者:干货店打杂的 /titer1 /Archimedes 出处:https://code.csdn.net/titer1 ...

随机推荐

  1. SM30维护视图添加按钮

    转自http://blog.csdn.net/tsj19881202/article/details/7517232 遇到某需求,要求维护sm30的视图时,能加上排序按钮. 基本参考: http:// ...

  2. C#使用FtpWebRequest上传文件

    System.Net命名空间下的FtpWebRequest类实现了ftp协议的.Net实现. FtpWebRequest.KeepAlive指定在请求完成后服务器是否要马上关闭连接 FtpWebReq ...

  3. numpy之sum

    Definition : sum(a, axis=None, dtype=None, out=None, keepdims=False) axis: None or int or tuple of i ...

  4. JavaScript----函数的封装、继承和多态

    1.封装:把实现一个功能的代码放在一个函数中封装起来,以后再想实现这个功能的时候,我们不需要重新的编写代码了,只需要执行对应的函数即可,我们把这种机制就称之为"函数的封装"--&g ...

  5. php常用配置(php.ini)

    查看php配置文件的位置 # /usr/local/php/bin/php -i | head php配置文件中的注释是用;号 1.disable_functions(php要禁用的函数) phpin ...

  6. K650D安装黑苹果

    1.需要UEFI+GPT模式的win8或win10 2.关闭UEFI模式,进PE,分一个400M的分区,格式化为FAT16或EFI模式 3.制作clover模式的MAC安装U盘:链接: http:// ...

  7. PHP的一些要点

    1.用户评论的内容应当使用htmlspecialchars()函数进行过滤,如htmlspecialchars($_POST['content']);再写入数据库,防止用户评论中含有JS和HTML代码 ...

  8. 用 C# 在 Windows 7 中写注册表想到的

    摘自:http://blog.163.com/dpj_001/blog/static/2742941520110251500753/ 某日做一个项目,需要在注册表中加入键,同时写值,操作系统环境为 W ...

  9. ARM体系的7种工作模式

    一.ARM体系的CPU有以下7种工作模式:   用户模式(usr)    大多数程序运行于用户模式 特权模式   系统模式(sys)   运行具有特权的操作系统任务 异常模式 中断模式(irq)   ...

  10. Linux驱动设计——并发与竞态控制

    并发的概念:多个执行单元同时.并行被执行. 共享资源:硬件资源(IO/外设等),软件上的全局变量.静态变量等. 四种并发控制机制(对共享资源互斥的访问):原子操作.自旋锁(spinlock).信号量( ...