转载:ExpandableListView的使用
来源:互联网 发布:pop协议端口 编辑:程序博客网 时间:2024/05/25 18:09
http://blog.csdn.net/javatiger427/article/details/5937882
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)
- 转载:ExpandableListView的使用
- ExpandableListView的使用
- ExpandableListView的使用
- Android ExpandableListView的使用
- Android ExpandableListView的使用
- ExpandableListView的使用
- expandablelistview的使用
- Android ExpandableListView的使用
- ExpandableListView组件的使用
- ExpandableListView的基本使用
- ExpandableListView的使用
- ExpandableListView的使用示例
- ExpandableListView的使用
- ExpandableListView的使用总结
- Android ExpandableListView的使用
- ExpandableListView的使用
- Android ExpandableListView的使用
- ExpandableListview的使用介绍
- http://zhangziqiang.blog.51cto.com/698396/500213
- 关于Activity的生命周期
- 提取Launcher中的WorkSapce,可以左右滑动切换屏幕页面的类
- 开发日志(12.18)
- 国内第5大黑客高手发布盗号木马昨受审
- 转载:ExpandableListView的使用
- 对frameset、frame、iframe的js操作
- JDK、JRE、JVM之间的关系
- Android中的XML解析与生成——SAX解析XML
- 实现两个数的相加
- 搜索引擎原理简介
- $(document).ready()和window.onload的区别
- Spring 框架的设计理念与设计模式分析
- jquery 操作iframe