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 ...
随机推荐
- easyui 入门
http://www.cnblogs.com/tangge/p/3214496.html 1.页面引用. jquery,easyui,主题easyui.css,图标ico.css,语言zh_CN.js ...
- [sql server发布订阅]after触发器执行失败造成复制不成功
结论: (以插入而例) 发布端的数据插入成功 订阅段的数据不会插入 实验 创建一张新表 create table test_subscriber (id int, mark varchar(2),in ...
- Munin监控的安装与配置
Munin 是一款类似 RRD tool 的优秀系统监控工具,它能提供给你多方面的系统性能信息,例如 磁盘.网络.进程.系统和用户. Munin 的工作原理 Munin 以客户端-服务器模式运行,主服 ...
- JavaScript----函数的封装、继承和多态
1.封装:把实现一个功能的代码放在一个函数中封装起来,以后再想实现这个功能的时候,我们不需要重新的编写代码了,只需要执行对应的函数即可,我们把这种机制就称之为"函数的封装"--&g ...
- 作业6 NABCD模型分析,产品Backlog
1.N(Need 需求): 随着生活水平的提高,每个家庭中都会有电脑和移动设备,可以更加快捷方便使用软件.以前孩子练习计算能力需要通做习题卷或老师出题目来进行,但现在只要通过这个四则运算的程序,可以自 ...
- px em rem在WEB前端开发中的区别
我们都知道基于像素的字体大小所用的单位是px,但是随着响应式设计的不断火热,基于相对字体大小的单位em变开始流行起来.当然,rem也在Web前端开发人员讨论如何更好设置字体大小的讨论话题之列.是不是需 ...
- kuangbin_ShortPath J (POJ 1511)
其实虽然一开始有被这个题的8000MS 和 256MB限制又被吓到 但是严格来说跟之前的POJ 3268是一样的做法只是数据大了点 但是问题就出在数据大了点上 其实严格来说也不大 1e6 数组加起来大 ...
- C++@sublime GDB调试
正文转自:http://www.cppblog.com/lucency/archive/2012/08/09/59214.html 之前在网上搜索了好久使用sublime调试C和C++的文章,但是徒劳 ...
- Unity入门知识
参考书:<Unity3D 游戏开发> ● scene图中按F键:放大,居中当前选中的物体 ● 坐标轴:红-x轴,绿-y轴,蓝-z轴 ● 逐帧运行程序: ● OnGUI:可以用来画界面 ● ...
- lua操作常用函数学习一
(1)lua 和 C++之间的交互的基本知识: lua 和 C++ 之间的数据交互通过堆栈进行,栈中的数据通过索引值进行定位,(栈就像是一个容器一样,放进去的东西都要有标号)其中栈顶是-1,栈底是1, ...