天天看点

安卓 ExpandableListView的使用详解

在Android开发中,我们知道经常会用到ListView来加载一些列表数据,但有时候ListView并不能完全十分满足我们的需求。比如如下图的效果用 ExpandableListView实现起来就更方便点,我们直接用ExpandableListView,设置Group不能点击即可。好,费话不多说。下面详细介绍 ExpandableListView的使用。

安卓 ExpandableListView的使用详解

          图(一) ExpandableListView主要由组与子元素组成。所以我们要分别对组元素以及子元素进行配置及操作。由于ExpandableListView与ListView类似,所以ExpandableListView必不可少的配置过程是必不可少的。首先在XML文件里配置ExpandableListView。 main.xml配置如下: [html]  view plain  copy  print ?

  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     tools:context=".MainActivity" >  
  6.     <ExpandableListView   
  7.         android:id="@+id/list"  
  8.         android:layout_width="fill_parent"  
  9.         android:layout_height="fill_parent"  
  10.         android:background="#ffffff"  
  11.         android:cacheColorHint="#00000000"  
  12.         android:listSelector="#00000000"  
  13.         >  
  14.     </ExpandableListView>   
  15. </RelativeLayout>  

group.xml配置如下: [html]  view plain  copy  print ?

  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="wrap_content"  
  5.     android:gravity="center"  
  6.     android:orientation="vertical" >  
  7.     <TextView  
  8.         android:id="@+id/group_text"  
  9.          android:layout_width="match_parent"  
  10.          android:layout_height="wrap_content"  
  11.          android:paddingTop="10dip"  
  12.          android:paddingBottom="10dip"  
  13.          android:gravity="center_horizontal"  
  14.          android:text="122"  
  15.         />  
  16. </LinearLayout>  

child.xml配置如下: [html]  view plain  copy  print ?

  1. <pre name="code" class="html"><?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="horizontal" >  
  6.     <ImageView  
  7.         android:id="@+id/image"  
  8.          android:layout_width="60dip"  
  9.          android:layout_height="60dip"  
  10.          android:src="@drawable/ic_launcher"  
  11.         />  
  12.    <LinearLayout   
  13.         android:layout_width="match_parent"  
  14.         android:layout_height="wrap_content"  
  15.         android:orientation="vertical"  
  16.        >  
  17.        <TextView  
  18.            android:id="@+id/textOne"  
  19.             android:layout_width="match_parent"  
  20.             android:layout_height="match_parent"  
  21.             android:text="1"  
  22.            />  
  23.          <TextView  
  24.               android:id="@+id/textTwo"  
  25.             android:layout_width="match_parent"  
  26.             android:layout_height="match_parent"  
  27.             android:text="2"  
  28.            />  
  29.            <TextView  
  30.                 android:id="@+id/textThree"  
  31.             android:layout_width="match_parent"  
  32.             android:layout_height="match_parent"  
  33.             android:text="3"  
  34.            />  
  35.    </LinearLayout>  
  36. </LinearLayout>  

