参考博客:

http://blog.csdn.net/xyz_lmn/article/details/6906268

http://www.apkbus.com/android-124715-1-1.html

有时候,使用ListView并不能满足应用程序所需要的功能。有些应用程序需要多组ListViw,这时候我们就要使用一种新的控件ExpandableListView——可以扩展的ListView。它的作用就是将ListView进行分组。就好像我们使用QQ的时候,有"我的好友","陌生人","黑名单"一样,点击一下会扩展开,再点击一下又会收缩回去。ExpandableListView是一个垂直滚动显示两级列表项的视图,与ListView不同的是,它可以有两层:每一层都能够被独立的展开并显示其子项。这些子项来自于与该视图关联的ExpandableListAdapter。

每一个可以扩展的列表项的旁边都有一个指示箭头用来说明列表项目前的状态(这些状态一般是已经展开的列表项,还没有展开的列表项,子列表和最后一个子列表项)。可以使用方法:setChildIndicator(Drawable),setGroupIndicator(Drawable)(或者相应的xml文件属性)去设置这些指示符的样式,当然也可以使用默认的指示符。

和ListView一样,ExpandableListView也是一个需要Adapter作为桥梁来取得数据的控件。一般适用于ExpandableListView的Adapter都要继承BaseExpandableListAdapter这个类,并且必须重载getGroupView和getChildView这两个最为重要的方法。

总结

1、ExpandableListView可扩展列表需要配合BaseExpanableListAdapter的子类来实现,并且实现getGroupView和getChildView两个方法

2、一级列表与二级列举数据集合问题


所以一级列表采用一级集合存储数据,二级列表采用二级集合存储数据,用代码实现如下:

ArrayList arrayList_groupData;ArrayList> arrayList_memberData;arrayList_groupData = new ArrayList();arrayList_memberData = new ArrayList>();for (int i = 0; i < 5; i++){     arrayList_groupData.add("Group "+i);     ArrayList arrayList_memberDataItem = new ArrayList();     for (int j = 0; j < 6; j++)     {        arrayList_memberDataItem.add("member "+j);     }     arrayList_memberData.add(arrayList_memberDataItem);}
   

3、在调用expandableListView.setAdapter(exAdapter)的时候,只会调用getGroupView方法(其中expandableListView是ExpandableListView的实例化对象,而exAdapter是继承于BaseExpandableListAdap的类实例对象)。而当一级列表子项被展开,首先会多次调用getGroupView方法,当是被展开的一级列表子项,则调用getChildView方法依次陈列二级列表子项。

4、设置二级列表左侧离一级列表的距离

只需要设置二级列表所对应布局中的第一个控件的android:layout_marginLeft属性,即设置该控件左边离父布局的距离。

5、设置一级列表的高度

将一级列表对应的布局文件所有控件放在一个LinearLayout线性布局中,然后通过设置此LinearLayout线性布局的android:layout_height属性,设置为多少个dip。在此之前我没有把此布局文件放到一个新的LinearLayout线性布局中,因为本身父布局就是LinearLayout线性布局,同样去设置android:layout_height属性值,却不能改变一级列表的高度,只有将所有空间重新放到新的LinearLayout线性布局中并设置android:layout_height属性值才可以,这里有点不明白?

AndroidManifest.xml——没有做任何修改,默认

<?xml version="1.0" encoding="utf-8"?>                                                                                    
string.xml

<?xml version="1.0" encoding="utf-8"?>     多级列表    Settings    Hello world!
grouplayout.xml

<?xml version="1.0" encoding="utf-8"?>                                            
memberlayout.xml
<?xml version="1.0" encoding="utf-8"?>            
activity_main.xml

    
MainActivity.java

