博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Android 学习笔记之ExpandableListView UI的简单用法
阅读量:4987 次
发布时间:2019-06-12

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

ExpandableListView是一个父子集的显示方式,

1、主界面

1 public class ExpListViewDome extends Activity { 2  3     private ExpandableListView Exp_ListView; 4     private String[] mGroups; 5     private List
> mChilds; 6 private ExpandableListAdapter adapter; 7 8 @Override 9 protected void onCreate(Bundle savedInstanceState) {10 super.onCreate(savedInstanceState);11 setContentView(R.layout.activity_explistviewdome);12 Exp_ListView = (ExpandableListView) this13 .findViewById(R.id.exp_ListView);14 mGroups = new String[2];15 mChilds = new ArrayList
>();16 getData();17 adapter = new ExpListView_Adapter(ExpListViewDome.this, mChilds,18 mGroups);19 Exp_ListView.setAdapter(adapter);20 21 //默认展开第一个父窗口22 Exp_ListView.expandGroup(0);23 24 }25 26 /**27 * 获取父窗口和子窗口的数据28 */29 public void getData() {30 31 mGroups[0] = "第一个老子";32 mGroups[1] = "第二个老子";33 34 mChilds = new ArrayList
>();35 List
list1 = new ArrayList
();36 list1.add("啦啦啦");37 list1.add("哈哈哈");38 list1.add("嘎嘎嘎");39 list1.add("呵呵呵");40 List
list2 = new ArrayList
();41 list2.add("巴巴巴");42 list2.add("恰恰恰");43 list2.add("呼呼呼");44 list2.add("哼哼哼");45 mChilds.add(list1);46 mChilds.add(list2);47 48 }

2、自定义适配器

1 public class ExpListView_Adapter extends BaseExpandableListAdapter { 2  3     private Context mContext; 4     private List
> mChilds; 5 private String[] mGroups; 6 7 public ExpListView_Adapter(Context context, List
> list, 8 String[] Groups) { 9 // TODO Auto-generated constructor stub]10 this.mContext = context;11 this.mChilds = list;12 this.mGroups = Groups;13 14 }15 16 @Override17 public Object getChild(int groupPosition, int childPosition) {18 // TODO Auto-generated method stub19 return mChilds.get(groupPosition).get(childPosition);20 }21 22 @Override23 public long getChildId(int groupPosition, int childPosition) {24 // TODO Auto-generated method stub25 return childPosition;26 }27 28 @Override29 public View getChildView(int groupPosition, int childPosition,30 boolean isLastChild, View convertView, ViewGroup parent) {31 // TODO Auto-generated method stub32 // 获取子窗口显示数据33 String str = mChilds.get(groupPosition).get(childPosition);34 // 设置数据显示样式35 TextView textView = new TextView(mContext);36 textView.setText(str);37 textView.setPadding(60, 0, 10, 10);38 textView.setGravity(Gravity.CENTER_VERTICAL);39 textView.setTextColor(Color.BLUE);40 textView.setTextSize(24);41 42 return textView;43 }44 45 @Override46 public int getChildrenCount(int groupPosition) {47 // TODO Auto-generated method stub48 return mChilds.get(groupPosition).size();49 }50 51 @Override52 public Object getGroup(int groupPosition) {53 // TODO Auto-generated method stub54 return mGroups[groupPosition];55 }56 57 @Override58 public int getGroupCount() {59 // TODO Auto-generated method stub60 return mGroups.length;61 }62 63 @Override64 public long getGroupId(int groupPosition) {65 // TODO Auto-generated method stub66 return groupPosition;67 }68 69 @Override70 public View getGroupView(int groupPosition, boolean isExpanded,71 View convertView, ViewGroup parent) {72 // TODO Auto-generated method stub73 // 获取父窗口显示数据74 String Title = mGroups[groupPosition];75 // 设置数据显示样式76 TextView textView = new TextView(mContext);77 textView.setText(Title);78 textView.setBackgroundColor(Color.GRAY);79 textView.setPadding(60, 0, 0, 0);80 textView.setTextColor(Color.BLUE);81 textView.setTextSize(30);82 83 return textView;84 }85 86 @Override87 public boolean hasStableIds() {88 // TODO Auto-generated method stub89 return false;90 }91 92 @Override93 public boolean isChildSelectable(int groupPosition, int childPosition) {94 // TODO Auto-generated method stub95 //true 为子窗口有点击事件96 return true;97 }
View Code

 

转载于:https://www.cnblogs.com/wjdawx/p/3229557.html

你可能感兴趣的文章
Maximum Gap 164
查看>>
Robot Framework Share 4
查看>>
【LeetCode】155. Min Stack
查看>>
【LeetCode】214. Shortest Palindrome
查看>>
现有资源和jsapi的融合一种方式
查看>>
UICollectionViewController的简单使用及一些注意点(json)
查看>>
Vue.js 源码分析(十三) 基础篇 组件 props属性详解
查看>>
Ubuntu系统升级内核方法
查看>>
Spring Bean单例与线程安全
查看>>
EasyUI datagrid.getSelections 没有返回正确的选择行数
查看>>
分享一个随机重排函数(C#)
查看>>
Asp.Net Core在CentOS部署与注意
查看>>
自反+递归 实现评论的无限引用
查看>>
新闻发布系统
查看>>
NOIP提高组2016 D1T2 【天天爱跑步】
查看>>
数据结构基础(19) --堆与堆排序
查看>>
HTML基础
查看>>
Window通过cmd查看端口占用、相应进程、杀死进程
查看>>
Exp4 恶意代码分析 _20151220
查看>>
Webbrowser 取消下载提示框
查看>>