Android之BaseExpandableListAdapter的用法
来源:互联网 发布:wex5 php 后端开发 编辑:程序博客网 时间:2024/05/17 06:45
BaseExpandableListAdapter是ExpandableListAdapter的抽象基类,从一些数据中提供数据和视图给可折叠列表视图。
例子详解:
首先定义一个xml布局文件:
- <?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="match_parent"
- android:orientation="vertical" >
- <ExpandableListView
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:id="@+id/ecpandable"
- />
- </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="match_parent" android:orientation="vertical" > <ExpandableListView android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/ecpandable" /></LinearLayout>
activity文件:
- package cn.csdn.activity;
- import android.app.Activity;
- import android.os.Bundle;
- 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;
- public class ExpandableListViewActivity extends Activity{
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- // TODO Auto-generated method stub
- super.onCreate(savedInstanceState);
- setContentView(R.layout.expandable_layout);
- /**BaseExpandableListAdapter实现了ExpandableListAdapter*/
- ExpandableListAdapter adapter = new BaseExpandableListAdapter(){
- /**----------定义数组-------------------------------------------------------------------*/
- private int[] images = new int[]{
- R.drawable.ic_launcher,
- R.drawable.stop,
- R.drawable.play
- };
- private String[] armTypes = new String[]{
- "神族","虫族","人族"
- };
- private String[][] arms = new String[][]{
- {"狂战士","龙骑士","黑暗圣堂"},
- {"小狗","飞龙","自爆妃子"},
- {"步兵","伞兵","护士mm"}
- };
- /*===========组元素表示可折叠的列表项,子元素表示列表项展开后看到的多个子元素项=============*/
- /**----------得到armTypes和arms中每一个元素的ID-------------------------------------------*/
- //获取组在给定的位置编号,即armTypes中元素的ID
- @Override
- public long getGroupId(int groupPosition) {
- return groupPosition;
- }
- //获取在给定的组的儿童的ID,就是arms中元素的ID
- @Override
- public long getChildId(int groupPosition, int childPosition) {
- return childPosition;
- }
- /**----------根据上面得到的ID的值,来得到armTypes和arms中元素的个数 ------------------------*/
- //获取的群体数量,得到armTypes里元素的个数
- @Override
- public int getGroupCount() {
- return armTypes.length;
- }
- //取得指定组中的儿童人数,就是armTypes中每一个种族它军种的个数
- @Override
- public int getChildrenCount(int groupPosition) {
- return arms[groupPosition].length;
- }
- /**----------利用上面getGroupId得到ID,从而根据ID得到armTypes中的数据,并填到TextView中 -----*/
- //获取与给定的组相关的数据,得到数组armTypes中元素的数据
- @Override
- public Object getGroup(int groupPosition) {
- return armTypes[groupPosition];
- }
- //获取一个视图显示给定组,存放armTypes
- @Override
- public View getGroupView(int groupPosition, boolean isExpanded,
- View convertView, ViewGroup parent) {
- TextView textView = getTextView();//调用定义的getTextView()方法
- textView.setText(getGroup(groupPosition).toString());//添加数据
- return textView;
- }
- /**----------利用上面getChildId得到ID,从而根据ID得到arms中的数据,并填到TextView中---------*/
- //获取与孩子在给定的组相关的数据,得到数组arms中元素的数据
- @Override
- public Object getChild(int groupPosition, int childPosition) {
- return arms[groupPosition][childPosition];
- }
- //获取一个视图显示在给定的组 的儿童的数据,就是存放arms
- @Override
- public View getChildView(int groupPosition, int childPosition, boolean isLastChild,
- View convertView, ViewGroup parent) {
- LinearLayout ll = new LinearLayout(ExpandableListViewActivity.this);
- ll.setOrientation(0);//定义为纵向排列
- ImageView logo = new ImageView(ExpandableListViewActivity.this);
- logo.setImageResource(images[groupPosition]);//添加图片
- ll.addView(logo);
- TextView textView = getTextView();//调用定义的getTextView()方法
- textView.setText(getChild(groupPosition,childPosition).toString());//添加数据
- ll.addView(textView);
- return ll;
- }
- /**------------------自定义一个设定TextView属性的方法----------------------------------------------*/
- //定义一个TextView
- private TextView getTextView(){
- AbsListView.LayoutParams lp = new AbsListView.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT,40);
- TextView textView = new TextView(ExpandableListViewActivity.this);
- textView.setLayoutParams(lp);
- textView.setPadding(36, 0, 0, 0);
- textView.setTextSize(20);
- return textView;
- }
- /**-------------------其他设置-------------------------------------------------------------------*/
- //孩子在指定的位置是可选的,即:arms中的元素是可点击的
- @Override
- public boolean isChildSelectable(int groupPosition,
- int childPosition) {
- return true;
- }
- //表示孩子是否和组ID是跨基础数据的更改稳定
- public boolean hasStableIds() {
- return true;
- }
- };
- /**使用适配器*/
- ExpandableListView expandListView = (ExpandableListView) this.findViewById(R.id.ecpandable);
- expandListView.setAdapter(adapter);
- }
- }
package cn.csdn.activity;import android.app.Activity;import android.os.Bundle;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;public class ExpandableListViewActivity extends Activity{@Overrideprotected void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.onCreate(savedInstanceState);setContentView(R.layout.expandable_layout);/**BaseExpandableListAdapter实现了ExpandableListAdapter*/ExpandableListAdapter adapter = new BaseExpandableListAdapter(){/**----------定义数组-------------------------------------------------------------------*/private int[] images = new int[]{R.drawable.ic_launcher,R.drawable.stop,R.drawable.play};private String[] armTypes = new String[]{"神族","虫族","人族"};private String[][] arms = new String[][]{{"狂战士","龙骑士","黑暗圣堂"},{"小狗","飞龙","自爆妃子"},{"步兵","伞兵","护士mm"}};/*===========组元素表示可折叠的列表项,子元素表示列表项展开后看到的多个子元素项=============*//**----------得到armTypes和arms中每一个元素的ID-------------------------------------------*///获取组在给定的位置编号,即armTypes中元素的ID@Overridepublic long getGroupId(int groupPosition) {return groupPosition;}//获取在给定的组的儿童的ID,就是arms中元素的ID@Overridepublic long getChildId(int groupPosition, int childPosition) {return childPosition;}/**----------根据上面得到的ID的值,来得到armTypes和arms中元素的个数 ------------------------*///获取的群体数量,得到armTypes里元素的个数@Overridepublic int getGroupCount() {return armTypes.length;}//取得指定组中的儿童人数,就是armTypes中每一个种族它军种的个数@Overridepublic int getChildrenCount(int groupPosition) {return arms[groupPosition].length;}/**----------利用上面getGroupId得到ID,从而根据ID得到armTypes中的数据,并填到TextView中 -----*///获取与给定的组相关的数据,得到数组armTypes中元素的数据@Overridepublic Object getGroup(int groupPosition) {return armTypes[groupPosition];}//获取一个视图显示给定组,存放armTypes@Overridepublic View getGroupView(int groupPosition, boolean isExpanded,View convertView, ViewGroup parent) {TextView textView = getTextView();//调用定义的getTextView()方法textView.setText(getGroup(groupPosition).toString());//添加数据return textView;}/**----------利用上面getChildId得到ID,从而根据ID得到arms中的数据,并填到TextView中---------*///获取与孩子在给定的组相关的数据,得到数组arms中元素的数据@Overridepublic Object getChild(int groupPosition, int childPosition) {return arms[groupPosition][childPosition];}//获取一个视图显示在给定的组 的儿童的数据,就是存放arms@Overridepublic View getChildView(int groupPosition, int childPosition, boolean isLastChild,View convertView, ViewGroup parent) {LinearLayout ll = new LinearLayout(ExpandableListViewActivity.this);ll.setOrientation(0);//定义为纵向排列ImageView logo = new ImageView(ExpandableListViewActivity.this);logo.setImageResource(images[groupPosition]);//添加图片ll.addView(logo);TextView textView = getTextView();//调用定义的getTextView()方法textView.setText(getChild(groupPosition,childPosition).toString());//添加数据ll.addView(textView);return ll;}/**------------------自定义一个设定TextView属性的方法----------------------------------------------*///定义一个TextViewprivate TextView getTextView(){AbsListView.LayoutParams lp = new AbsListView.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT,40);TextView textView = new TextView(ExpandableListViewActivity.this);textView.setLayoutParams(lp);textView.setPadding(36, 0, 0, 0);textView.setTextSize(20);return textView;}/**-------------------其他设置-------------------------------------------------------------------*///孩子在指定的位置是可选的,即:arms中的元素是可点击的@Overridepublic boolean isChildSelectable(int groupPosition,int childPosition) {return true;}//表示孩子是否和组ID是跨基础数据的更改稳定public boolean hasStableIds() {return true;}};/**使用适配器*/ExpandableListView expandListView = (ExpandableListView) this.findViewById(R.id.ecpandable);expandListView.setAdapter(adapter);}}
效果图:
0 0
- Android之BaseExpandableListAdapter的用法
- Android成长之路-BaseExpandableListAdapter的用法
- Android成长之路-BaseExpandableListAdapter的用法
- android之expandableListView+BaseExpandableListAdapter+ArrayList伸缩的用法:
- BaseExpandableListAdapter的用法
- BaseExpandableListAdapter的用法
- BaseExpandableListAdapter的用法
- BaseExpandableListAdapter的用法
- BaseExpandableListAdapter的用法
- BaseExpandableListAdapter的用法
- BaseExpandableListAdapter的用法
- BaseExpandableListAdapter的用法
- Android之ExpandableList扩展用法(基于BaseExpandableListAdapter)
- Android开发ListView之BaseExpandableListAdapter
- android 适配器Adpter的使用总结 之 BaseExpandableListAdapter
- Android开发:BaseExpandableListAdapter的使用
- Android开发:BaseExpandableListAdapter的使用
- Android BaseExpandableListAdapter
- [1545] New Year 2014(数位DP,现放标程,待看)
- ExtJs xtype一览
- 源码推荐(2月12日):高效低内存音频播放引擎 基于NSXMLParser的Objective-C XML阅读器
- android wifi休眠策略
- python fabric实现远程操作和部署
- Android之BaseExpandableListAdapter的用法
- backlight子系统二
- Confluence 贡献人摘要(Contributors Summary)宏
- 两表测试SQL语句
- 设计师浅谈标志设计经验
- 如何给自己的网站添加小图标
- 快速学五笔
- spring 的 一些心得
- java泛型(2)