仿QQ好友收起展开界面

来源:互联网 发布:数据库管理员 证书 编辑:程序博客网 时间:2024/05/23 11:57

最近做一个项目,想根据公司部门查看公司里面的成员,相当于QQ好友列表,可以展开,可以收起来,以前见过ExpandableListView的使用没太注意,现在重新学习一下!

目录:

一、简单ExpandableListView使用

主布局activity_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" >    <ExpandableListView         android:id="@id/android:list"        android:layout_width="fill_parent"        android:layout_height="fill_parent"        ></ExpandableListView></LinearLayout>

一级列表groups.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/textGroup"        android:layout_width="match_parent"        android:layout_height="match_parent"        android:paddingLeft="40dp"        android:paddingTop="6dp"        android:paddingBottom="6dp"        android:textSize="25dp"        android:text="暂无数据"        /></LinearLayout>

二级列表childs.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/textChild"        android:layout_width="match_parent"        android:layout_height="match_parent"        android:paddingLeft="60dp"        android:paddingTop="10dp"        android:paddingBottom="10dp"        android:textSize="20dp"        android:text="暂无数据"        /></LinearLayout>

MainActivity.java:

package com.example.demo3;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;import android.app.ExpandableListActivity;import android.os.Bundle;import android.view.View;import android.widget.ExpandableListView;import android.widget.SimpleExpandableListAdapter;import android.widget.Toast;public class MainActivity extends ExpandableListActivity  {        //创建一级条目容器    List<Map<String, String>> groups = new ArrayList<Map<String,String>>();    //存放内容,以便显示在列表中    List<List<Map<String, String>>> childs = new ArrayList<List<Map<String,String>>>();        @Override      public void onCreate(Bundle savedInstanceState) {          super.onCreate(savedInstanceState);          setContentView(R.layout.main);          setListData();      }          //设置列表内容    public void setListData(){        //创建几个一级条目标题        for (int i = 0; i < 2; i++) {            Map<String, String> title = new HashMap<String, String>();            title.put("group", "部门"+i);            groups.add(title);        }                //创建二级条目内容        //内容一:        List<Map<String, String>> childs_1 = new ArrayList<Map<String,String>>();        for (int i = 0; i < 3; i++) {            Map<String, String> title_1_content_1 = new HashMap<String, String>();            title_1_content_1.put("child", "部门一_成员 "+i);            childs_1.add(title_1_content_1);        }                // 内容二  :        List<Map<String, String>> childs_2 = new ArrayList<Map<String, String>>();          for (int i = 0; i < 4; i++) {            Map<String, String> title_2_content_1 = new HashMap<String, String>();             title_2_content_1.put("child", "部门二_成员  "+i);              childs_2.add(title_2_content_1);          }                childs.add(childs_1);          childs.add(childs_2);                 /*         * 创建ExpandableList的Adapter容器            * SimpleExpandableListAdapter adapter = new SimpleExpandableListAdapter(context,                                groupData, expandedGroupLayout, collapsedGroupLayout, groupFrom,                                 groupTo, childData, childLayout, childFrom, childTo)         * 参数:上下文、一级集合、一级xml文件、一级条目键值(Map集合的键)、         *     一级显示控件名(xml中的对应id)、二级集合、二级xml文件、二级天目键值(Map集合的键)、二级显示控件名(xml中的对应id)         * */        SimpleExpandableListAdapter sela = new SimpleExpandableListAdapter(                  MainActivity.this, groups, R.layout.groups, new String[] { "group" },                  new int[] { R.id.textGroup }, childs, R.layout.childs,                  new String[] { "child" }, new int[] { R.id.textChild });          // 加入列表          setListAdapter(sela);      }        //列表内容按下    @Override    public boolean onChildClick(ExpandableListView parent, View v,            int groupPosition, int childPosition, long id) {        Toast.makeText(MainActivity.this,"您选择了" + groups.get(groupPosition).toString() + "部门下的" + childs.get(groupPosition).get(childPosition)                                  .toString(), Toast.LENGTH_SHORT).show();          return super.onChildClick(parent, v, groupPosition, childPosition, id);    }    /**      * 二级标题按下      */      @Override      public boolean setSelectedChild(int groupPosition, int childPosition,              boolean shouldExpandGroup) {          return super.setSelectedChild(groupPosition, childPosition,                  shouldExpandGroup);      }        /**      * 一级标题按下      */      @Override      public void setSelectedGroup(int groupPosition) {          super.setSelectedGroup(groupPosition);      }  }

效果图:


二、更换列表图标

新建选择器:change.xml

<?xml version="1.0" encoding="utf-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android" >    <item android:state_expanded = "true" android:drawable = "@drawable/ic_action_arrow_right"/>           <item android:drawable = "@drawable/ic_action_arrow_bottom"/></selector>

修改主布局: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" >            <!-- 情况二 -->    <ExpandableListView          android:id="@id/android:list"         android:layout_width="fill_parent"         android:layout_height="fill_parent"         android:groupIndicator="@drawable/change"      ></ExpandableListView></LinearLayout>

效果图:

源码:http://download.csdn.net/download/fancheng614/9961675                                                                                                            


原创粉丝点击