Android之BaseExpandableListAdapter的用法

BaseExpandableListAdapter是ExpandableListAdapter的抽象基类,从一些数据中提供数据和视图给可折叠列表视图。

例子详解:

首先定义一个xml布局文件:

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:orientation="vertical" >  
  6.   
  7.     <ExpandableListView   
  8.         android:layout_width="fill_parent"  
  9.         android:layout_height="wrap_content"  
  10.         android:id="@+id/ecpandable"  
  11.         />  
  12.   
  13. </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文件:

  1. package cn.csdn.activity;  
  2.   
  3. import android.app.Activity;  
  4. import android.os.Bundle;  
  5. import android.view.View;  
  6. import android.view.ViewGroup;  
  7. import android.widget.AbsListView;  
  8. import android.widget.BaseExpandableListAdapter;  
  9. import android.widget.ExpandableListAdapter;  
  10. import android.widget.ExpandableListView;  
  11. import android.widget.ImageView;  
  12. import android.widget.LinearLayout;  
  13. import android.widget.TextView;  
  14.   
  15. public class ExpandableListViewActivity extends Activity{  
  16.   
  17.     @Override  
  18.     protected void onCreate(Bundle savedInstanceState) {  
  19.         // TODO Auto-generated method stub   
  20.         super.onCreate(savedInstanceState);  
  21.         setContentView(R.layout.expandable_layout);  
  22.           
  23.         /**BaseExpandableListAdapter实现了ExpandableListAdapter*/  
  24.         ExpandableListAdapter adapter = new BaseExpandableListAdapter(){  
  25.   
  26. /**----------定义数组-------------------------------------------------------------------*/  
  27.             private int[] images = new int[]{  
  28.                     R.drawable.ic_launcher,  
  29.                     R.drawable.stop,  
  30.                     R.drawable.play  
  31.             };  
  32.             private String[] armTypes = new String[]{  
  33.                     "神族","虫族","人族"  
  34.             };  
  35.             private String[][] arms = new String[][]{  
  36.                     {"狂战士","龙骑士","黑暗圣堂"},  
  37.                     {"小狗","飞龙","自爆妃子"},  
  38.                     {"步兵","伞兵","护士mm"}  
  39.             };  
  40.                           
  41. /*===========组元素表示可折叠的列表项,子元素表示列表项展开后看到的多个子元素项=============*/  
  42.   
  43. /**----------得到armTypes和arms中每一个元素的ID-------------------------------------------*/  
  44.               
  45.             //获取组在给定的位置编号,即armTypes中元素的ID   
  46.             @Override  
  47.             public long getGroupId(int groupPosition) {  
  48.                 return groupPosition;  
  49.             }  
  50.           
  51.             //获取在给定的组的儿童的ID,就是arms中元素的ID   
  52.             @Override  
  53.             public long getChildId(int groupPosition, int childPosition) {  
  54.                 return childPosition;  
  55.             }  
  56.               
  57. /**----------根据上面得到的ID的值,来得到armTypes和arms中元素的个数 ------------------------*/  
  58.               
  59.             //获取的群体数量,得到armTypes里元素的个数   
  60.             @Override  
  61.             public int getGroupCount() {  
  62.                 return armTypes.length;  
  63.             }  
  64.               
  65.             //取得指定组中的儿童人数,就是armTypes中每一个种族它军种的个数   
  66.             @Override  
  67.             public int getChildrenCount(int groupPosition) {  
  68.                 return arms[groupPosition].length;  
  69.             }  
  70.               
  71. /**----------利用上面getGroupId得到ID,从而根据ID得到armTypes中的数据,并填到TextView中 -----*/  
  72.               
  73.             //获取与给定的组相关的数据,得到数组armTypes中元素的数据   
  74.             @Override  
  75.             public Object getGroup(int groupPosition) {  
  76.                 return armTypes[groupPosition];  
  77.             }  
  78.   
  79.             //获取一个视图显示给定组,存放armTypes   
  80.             @Override  
  81.             public View getGroupView(int groupPosition, boolean isExpanded,  
  82.                     View convertView, ViewGroup parent) {  
  83.                 TextView textView = getTextView();//调用定义的getTextView()方法   
  84.                 textView.setText(getGroup(groupPosition).toString());//添加数据   
  85.                 return textView;  
  86.             }  
  87.   
  88. /**----------利用上面getChildId得到ID,从而根据ID得到arms中的数据,并填到TextView中---------*/  
  89.               
  90.             //获取与孩子在给定的组相关的数据,得到数组arms中元素的数据   
  91.             @Override  
  92.             public Object getChild(int groupPosition, int childPosition) {  
  93.                 return arms[groupPosition][childPosition];  
  94.             }  
  95.               
  96.             //获取一个视图显示在给定的组 的儿童的数据,就是存放arms   
  97.             @Override  
  98.             public View getChildView(int groupPosition, int childPosition, boolean isLastChild,  
  99.                     View convertView, ViewGroup parent) {  
  100.                 LinearLayout ll = new LinearLayout(ExpandableListViewActivity.this);  
  101.                 ll.setOrientation(0);//定义为纵向排列   
  102.                 ImageView logo = new ImageView(ExpandableListViewActivity.this);  
  103.                 logo.setImageResource(images[groupPosition]);//添加图片   
  104.                 ll.addView(logo);  
  105.                 TextView textView = getTextView();//调用定义的getTextView()方法   
  106.                 textView.setText(getChild(groupPosition,childPosition).toString());//添加数据   
  107.                 ll.addView(textView);  
  108.                 return ll;  
  109.             }  
  110.               
  111. /**------------------自定义一个设定TextView属性的方法----------------------------------------------*/  
  112.               
  113.             //定义一个TextView   
  114.             private TextView getTextView(){  
  115.                 AbsListView.LayoutParams lp = new AbsListView.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT,40);  
  116.                 TextView textView = new TextView(ExpandableListViewActivity.this);  
  117.                 textView.setLayoutParams(lp);  
  118.                 textView.setPadding(36000);  
  119.                 textView.setTextSize(20);  
  120.                 return textView;  
  121.             }  
  122.               
  123. /**-------------------其他设置-------------------------------------------------------------------*/  
  124.               
  125.             //孩子在指定的位置是可选的,即:arms中的元素是可点击的   
  126.             @Override  
  127.             public boolean isChildSelectable(int groupPosition,  
  128.                     int childPosition) {  
  129.                 return true;  
  130.             }  
  131.   
  132.             //表示孩子是否和组ID是跨基础数据的更改稳定   
  133.             public boolean hasStableIds() {  
  134.                 return true;  
  135.             }  
  136.         };  
  137.               
  138.             /**使用适配器*/  
  139.             ExpandableListView expandListView = (ExpandableListView) this.findViewById(R.id.ecpandable);  
  140.             expandListView.setAdapter(adapter);  
  141.     }  
  142.   
  143.       
  144. }  
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);
	}

	
}


 

效果图:

更多 1
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值