Android中ExpandableListView的使用
来源:互联网 发布:java大数据是什么 编辑:程序博客网 时间:2024/05/21 05:37
Android中ExpandableListView的使用
ExpandableListView是android中可以实现下拉list的一个控件,具体的实现方法如下:
首先:在layout的xml文件中定义一个ExpandableListView
- < LinearLayout
- android:id ="@+id/linearLayout"
- android:layout_width ="fill_parent"
- android:layout_height ="fill_parent"
- androidrientation ="vertical"
- >
- < ExpandableListView
- android:id ="@+id/expandableListView"
- android:layout_width ="fill_parent"
- android:layout_height ="wrap_content"
- />
- </ LinearLayout >
定义两个List,用来存放控件中Group/Child中的String
- private List<String> groupArray;
- private List<List<String>> childArray;
对这两个List进行初始化,并插入一些数据
- groupArray = new ArrayList<String>();
- childArray = new ArrayList<List<String>>();
- groupArray.add("第一行" );
- groupArray.add("第二行" );
- List<String> tempArray = new ArrayList<String>();
- tempArray.add("第一条" );
- tempArray.add("第二条" );
- tempArray.add("第三条" );
- for (int index = 0 ; index <groupArray.size(); ++index)
- {
- childArray.add(tempArray);
- }
定义ExpandableListView的Adapter
- //ExpandableListView的Adapter
- public class ExpandableAdapter extends BaseExpandableListAdapter
- {
- Activity activity;
- public ExpandableAdapter(Activity a)
- {
- activity = a;
- }
- public Object getChild(int groupPosition, int childPosition)
- {
- return childArray.get(groupPosition).get(childPosition);
- }
- public long getChildId(int groupPosition, int childPosition)
- {
- return childPosition;
- }
- public int getChildrenCount(int groupPosition)
- {
- return childArray.get(groupPosition).size();
- }
- public View getChildView(int groupPosition, int childPosition,
- boolean isLastChild, View convertView, ViewGroup parent)
- {
- String string = childArray.get(groupPosition).get(childPosition);
- return getGenericView(string);
- }
- // group method stub
- public Object getGroup(int groupPosition)
- {
- return groupArray.get(groupPosition);
- }
- public int getGroupCount()
- {
- return groupArray.size();
- }
- public long getGroupId(int groupPosition)
- {
- return groupPosition;
- }
- public View getGroupView(int groupPosition, boolean isExpanded,
- View convertView, ViewGroup parent)
- {
- String string = groupArray.get(groupPosition);
- return getGenericView(string);
- }
- // View stub to create Group/Children 's View
- public TextView getGenericView(String string)
- {
- // Layout parameters for the ExpandableListView
- AbsListView.LayoutParams layoutParams = new AbsListView.LayoutParams(
- ViewGroup.LayoutParams.FILL_PARENT, 64 );
- TextView text = new TextView(activity);
- text.setLayoutParams(layoutParams);
- // Center the text vertically
- text.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
- // Set the text starting position
- text.setPadding(36 , 0 , 0 , 0 );
- text.setText(string);
- return text;
- }
- public boolean hasStableIds()
- {
- return false ;
- }
- public boolean isChildSelectable(int groupPosition, int childPosition)
- {
- return true ;
- }
- }
最后,给定义好的ExpandableListView添加上Adapter
- ExpandableListView expandableListView = (ExpandableListView)findViewById(R.id.expandableListView);
- expandableListView.setAdapter(new ExpandableAdapter(Main.this ));
运行即可见效果~~~
----------------------------------------------------------------------------------------------------------------
Android版手风琴(ExpandableListView)
先看效果,过瘾一番。
下载地址:http://download.csdn.net/detail/a358763471/7755137
0 0
- Android中ExpandableListView的使用
- Android中ExpandableListView的使用
- Android中ExpandableListView的使用
- Android中ExpandableListView的使用
- Android中ExpandableListView的使用
- Android中ExpandableListView的使用
- Android中ExpandableListView的使用
- Android中ExpandableListView的使用
- Android中ExpandableListView的使用
- Android中ExpandableListView的使用
- Android中ExpandableListView的使用
- Android中ExpandableListView的使用
- Android中ExpandableListView的使用
- android中ExpandableListView的使用
- Android中ExpandableListView的使用
- android中ExpandableListView的使用
- Android中ExpandableListView的使用
- Android中ExpandableListView的使用
- Object与Json串相互转换工具JsonlibUtil
- python 字典 get方法
- TortoiseMerge 中文文档
- opencv 实时显示当前时间
- mysql 批量更新
- Android中ExpandableListView的使用
- 神奇的js Jquery的$命名冲突问题
- Android实现数据的存储技术(转)
- jsp与servlet的关系
- android 自定义Dialog背景透明及显示位置设置
- 1637 - Double Patience(状态转移+求成功概率)
- 用Python和Pygame写游戏-从入门到精通(实战二:恶搞俄罗斯方块4)
- Days in DLL(一)
- 存储过程运行日志记录通用模块