前面介绍过 ExpandableListView与ListView类似,所以ListView   Adapter中存在的方法, ExpandableListView  Adapter必定存在,只是Group和Child分别重写了 ListView   Adapter中的方法,同时新增加了两个方法,分别是hasStableIds() 和isChildSelectable(int groupPosition, int childPosition),所以 , ExpandableListView  Adapter中总共重写了10个方法。 其中要注意 hasStableIds() 和isChildSelectable(int groupPosition, int childPosition)这两个方法。hasStableIds() 主要是用来判断ExpandableListView内容id是否有效的(返回true or false),系统会跟据id来确定当前显示哪条内容,也就是firstVisibleChild的位置。而 isChildSelectable(int groupPosition, int childPosition)用来判断某Group某个child是否可可选。我们可以添加条件控制某Group某个child可点或不可点击。当不加任何条件直接返回false,所有的组的child均不可点击。 经过对ExpandableListView Adapter简单的说明,下面我们来看下TestExpandableListViewDemo源码。 其MainActiivty.java如下所示: [java]  view plain  copy  print ?

  1. <pre name="code" class="java">package com.example.testexpandlistview;  
  2. import java.util.ArrayList;  
  3. import java.util.List;  
  4. import android.app.Activity;  
  5. import android.os.Bundle;  
  6. import android.view.LayoutInflater;  
  7. import android.view.View;  
  8. import android.view.View.OnClickListener;  
  9. import android.view.ViewGroup;  
  10. import android.widget.BaseExpandableListAdapter;  
  11. import android.widget.ExpandableListView;  
  12. import android.widget.ExpandableListView.OnChildClickListener;  
  13. import android.widget.ExpandableListView.OnGroupClickListener;  
  14. import android.widget.ExpandableListView.OnGroupCollapseListener;  
  15. import android.widget.ExpandableListView.OnGroupExpandListener;  
  16. import android.widget.ImageView;  
  17. import android.widget.TextView;  
  18. import android.widget.Toast;  
  19. public class MainActivity extends Activity {  
  20.   private ExpandableListView mListView;  
  21.   private List<StrBean> group_list;  
  22.   private List<memBean> child_list;  
  23.   private StrBean strbean;  
  24.   private memBean membean;  
  25.   private LayoutInflater mInflater;  
  26.     @Override  
  27.     protected void onCreate(Bundle savedInstanceState) {  
  28.         super.onCreate(savedInstanceState);  
  29.         setContentView(R.layout.activity_main);  
  30.         initView();  
  31.     }  
  32.     private void initView(){  
  33.         mListView = (ExpandableListView) findViewById(R.id.list);  
  34.         mInflater = LayoutInflater.from(MainActivity.this);  
  35.         group_list = new ArrayList<StrBean>();  
  36.         for(int i=0;i<5;i++){  
  37.             strbean =new StrBean();  
  38.             strbean.spacename = "深圳航港"+i;  
  39.             strbean.childsize = 8;  
  40.             child_list = new ArrayList<memBean>();  
  41.             for(int j=0;j<8;j++){  
  42.                 membean = new memBean();  
  43.                 membean.price=1+j+"万";  
  44.                 membean.secondPrice="1.6万";  
  45.                 membean.flag = i;  
  46.                 membean.title = "商务卡";  
  47.                 child_list.add(membean);  
  48.             }  
  49.             strbean.setList(child_list);   
  50.             group_list.add(strbean);  
  51.         }  
  52.         Adapter adapter = new Adapter();  
  53.         mListView.setGroupIndicator(null);  
  54.         mListView.setOnGroupClickListener(new OnGroupClickListener() {  
  55.             @Override  
  56.             public boolean onGroupClick(ExpandableListView parent, View v,  
  57.                     int groupPosition, long id) {  
  58.                 Toast.makeText(MainActivity.this, "第"+groupPosition+"组被点击了", 0).show();  
  59.                 return true;  
  60.             }  
  61.         });  
  62.         mListView.setOnGroupExpandListener(new OnGroupExpandListener() {  
  63.             @Override  
  64.             public void onGroupExpand(int groupPosition) {  
  65.                 Toast.makeText(MainActivity.this, "第"+groupPosition+"组展开", 0).show();  
  66.             }  
  67.         });  
  68.         mListView.setOnGroupCollapseListener(new OnGroupCollapseListener() {  
  69.             @Override  
  70.             public void onGroupCollapse(int groupPosition) {  
  71.                 Toast.makeText(MainActivity.this, "第"+groupPosition+"组合拢", 0).show();  
  72.             }  
  73.         });  
  74.         mListView.setOnChildClickListener(new OnChildClickListener() {  
  75.             @Override  
  76.             public boolean onChildClick(ExpandableListView parent, View v,  
  77.                     int groupPosition, int childPosition, long id) {  
  78.                 Toast.makeText(MainActivity.this, "第"+groupPosition+"组的第"+childPosition+"被点击了", 0).show();  
  79.                 return true;  
  80.             }  
  81.         });  
  82.         mListView.setAdapter(adapter);  

[java]  view plain  copy  print ?

  1.                 //设置Group默认展开  
  2.      int groupCount = mListView.getCount();  
  3.      for(int i=0;i<groupCount;i++){  
  4.          mListView.expandGroup(i);  
  5.      }  
  6. }  
  7. //自定义适配器  
  8. class Adapter extends BaseExpandableListAdapter{  
  9.        //获取子元素对象  
  10.     @Override  
  11.     public Object getChild(int groupPosition, int childPosition) {  
  12.         return null;  
  13.     }  
  14.        //获取子元素Id  
  15.     @Override  
  16.     public long getChildId(int groupPosition, int childPosition) {  
  17.         return childPosition;  
  18.     }  
  19.        //加载子元素并显示  
  20.     @Override  
  21.     public View getChildView(final int groupPosition, final int childPosition,  
  22.             boolean isLastChild, View convertView, ViewGroup parent) {  
  23.         View view=null;  
  24.         ChildHolder childholder = null;  
  25.         if(convertView!=null){  
  26.             view = convertView;  
  27.             childholder = (ChildHolder) view.getTag();  
  28.         }else{  
  29.             view = View.inflate(MainActivity.this,R.layout.item, null);  
  30.             childholder = new ChildHolder();  
  31.             childholder.mImage = (ImageView) view.findViewById(R.id.image);  
  32.             childholder.mPrice = (TextView) view.findViewById(R.id.textTwo);  
  33.             childholder.mStateText = (TextView) view.findViewById(R.id.textOne);  
  34.             childholder.mSecondPrice = (TextView) view.findViewById(R.id.textThree);  
  35.             view.setTag(childholder);  
  36.         }  
  37.         childholder.mImage.setOnClickListener(new OnClickListener() {  
  38.             @Override  
  39.             public void onClick(View v) {  
  40.                 Toast.makeText(MainActivity.this, "第"+groupPosition+"组的第"+childPosition+"圖標被点击了", 0).show();  
  41.             }  
  42.         });  
  43.         childholder.mPrice.setText(group_list.get(groupPosition)  
  44.                 .getList().get(childPosition).price);  
  45.         int len = group_list.get(groupPosition)  
  46.                 .getList().size();  
  47.         System.out.println(len+"-----------------");  
  48.         childholder.mStateText.setText(group_list.get(groupPosition)  
  49.                 .getList().get(childPosition).title);  
  50.         childholder.mSecondPrice.setText(group_list.get(groupPosition)  
  51.                 .getList().get(childPosition).secondPrice);  
  52.         return view;  
  53.     }  
  54.        //获取子元素数目  
  55.     @Override  
  56.     public int getChildrenCount(int groupPosition) {  
  57.         return group_list.get(groupPosition).childsize;  
  58.     }  
  59.     //获取组元素对象  
  60.     @Override  
  61.     public Object getGroup(int groupPosition) {  
  62.         return group_list.get(groupPosition);  
  63.     }  
  64.        //获取组元素数目  
  65.     @Override  
  66.     public int getGroupCount() {  
  67.         return group_list.size();  
  68.     }  
  69.     //获取组元素Id  
  70.     @Override  
  71.     public long getGroupId(int groupPosition) {  
  72.         return groupPosition;  
  73.     }  
  74.        //加载并显示组元素  
  75.     @Override  
  76.     public View getGroupView(int groupPosition, boolean isExpanded,  
  77.             View convertView, ViewGroup parent) {  
  78.         View view=null;  
  79.         GroupHolder groupholder = null;  
  80.         if(convertView!=null){  
  81.             view = convertView;  
  82.             groupholder = (GroupHolder) view.getTag();  
  83.         }else{  
  84.             view = View.inflate(MainActivity.this,R.layout.textview, null);  
  85.             groupholder =new GroupHolder();  
  86.             groupholder.mSpaceText = (TextView) view.findViewById(R.id.group_text);  
  87.             view.setTag(groupholder);  
  88.         }  
  89.         groupholder.mSpaceText.setText(group_list.get(groupPosition).spacename);  
  90.         return view;  
  91.     }  
  92.     @Override  
  93.     public boolean hasStableIds() {  
  94.         return true;  
  95.     }  
  96.     @Override  
  97.     public boolean isChildSelectable(int groupPosition, int childPosition) {  
  98.         return false;  
  99.     }  
  100. }  
  101. static class GroupHolder{  
  102.     TextView mSpaceText;  
  103. }  
  104. static class ChildHolder{  
  105.     ImageView mImage;  
  106.     TextView mStateText;  
  107.     TextView mPrice;  
  108.     TextView mSecondPrice;  
  109. }  
安卓 ExpandableListView的使用详解

               效果图

在上面的源码中,总结说来ExpandableListView主要有setOnGroupClickListener, setOnGroupExpandListener, setOnGroupCollapseListener,setOnChildClickListener这四个监听事件,分别表示组元素点击事件,组元素展开,组收缩,子元素点击事件。我们可按需添加这四个监听事件即可。由此看来ExpandableListView是多么的简单。看似简单,但我们也要注意其中的一些细节。我相信你听过细节决定成败。下面我们就来一起看看细节。

1. 当设置setOnGroupClickListener监听并让其返回true时,所有Group消费点击事件,事件均不能分发传递给child(换言之,设置setOnChildClickListener不起任何作用)。

2.默认设置完Group以及child,Group左边会默认有以上下切换的图标,假如你有强迫症可以通过mListView.setGroupIndicator(null)去除。

3.前面已经简单说明了isChildSelectable(int groupPosition, int childPosition)方法的作用,所以当我们需要child可点击时,必须将setOnGroupClickListener和isChildSelectable对应设置为false和true。

4.说到这里也许读者会问,到底还是没有说出图一的做法。现在对图一进行详细的介绍。图一主要是将Group设置为不能收缩并且使其默认展开(即设置setOnGroupClickListener返回true,并且添加源码中setAdapter后三行代码)。

以上就是本人对ExpandableListView的一些浅薄之见,今天也是鄙人第一次写博客。都说Android技术博大精深,可能我阐述的只是冰山一角,希望大家踊跃的给出批评与建议。本人也希望能与热爱技术的开发人员进行深入的学习与交流。