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. 使用Jmeter测试MySQL性能——(1)连接配置

    在搭建MySQL集群之后需要测试集群的性能究竟如何,采用Apache的测试工具Jmeter进行测试,本文主要介绍主要实现Jmeter配置连接到MySQL. 安装相应的软件 首先Jmeter是基于Jav ...

  2. 安装完openfire之后打不开的解决方案

    今天在http://www.igniterealtime.org/downloads/index.jsp下载了一个最新openfire for mac版 在系统的偏好设置里面是这样的.那个open A ...

  3. 转载:为什么要对URI进行编码

            为什么需要Url编码,通常如果一样东西需要编码,说明这样东西并不适合传输.原因多种多样,如Size过大,包含隐私数据,对于Url来说,之所以要进行编码,是因为Url中有些字符会引起歧义 ...

  4. PHP新手入门1——表单

    注:本身是Android,Android之前是java.但公司后台PHP特别多.就好奇php后台是怎么通过一个url给我数据的(完全不懂php).于是就学呗.学习系列随笔第一人称是一个Android小 ...

  5. hdu4639 hehe ——斐波纳契数列,找规律

    link:http://acm.hdu.edu.cn/showproblem.php?pid=4639 refer to: http://blog.csdn.net/dongdongzhang_/ar ...

  6. leetcode 93 Restore IP Addresses ----- java

    Given a string containing only digits, restore it by returning all possible valid IP address combina ...

  7. linux权限管理_ACL权限

    一.什么是ACL权限 ACL是Access Control List(访问控制列表)的缩写,主要的目的是在提供传统的owner,group,others的read,write,execute权限之外的 ...

  8. 51nod 最大子矩阵和(动态规划)

    最大子矩阵和 一个M*N的矩阵,矩阵中有一些整数(有正有负),找到此矩阵的一个子矩阵,并且这个子矩阵的元素的和是最大的,输出这个最大的值. 输入 第1行:M和N,中间用空格隔开(2 <= M,N ...

  9. svn 安装 、使用(2)

    写在前面的话: p.s.有必要读一读,不然可能会浪费你的时间. 该篇是接着上一篇的,上一篇是看了很多人写的文章,汇总的一些可能的情况,最后还是没有成功.此篇是在一个同学的帮助下成功的,也是在自己做好的 ...

  10. nginx配置说明

    #user nobody; worker_processes ; #error_log logs/error.log; #error_log logs/error.log notice; #error ...