package com.wxl.expandablelistview;import java.util.ArrayList;import android.os.Bundle;import android.util.Log;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.widget.BaseExpandableListAdapter;import android.widget.ExpandableListView;import android.widget.ExpandableListView.OnChildClickListener;import android.widget.ImageView;import android.widget.TextView;import android.app.Activity;import android.content.Context;public class MainActivity extends Activity {ExpandableListView expandableListView;ArrayList arrayList_groupData;ArrayList> arrayList_memberData;ExAdapter exAdapter;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        expandableListView = (ExpandableListView)this.findViewById(R.id.expandableListView);        arrayList_groupData = new ArrayList();        arrayList_memberData = new ArrayList>();        for (int i = 0; i < 5; i++)        {        arrayList_groupData.add("Group "+i);        ArrayList arrayList_memberDataItem = new ArrayList();        for (int j = 0; j < 6; j++)        {        arrayList_memberDataItem.add("member "+j);        }        arrayList_memberData.add(arrayList_memberDataItem);        }                exAdapter = new ExAdapter(this);        expandableListView.setAdapter(exAdapter);        //expandableListView.expandGroup(0);//设置第一组张开        //expandableListView.collapseGroup(0); 将第group组收起        expandableListView.setGroupIndicator(null);//除去自带的箭头,自带的箭头在父列表的最左边,不展开向下,展开向上        expandableListView.setDivider(null);//这个是设定每个Group之间的分割线。,默认有分割线,设置null没有分割线                expandableListView.setOnChildClickListener(new OnChildClickListener() {@Overridepublic boolean onChildClick(ExpandableListView parent, View view, int groupPosition,int childPosition, long id) {// TODO Auto-generated method stubexAdapter.setChildSelection(groupPosition,childPosition);exAdapter.notifyDataSetChanged();return true;}});    }        public class ExAdapter extends BaseExpandableListAdapter    {    Context context;    int selectParentItem = -1;    int selectChildItem = -1;    public ExAdapter(Context context) {// TODO Auto-generated constructor stub    this.context = context;}        public void setChildSelection(int groupPosition, int childPosition)    {    selectParentItem = groupPosition;    selectChildItem = childPosition;    }    @Overridepublic Object getChild(int groupPosition, int childPosition) {// TODO Auto-generated method stubreturn arrayList_memberData.get(groupPosition).get(childPosition);}@Overridepublic long getChildId(int groupPosition, int childPosition) {// TODO Auto-generated method stubreturn childPosition;}@Overridepublic View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView,ViewGroup parent) {// TODO Auto-generated method stubView view = convertView;Log.i("++++++++++", "groupPosition="+groupPosition+","+"childPosition"+childPosition);if (null == view){//获取LayoutInflaterLayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);//获取对应的布局view = layoutInflater.inflate(R.layout.memberlayout, null);}TextView textView = (TextView)view.findViewById(R.id.memberlayout_textView);textView.setText(arrayList_memberData.get(groupPosition).get(childPosition));if (selectChildItem == childPosition && selectParentItem == groupPosition){Log.i("++++++++++", "点击:"+groupPosition+","+childPosition);}return view;}@Overridepublic int getChildrenCount(int groupPosition) {// TODO Auto-generated method stubreturn arrayList_memberData.get(groupPosition).size();}@Overridepublic Object getGroup(int groupPosition) {// TODO Auto-generated method stubreturn arrayList_groupData.get(groupPosition);}@Overridepublic int getGroupCount() {// TODO Auto-generated method stubreturn arrayList_groupData.size();}@Overridepublic long getGroupId(int groupPosition) {// TODO Auto-generated method stubreturn groupPosition;}@Overridepublic View getGroupView(int groupPosition, boolean isExpanded, View convertView,ViewGroup parent) {// TODO Auto-generated method stubView view = convertView;Log.i("++++++++++", "groupPosition="+groupPosition);if (null == view){LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);view = layoutInflater.inflate(R.layout.grouplayout, null);}TextView textView = (TextView)view.findViewById(R.id.grouplayout_textView);textView.setText(arrayList_groupData.get(groupPosition));ImageView image=(ImageView) view.findViewById(R.id.grouplayout_imageView_tubiao);if(isExpanded){image.setBackgroundResource(R.drawable.btn_browser2);}else {image.setBackgroundResource(R.drawable.btn_browser);}return view;}@Overridepublic boolean hasStableIds() {// TODO Auto-generated method stubreturn true;}@Overridepublic boolean isChildSelectable(int groupPosition, int childPosition) {// TODO Auto-generated method stubreturn true;}        }    }






更多相关文章

  1. 在EditText中限制输入内容的长度
  2. Android开发之布局简介(1)
  3. Android定时闹钟与定时情景模式
  4. android ImageView实现上面圆角下面直角(xml实现方法)
  5. 你需要了解下Android(安卓)View的更新requestLayout与重绘invali
  6. FrameLayout的android:layout_height属性设置为dimen文件中的参
  7. Error:(27, 13) Failed to resolve: com.android.support.constr
  8. Android(安卓)版本列表
  9. TextView设置autoLink

随机推荐

  1. Android开发方式分析
  2. android 百度地图 定位获取位置失败 62错
  3. 找不到android系统库的解决方案
  4. Android仿微信底部按钮滑动变色
  5. Android(安卓)studio 解决各种错误
  6. Android Listener侦听的N种写法
  7. android Shader类简介_渲染图像
  8. 深入ListView
  9. Android(安卓)Eclipse 闪退问题
  10. 异常 报错 Android library projects can