ExpandableListView(QQ好友列表)

ExpandableListView,就是可折叠的列表,它是ListView的子类, 在ListView的基础上它把应用中的列表项分为几组,每组里又可包含多个列表项。

ExpandableListView用法和Listview差不多,设置适配器,适配器里面设置数据。

1.我们先在控制器的布局文件中添加一个ExpandableListView







2.ExpandableListView中分为组头和组里的每一个item,所以需要两个布局文件,相互对应。
设置组头的布局文件group_item.xml




    


设置每一组的item布局文件list_item.xml




    

    
    

回到控制器中
获取到ExpandableListView

        expandableListView = (ExpandableListView) findViewById(R.id.exlistview);

设置适配器

         expandableListView.setAdapter(new MyExpandableListView());// 设置适配器

自定义适配器

//为ExpandableListView自定义适配器
    class MyExpandableListView extends BaseExpandableListAdapter {

        //返回一级列表的个数
        @Override
        public int getGroupCount() {
            return groups.length;
        }

        //返回每个二级列表的个数
        @Override
        public int getChildrenCount(int groupPosition) { //参数groupPosition表示第几个一级列表
            ArrayList list = childs.get(groupPosition);
            return list.size();
//            return childs[groupPosition].length;
        }

        //返回一级列表的单个item(返回的是对象)
        @Override
        public Object getGroup(int groupPosition) {
            return groups[groupPosition];
        }

        //返回二级列表中的单个item(返回的是对象)
        @Override
        public Object getChild(int groupPosition, int childPosition) {
//            return childs[groupPosition][childPosition];  //不要误写成groups[groupPosition][childPosition]
            ArrayList list = childs.get(groupPosition);
            return list.get(childPosition);
        }

        @Override
        public long getGroupId(int groupPosition) {
            return groupPosition;
        }

        @Override
        public long getChildId(int groupPosition, int childPosition) {
            return childPosition;
        }

        //每个item的id是否是固定?一般为true
        @Override
        public boolean hasStableIds() {
            return true;
        }

        //【重要】填充一级列表
        @Override
        public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {

            if (convertView == null) {
                convertView = getLayoutInflater().inflate(R.layout.group_item, null);
            } else {

            }
            TextView tv_group = (TextView) convertView.findViewById(R.id.tv_group_name);
            tv_group.setText(groups[groupPosition]);
            return convertView;
        }

        //【重要】填充二级列表
        @Override
        public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {

            if (convertView == null) {
                convertView = getLayoutInflater().inflate(R.layout.list_item, null);
            }

            ImageView iv_child = (ImageView) convertView.findViewById(R.id.img_icon);
            TextView tv_child = (TextView) convertView.findViewById(R.id.tv_name);

            ArrayList list = childs.get(groupPosition);
            ItemModel model = (ItemModel)list.get(childPosition);
            iv_child.setImageResource(model.getiId());
            tv_child.setText(model.getiName());

//            tv_child.setText(childs[groupPosition][childPosition]);

            return convertView;
        }

        //二级列表中的item是否能够被选中?
        @Override
        public boolean isChildSelectable(int groupPosition, int childPosition) {
            return true;
        }
    }

设置每一组是否展开:.expandGroup()
全部展开:

for (int i = 0;i

// 设置组头箭头为空

expandableListView.setGroupIndicator(null);

完整Demo

你可能感兴趣的:(ExpandableListView(QQ好友列表))