android学习笔记13——ExpandableListView
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的更多相关文章
- 【转】Pro Android学习笔记(二五):用户界面和控制(13):LinearLayout和TableLayout
目录(?)[-] 布局Layout 线性布局LinearLayout 表格布局TableLayout 布局Layout Layout是容器,用于对所包含的view进行布局.layout是view的子类 ...
- Android学习笔记之JSON数据解析
转载:Android学习笔记44:JSON数据解析 JSON(JavaScript Object Notation)是一种轻量级的数据交换格式,采用完全独立于语言的文本格式,为Web应用开发提供了一种 ...
- Android学习笔记36:使用SQLite方式存储数据
在Android中一共提供了5种数据存储方式,分别为: (1)Files:通过FileInputStream和FileOutputStream对文件进行操作.具体使用方法可以参阅博文<Andro ...
- 【转】Pro Android学习笔记(四):了解Android资源(下)
处理任意的XML文件 自定义的xml文件放置在res/xml/下,可以通过R.xml.file_name来获取一个XMLResourceParser对象.下面是xml文件的例子: <rootna ...
- Android 学习笔记之Volley(七)实现Json数据加载和解析...
学习内容: 1.使用Volley实现异步加载Json数据... Volley的第二大请求就是通过发送请求异步实现Json数据信息的加载,加载Json数据有两种方式,一种是通过获取Json对象,然后 ...
- Android学习笔记进阶之在图片上涂鸦(能清屏)
Android学习笔记进阶之在图片上涂鸦(能清屏) 2013-11-19 10:52 117人阅读 评论(0) 收藏 举报 HandWritingActivity.java package xiaos ...
- android学习笔记36——使用原始XML文件
XML文件 android中使用XML文件,需要开发者手动创建res/xml文件夹. 实例如下: book.xml==> <?xml version="1.0" enc ...
- Ext.Net学习笔记13:Ext.Net GridPanel Sorter用法
Ext.Net学习笔记13:Ext.Net GridPanel Sorter用法 这篇笔记将介绍如何使用Ext.Net GridPanel 中使用Sorter. 默认情况下,Ext.Net GridP ...
- udacity android 学习笔记: lesson 4 part b
udacity android 学习笔记: lesson 4 part b 作者:干货店打杂的 /titer1 /Archimedes 出处:https://code.csdn.net/titer1 ...
随机推荐
- public static void main(String[] args){}函数诠释
public static void main(String[] args){}函数诠释 主函数的一般写法如下: public static void main(String[] args){-} 下 ...
- 如何优雅的写C++代码(一)
// get the greatest power of two that is a divisor of n: return n&-n; // swap two integers a and ...
- hihoCoder挑战赛23
hihoCoder挑战赛23 A.Emulator 题意 给一张图,有\(N(N \le 300)\)个点, 给出任意两点之间的最短路. 求最多可以去掉多少条边,使得任意两点的最短路长度不变. 思路 ...
- nginx和apache下的url rewrite
将服务器上面的数据同步到本地之后,发现打开首页显示不正常,本地服务器是apache,经过打开url rewrite之后本地首页正常显示. 原因是phpwind本身支持了url rewrite的功能,但 ...
- UVA-11468 Substring(AC自动机+DP)
题目大意:给一些模板串,一些字符的出现概率.问不会出现模板串的概率是多少. 题目分析:是比较简单的概率DP+AC自动机.利用全概率公式递推即可. 代码如下: # include<iostream ...
- caffe: test code for Deep Learning approach
#include <stdio.h> // for snprintf #include <string> #include <vector> #include &q ...
- Java Language and Virtual Machine Specifications
The Java Language Specification, Java SE 8 Edition HTML | PDF The Java Virtual Machine Specification ...
- EJB 的理解
引用源:http://blog.csdn.NET/cymm_liu/article/details/7760989 1.EJB 概念的剖析 我们先看一下,EJB 的官方解释: 商务软件的核心部分是它的 ...
- centos7下环境配置
1: 安装memcached 问题:error: libevent is required. If it's already installed, specify its path using –w ...
- jQuery.fn.extend与jQuery.extend
jQuery.extend(),是扩展的jQuery这个类. 假设我们把jQuery这个类看成是人类,能吃饭能喝水能跑能跳,现在我们用jQuery.extend这个方法给这个类拓展一个能唱歌的技能.这 ...