博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
ExpandableListView的简单例子
阅读量:4137 次
发布时间:2019-05-25

本文共 3805 字,大约阅读时间需要 12 分钟。

最近一段时间参考网上的例子,做了一下简单的 ExpandableListView,现在和大家共享一下:

1:main.xml的内容

[html] 
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout     
  3.      xmlns:android="http://schemas.android.com/apk/res/android"  
  4.      android:id="@+id/linearLayout"    
  5.      android:layout_width="fill_parent"     
  6.      android:layout_height="fill_parent"    
  7.      androidrientation="vertical"    
  8.      >    
  9.          
  10.      <ExpandableListView    
  11.      android:id="@+id/expandableListView"    
  12.      android:layout_width="fill_parent"    
  13.      android:layout_height="wrap_content"    
  14.          />    
  15. </LinearLayout>   

显示一个ExpandableListView

2:ExpandableListViewDemo 类的内容

[java] 
  1. package com.chama.expandablelistviewdemo;  
  2.   
  3. import java.util.ArrayList;  
  4. import java.util.List;  
  5. import android.app.Activity;  
  6. import android.os.Bundle;  
  7. import android.view.Gravity;  
  8. import android.view.View;  
  9. import android.view.ViewGroup;  
  10. import android.widget.AbsListView;  
  11. import android.widget.BaseExpandableListAdapter;  
  12. import android.widget.ExpandableListView;  
  13. import android.widget.TextView;  
  14.   
  15. public class ExpandableListViewDemo extends Activity {  
  16.       
  17.      //定义两个List,用来存放控件中Group/Child中的String  
  18.     private List<String> groupArray;          
  19.     private List<List<String>> childArray;  
  20.       
  21.     /** Called when the activity is first created. */  
  22.     @Override  
  23.     public void onCreate(Bundle savedInstanceState) {  
  24.         super.onCreate(savedInstanceState);  
  25.         setContentView(R.layout.main);  
  26.           
  27.         //对这两个List进行初始化,并插入一些数据  
  28.          groupArray = new ArrayList<String>();    
  29.          childArray = new ArrayList<List<String>>();    
  30.             
  31.          groupArray.add("第一行");    
  32.          groupArray.add("第二行");    
  33.              
  34.          List<String> tempArray = new ArrayList<String>();    
  35.          tempArray.add("第一条");    
  36.          tempArray.add("第二条");    
  37.          tempArray.add("第三条");    
  38.             
  39.         for(int index = 0; index <groupArray.size(); ++index)    
  40.         {    
  41.            childArray.add(tempArray);    
  42.        }    
  43.           
  44.        //给定义好的ExpandableListView添加上Adapter  
  45.        ExpandableListView expandableListView = (ExpandableListView)findViewById(R.id.expandableListView);  
  46.        ExpandableAdapter adapter = new ExpandableAdapter(this);  
  47.        expandableListView.setAdapter(adapter);  
  48.          
  49.     }  
  50.       
  51.     //定义ExpandableListView的Adapter  
  52.      public class ExpandableAdapter extends BaseExpandableListAdapter    
  53.      {    
  54.          Activity activity;    
  55.              
  56.          public ExpandableAdapter(Activity a)    
  57.          {    
  58.              activity = a;    
  59.          }    
  60.            
  61.          public Object getChild(int groupPosition, int childPosition)    
  62.          {    
  63.              return childArray.get(groupPosition).get(childPosition);    
  64.          }   
  65.            
  66.          public long getChildId(int groupPosition, int childPosition)    
  67.          {    
  68.              return childPosition;    
  69.          }   
  70.            
  71.          public int getChildrenCount(int groupPosition)    
  72.          {    
  73.              return childArray.get(groupPosition).size();    
  74.          }   
  75.            
  76.          public View getChildView(int groupPosition, int childPosition,    
  77.                  boolean isLastChild, View convertView, ViewGroup parent)    
  78.          {    
  79.              String string = childArray.get(groupPosition).get(childPosition);    
  80.              return getGenericView(string);    
  81.          }    
  82.            
  83.          // group method stub    
  84.          public Object getGroup(int groupPosition)    
  85.          {    
  86.              return groupArray.get(groupPosition);    
  87.          }    
  88.            
  89.          public int getGroupCount()    
  90.          {    
  91.              return groupArray.size();    
  92.          }   
  93.            
  94.          public long getGroupId(int groupPosition)    
  95.          {    
  96.              return groupPosition;    
  97.          }    
  98.            
  99.          public View getGroupView(int groupPosition, boolean isExpanded,    
  100.                  View convertView, ViewGroup parent)    
  101.          {    
  102.              String string = groupArray.get(groupPosition);    
  103.              return getGenericView(string);    
  104.          }    
  105.            
  106.          // View stub to create Group/Children 's View    
  107.          public TextView getGenericView(String string)    
  108.          {    
  109.              // Layout parameters for the ExpandableListView    
  110.              AbsListView.LayoutParams layoutParams = new AbsListView.LayoutParams(    
  111.                      ViewGroup.LayoutParams.FILL_PARENT, 64);    
  112.              TextView text = new TextView(activity);    
  113.              text.setLayoutParams(layoutParams);    
  114.              // Center the text vertically    
  115.              text.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);    
  116.              // Set the text starting position    
  117.              text.setPadding(36000);    
  118.              text.setText(string);    
  119.              return text;    
  120.          }    
  121.            
  122.         public boolean hasStableIds()    
  123.          {    
  124.              return false;    
  125.          }    
  126.          public boolean isChildSelectable(int groupPosition, int childPosition)    
  127.          {    
  128.              return true;    
  129.          }    
  130.      }    
  131. }  

需要注意的内容:

  1:groupArray 和childArray的内容,分别定义父对象和子对象显示的内容

  2:ExpandableAdapter继承了BaseExpandableListAdapter,并重载了其中的一些方法, 注意public TextView getGenericView(String string)的作用,主要用形成显示父对象和子对象视图的类

3:程序效果

 

转载地址:http://bclvi.baihongyu.com/

你可能感兴趣的文章
Jackson Tree Model Example
查看>>
常用js收集
查看>>
如何防止sql注入
查看>>
springmvc传值
查看>>
在Eclipse中查看Android源码
查看>>
Android使用webservice客户端实例
查看>>
[转]C语言printf
查看>>
C 语言 学习---获取文本框内容及字符串拼接
查看>>
C 语言学习 --设置文本框内容及进制转换
查看>>
C 语言 学习---判断文本框取得的数是否是整数
查看>>
C 语言 学习---ComboBox相关、简单计算器
查看>>
C 语言 学习---ComboBox相关、简易“假”管理系统
查看>>
C 语言 学习---回调、时间定时更新程序
查看>>
C 语言 学习---复选框及列表框的使用
查看>>
第十一章 - 直接内存
查看>>
JDBC核心技术 - 上篇
查看>>
一篇搞懂Java反射机制
查看>>
Single Number II --出现一次的数(重)
查看>>
Palindrome Partitioning --回文切割 深搜(重重)
查看>>
对话周鸿袆:从程序员创业谈起
查看>>