• Android开发系列(八) ExpandableListView的使用方法


      前几天曾经写过ListView的使用方法,并且设计了一个QQ联系人列表。今天在此基础上讲一下ExpandableListView的用法,与之前一样,这里分成两节,首先讲使用SimpleExpandableListAdapter的使用,后面如果搜集到足够的材料会讲一下自定义适配器的ExpandableListView的使用,到时候,会把之前的QQ联系人列表进行一下升级。

      

      与ListView类似,首先是xml布局文件,布局文件要有三个,一个是放置ExpandableListView的主布局文件,一个是一级条目的布局文件,另一个是二级条目(即子菜单)的布局文件。

    main.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" >
        <!-- 注意这里的id还是系统自带的list -->
        <ExpandableListView 
            android:id="@android:id/list"               
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>
        
    
    </LinearLayout>

    group_layout.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" >
        
        
        <TextView 
            android:id="@+id/myGroup"
            android:layout_width="match_parent"
            android:layout_height="50dip"
            android:paddingLeft="50dip"
            android:textSize="12pt"
            android:gravity="center_vertical"/>
        
    
    </LinearLayout>

    child_layout.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" >
        
        <TextView 
            android:id="@+id/myChild"
            android:layout_width="match_parent"
            android:layout_height="40dip"
            android:paddingLeft="70dip"
            android:textSize="10pt"
            android:gravity="center_vertical"/>
        
    
    </LinearLayout>

    然后就设置一下MainActivity类就可以了,这里MainActivity类需要继承 ExpandableListView类

    package com.example.android_expandablelistview;
    
    import java.util.*;
    
    import android.os.Bundle;
    import android.app.Activity;
    import android.app.ExpandableListActivity;
    import android.view.Menu;
    import android.widget.SimpleExpandableListAdapter;
    
    public class MainActivity extends ExpandableListActivity {
        
        
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            
            //设置一级条目的数据
            String[] keysOfGroup={"groupName"};
            String[] valuesOfGroup={"group1","group2"};
            ArrayList<HashMap<String,String>> listOfGroup=getDataList(keysOfGroup, valuesOfGroup);
            
            //设置二级条目的数据,一级条目有几个,就要创建几个二级条目的list
            String[] keysOfChild={"childName"};  
            //因为不同的一级条目内的子条目对应的键名应该是统一的,所以这里不再单独为每一个子条目设计键名,而是用二级条目共有的键名
            //group1的子条目的Value值
            String[] valuesOfChild1={"child1Data1","child1Data2"};
            //group2的子条目的Value值
            String[] valuesOfChild2={"child2Data1"};
            
            ArrayList<HashMap<String,String>> listOfChild1=getDataList(keysOfChild, valuesOfChild1);
            ArrayList<HashMap<String,String>> listOfChild2=getDataList(keysOfChild, valuesOfChild2);
            
            //还要定义一个List对象,将所有的二级条目对应的List按照与group相对应的顺序装进一个list中
            ArrayList<ArrayList<HashMap<String,String>>> childs=new ArrayList<ArrayList<HashMap<String,String>>> ();
            childs.add(listOfChild1);
            childs.add(listOfChild2);
            
            //创建SimpleExpandableListAdapter对象,这里用的是最简单的那个构造方法!
            SimpleExpandableListAdapter adapter=new SimpleExpandableListAdapter(this, listOfGroup,
                    R.layout.group_layout, keysOfGroup,new int[]{R.id.myGroup},
                    childs,R.layout.child_layout,keysOfChild,new int[]{R.id.myChild});
            
            setListAdapter(adapter);
            
            
        }
        
        //构造一个能够返回List<Map<String,String>>对象的方法
        /**
         * 
         * @param keys  List中Map的键名,
         * @param values  一个List中包含的全部Item的值,因为每一个Item又对应多个键名,所以使用了一个二维Vector的形式
         * @return  list
         */
        private ArrayList<HashMap<String,String>> getDataList(String[] keys, String[]values){
            ArrayList<HashMap<String,String>> list=new ArrayList<HashMap<String,String>>();
            for(int i=0; i<values.length/keys.length; i++){
                HashMap<String,String> map=new HashMap<String,String>();
                for(int j=0;j<keys.length; j++ ){
                    map.put(keys[j], values[i*keys.length+j]);
                }
                list.add(map);
            }
        
            return list;
        }
        
        
        
    
    
    
    }

    效果图:

  • 相关阅读:
    SQL群集多实例卸载、安装
    Java 数组的 12 个方法
    Java 容器
    RDA 升级
    DVB条件接收系统多密技术的设计与实现
    perl数组的长度与元素个数
    TS流解析 四
    Python中操作myslq的方法
    DVB-subtitle解析流程浅
    JAVA回调机制(CallBack)详解
  • 原文地址:https://www.cnblogs.com/carlos-vic/p/Carlos_V_Android_8.html
Copyright © 2020-2023  润新知