博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
ExpandableListView可展开列表
阅读量:6936 次
发布时间:2019-06-27

本文共 3622 字,大约阅读时间需要 12 分钟。

hot3.png

ExpandableListView是ListView的子类,在其基础上进行了扩展,把英语中的列表项分为几组,每组里又可包含多个列表项。

在用法上,其显示的列表项由ExpandableAdapter提供 

xml

MainActivity

package org.crazyit.listview;import android.app.Activity;import android.os.Bundle;import android.view.Gravity;import android.view.View;import android.view.ViewGroup;import android.widget.AbsListView;import android.widget.BaseExpandableListAdapter;import android.widget.ExpandableListAdapter;import android.widget.ExpandableListView;import android.widget.ImageView;import android.widget.LinearLayout;import android.widget.TextView;/** * Description: * 
site: 
crazyit.org  * 
Copyright (C), 2001-2012, Yeeku.H.Lee * 
This program is protected by copyright laws. * 
Program Name: * 
Date: * @author  Yeeku.H.Lee kongyeeku@163.com * @version  1.0 */public class ExpandableListViewTest extends Activity{ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); //创建一个BaseExpandableListAdapter对象 ExpandableListAdapter adapter = new BaseExpandableListAdapter() { int[] logos = new int[] { R.drawable.p, R.drawable.z, R.drawable.t };  private String[] armTypes = new String[] { "神族兵种", "虫族兵种", "人族兵种"}; private String[][] arms = new String[][] { { "狂战士", "龙骑士", "黑暗圣堂", "电兵" }, { "小狗", "刺蛇", "飞龙", "自爆飞机" }, { "机枪兵", "护士MM" , "幽灵" } }; //获取指定组位置、指定子列表项处的子列表项数据 @Override public Object getChild(int groupPosition, int childPosition) { return arms[groupPosition][childPosition]; } @Override public long getChildId(int groupPosition, int childPosition) { return childPosition; } @Override public int getChildrenCount(int groupPosition) { return arms[groupPosition].length; } private TextView getTextView() { AbsListView.LayoutParams lp = new AbsListView.LayoutParams( ViewGroup.LayoutParams.FILL_PARENT, 64); TextView textView = new TextView(ExpandableListViewTest.this); textView.setLayoutParams(lp); textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT); textView.setPadding(36, 0, 0, 0); textView.setTextSize(20); return textView; } //该方法决定每个子选项的外观 @Override public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) { TextView textView = getTextView(); textView.setText(getChild(groupPosition, childPosition).toString()); return textView; } //获取指定组位置处的组数据 @Override public Object getGroup(int groupPosition) { return armTypes[groupPosition]; } @Override public int getGroupCount() { return armTypes.length; } @Override public long getGroupId(int groupPosition) { return groupPosition; } //该方法决定每个组选项的外观 @Override public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) { LinearLayout ll = new LinearLayout(ExpandableListViewTest.this); ll.setOrientation(0); ImageView logo = new ImageView(ExpandableListViewTest.this); logo.setImageResource(logos[groupPosition]); ll.addView(logo); TextView textView = getTextView(); textView.setText(getGroup(groupPosition).toString()); ll.addView(textView); return ll; } @Override public boolean isChildSelectable(int groupPosition, int childPosition) { return true; } @Override public boolean hasStableIds() { return true; } }; ExpandableListView expandListView = (ExpandableListView) findViewById(R.id.list); expandListView.setAdapter(adapter); }}

转载于:https://my.oschina.net/mutouzhang/blog/207092

你可能感兴趣的文章
Oracle Database基础
查看>>
python编码最佳实践之总结
查看>>
TensorFlow的基本运算01-01
查看>>
SylixOS 基于STM32平台的GPIO模仿I2C总线的驱动开发流程
查看>>
波音公司计划利用 3D 打印技术制作模块化卫星
查看>>
将tgz文件解压到指定目录
查看>>
XSS攻击之窃取Cookie
查看>>
苹果支付和ios安全 - 你需要知道的
查看>>
String和int 转换
查看>>
Eclipse中阿里JAVA代码规范插件(P3C)的安装及使用
查看>>
Android零基础入门第77节:Activity任务栈和启动模式
查看>>
身份证号码的正则表达式及验证详解(JavaScript,Regex)
查看>>
知识点041-Samba 的安装
查看>>
阿里云推荐引擎使用教程
查看>>
2018/02/06
查看>>
easyopen原理解析——不到100行代码实现一个最精简的easyopen
查看>>
angularjs入门(四)
查看>>
TextView文本折叠Three
查看>>
linux基础(day22)
查看>>
生成RSA秘钥文件
查看>>