ExpandableListView控件的使用
来源:互联网 发布:龙虎榜数据哪里查 编辑:程序博客网 时间:2024/04/30 01:58
类似QQ手机,点击一个选项展现一个列表。Android中控件ExpandableListView可实现类似于此功能。
主界面布局文件:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#fff" > <ExpandableListView android:id="@+id/id_expandablelv" android:layout_width="match_parent" android:layout_height="wrap_content" /></RelativeLayout>
类似于ListView控件,直接获取该控件id,设置其适配器即可。
package com.example.expandablelistviewdemo;import android.app.Activity;import android.os.Bundle;import android.widget.ExpandableListView;public class MainActivity extends Activity {private ExpandableListView mExpandableListView;private ExpandableAdapter mExpandableAdapter;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);mExpandableListView = (ExpandableListView) findViewById(R.id.id_expandablelv);mExpandableAdapter = new ExpandableAdapter(this);mExpandableListView.setAdapter(mExpandableAdapter);}}
适配器文件:
package com.example.expandablelistviewdemo;import android.content.Context;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.widget.BaseExpandableListAdapter;import android.widget.ImageView;import android.widget.TextView;public class ExpandableAdapter extends BaseExpandableListAdapter {int[] logos = new int[]{ R.drawable.word, R.drawable.excel, R.drawable.email, R.drawable.ppt }; private String[] armTypes = new String[]{ "WORD", "EXCEL", "EMAIL", "PPT" }; private String[][] arms = new String[][]{ {"文档编辑", "文档排版", "文档处理", "文档打印"}, {"表格编辑", "表格排版", "表格处理", "表格打印"}, {"收发邮件", "管理邮箱", "登录登出", "注册绑定"}, {"演示编辑", "演示排版", "演示处理", "演示打印"}, }; LayoutInflater mLayoutInflater; public ExpandableAdapter(Context mContext) {mLayoutInflater = LayoutInflater.from(mContext);} @Overridepublic int getGroupCount() {//返回类别长度return armTypes.length;}@Overridepublic int getChildrenCount(int groupPosition) {return arms[groupPosition].length;}@Overridepublic Object getGroup(int groupPosition) {return armTypes[groupPosition];}@Overridepublic Object getChild(int groupPosition, int childPosition) {return arms[groupPosition][childPosition];}@Overridepublic long getGroupId(int groupPosition) {return groupPosition;}@Overridepublic long getChildId(int groupPosition, int childPosition) {return childPosition;}@Overridepublic boolean hasStableIds() {return true;}@Overridepublic View getGroupView(int groupPosition, boolean isExpanded,View convertView, ViewGroup parent) {convertView = mLayoutInflater.inflate(R.layout.item_group, null);ImageView logo = (ImageView) convertView.findViewById(R.id.id_group_logo);logo.setImageResource(logos[groupPosition]);TextView text = (TextView) convertView.findViewById(R.id.id_group_tv);text.setText(getGroup(groupPosition).toString());return convertView;}@Overridepublic View getChildView(int groupPosition, int childPosition,boolean isLastChild, View convertView, ViewGroup parent) {convertView = mLayoutInflater.inflate(R.layout.item_sub, null);TextView tv = (TextView) convertView.findViewById(R.id.id_sub_tv);tv.setText(getChild(groupPosition, childPosition).toString()); return convertView;}@Overridepublic boolean isChildSelectable(int groupPosition, int childPosition) {return true;}}
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="50dp" android:orientation="horizontal" > <ImageView android:id="@+id/id_group_logo" android:layout_width="20dp" android:layout_height="20dp" android:layout_marginLeft="30dp" android:layout_gravity="center_vertical" /> <TextView android:id="@+id/id_group_tv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:gravity="center_vertical" android:paddingLeft="5dp" /></LinearLayout>
自定义每个列表选项布局文件
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="45dp" android:orientation="vertical" > <TextView android:id="@+id/id_sub_tv" android:layout_width="wrap_content" android:layout_height="30dp" android:layout_gravity="center_horizontal|left" android:gravity="center_vertical|left" android:layout_marginLeft="30dp" android:textSize="20sp" /></LinearLayout>
使用还是挺简单的。
源码下载:
0 0
- ExpandableListView控件的使用
- Android::ExpandableListView二级列表控件的使用
- Android自带控件ExpandableListView的使用
- Android中ExpandableListView控件的使用
- ExpandableListView控件使用
- ExpandableListView控件基本使用
- 树形ExpandableListView控件使用
- Android之ExpandableListView控件使用
- 仿QQ列表--Android控件ExpandableListView的使用
- android 中可展开控件ExpandableListView的使用
- ExpandableListView控件使用中一些小问题的总结
- ExpandableListview控件使用---可展开的列表组件
- 使用Expandablelistview控件出现二级菜单出不来的问题
- [控件]支持索引的ExpandableListView
- ExpandableListView的使用
- ExpandableListView的使用
- Android ExpandableListView的使用
- Android ExpandableListView的使用
- android的编码习惯
- 为什么找不到js文件?
- 背包入门
- IOS开发之改变UIWebView文本字体的样式和大小
- 基于 Openstack 的 Tempest 测试框架的原理与实践
- ExpandableListView控件的使用
- OSX下虚拟域名问题(laravel)
- openwrt 之 NC 指令
- TCP/IP概述
- IDEA默认svn检出项目出错
- Java之DOM解析XML文件
- Cbuilder一定要包含有UnitN.cpp
- 深入理解C指针学习笔记七之动态内存管理
- 正则表达式全部符号解释