`
肖福燕
  • 浏览: 11826 次
  • 性别: Icon_minigender_1
  • 来自: 成都
社区版块
存档分类
最新评论

Android开发高级组件--ExpandableListView(可伸展的列表组件)

阅读更多
1、在Android开发中,有时候希望对列表项可以分组管理并实现收缩功能,例如QQ在使用时,有“我的好友”、“家人”、“同学”等分组,单击其中一项会展开,再单击一次又缩回去。要实现这种功能,就得使用到我们今天的主角ExpandableListView组件了。

2、该组件层次结构关系如下:
   java.lang.Object
      android.view.View
         android.view.ViewGroup
            android widget.AdapterView<T extends android.widget.Adapter>
               android.widget.AbsListView
                  android.widget.ListView
                     android.widget.ExpandableListView

3、每一个可扩展项旁边都有一个提示符(箭头等)用来说明该列表项目前的状态,可以使用方法:setChildIndicator(Drawable)和setGroupIndicator(Drawable)(或相应的XML文件的属性)去设置这些提示符的样式。注意:在XML布局文件中,一般不对ExpandableListView的android:layout_height属性使用wrap_content,否则可能会报错。
  与ListView一样ExpandableListView也需要一个适配器做桥梁来提供数据,ExpandableListView是一个垂直滚动显示两级列表项,它可以有两层,每层都能够独立的展开并显示其子项。BaseExpandableListAdapter是一个用在ExpandableListView组件的适配器。

4、创建布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:textSize="24sp"
        android:gravity="center"
        android:text="花名册"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />

    <ExpandableListView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:id="@+id/list"
        android:background="#abcdef"/>

</LinearLayout>

5、修改ExpandableActivity.java文件
package xiao.fuyan.testapp;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

/**
* Created by xiao on 2017/1/6.
*/
public class ExpandableActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.expandable_xml);

        //
        ExpandableListAdapter adapter = new BaseExpandableListAdapter() {
            int[] logos = new int[]{R.mipmap.ic_launcher, R.mipmap.ic_launcher, R.mipmap.ic_launcher};
            private String[] generalsTypes = new String[]{"1", "2", "3"};

            private String[][] generals = new String[][]{
                    {"a", "b", "c", "d"},
                    {"e", "f", "g", "h"},
                    {"i", "j", "k", "l"}
            };
            private int[][] generalsLogos = new int[][]{
                    {R.mipmap.ic_launcher, R.mipmap.ic_launcher, R.mipmap.ic_launcher, R.mipmap.ic_launcher},
                    {R.mipmap.ic_launcher, R.mipmap.ic_launcher, R.mipmap.ic_launcher, R.mipmap.ic_launcher},
                    {R.mipmap.ic_launcher, R.mipmap.ic_launcher, R.mipmap.ic_launcher, R.mipmap.ic_launcher}
            };

            TextView getTextView(){
                AbsListView.LayoutParams lp = new AbsListView.LayoutParams(
                    ViewGroup.LayoutParams.FILL_PARENT, 64
                );
                TextView textView = new TextView(ExpandableActivity.this);
                textView.setLayoutParams(lp);
                textView.setGravity(Gravity.CENTER_VERTICAL);
                textView.setPadding(36, 0, 0, 0);
                textView.setTextSize(16);
                textView.setTextColor(Color.BLACK);
                return textView;
            }

            @Override
            public int getGroupCount() {
                return generalsTypes.length;
            }

            @Override
            public int getChildrenCount(int groupPosition) {
                return generals[groupPosition].length;
            }

            @Override
            public Object getGroup(int groupPosition) {
                return generalsTypes[groupPosition];
            }

            @Override
            public Object getChild(int groupPosition, int childPosition) {
                return generals[groupPosition][childPosition];
            }

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

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

            @Override
            public boolean hasStableIds() {
                return true;
            }

            @Override
            public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
                LinearLayout ll = new LinearLayout(ExpandableActivity.this);
                ll.setOrientation(LinearLayout.HORIZONTAL);
                ImageView logo = new ImageView(ExpandableActivity.this);
                logo.setImageResource(logos[groupPosition]);
                logo.setPadding(20, 0, 0, 0);
                ll.addView(logo);
                TextView textView = getTextView();
                textView.setTextColor(Color.BLACK);
                textView.setText(getGroup(groupPosition).toString());
                ll.addView(textView);
                return ll;
            }

            @Override
            public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
                LinearLayout ll = new LinearLayout(ExpandableActivity.this);
                ll.setOrientation(LinearLayout.HORIZONTAL);
                ImageView generalLogo = new ImageView(ExpandableActivity.this);
                generalLogo.setImageResource(generalsLogos[groupPosition][childPosition]);
                ll.addView(generalLogo);
                TextView textView = getTextView();
                textView.setText(getChild(groupPosition, childPosition).toString());
                ll.addView(textView);
                return ll;
            }

            @Override
            public boolean isChildSelectable(int groupPosition, int childPosition) {
                return true;
            }
        };

        ExpandableListView expandableListView = (ExpandableListView)
                findViewById(R.id.list);
        expandableListView.setAdapter(adapter);


    }
}


分享到:
评论

相关推荐

    Android高级应用源码-一个ExpandableListView的例子,实现多级菜单分类展示.zip

    总之,通过分析`Android高级应用源码-一个ExpandableListView的例子,实现多级菜单分类展示.zip`中的源码,开发者可以学习到如何在Android应用程序中创建具有层次结构的交互式列表,这对于构建如导航菜单、目录树等...

    Android项目实战--手机卫士33--ExpandableListView的使用

    总的来说,`ExpandableListView`在Android开发中是一个强大且灵活的组件,尤其适用于显示有层次结构的数据。通过理解和熟练运用,开发者可以为用户提供更直观、易用的界面,提升应用的用户体验。在手机卫士项目中,...

    Android开发实战经典-020719树型组件:ExpandableListView源代码和视频教程.zip

    Android开发实战经典_020719树型组件:ExpandableListView源代码和视频教程.zip

    Android代码-基于自定义适配器的ExpandableListView源码.zip

    在Android开发中,`ExpandableListView`是一种可扩展的列表视图,允许用户折叠和展开组项以显示或隐藏子项。这个`Android代码-基于自定义适配器的ExpandableListView源码.zip`文件包含了一个示例项目,演示了如何...

    android--ExpandableListView实例

    在Android开发中,`ExpandableListView`是一种常用的控件,它允许用户展开和折叠分组,通常用于展示具有层次结构的数据。在这个实例中,我们将深入探讨`ExpandableListView`的使用,包括其基本概念、设置数据、事件...

    Android代码-基于ExpandableListView实现评论和回复的功能。

    CommentWithReplyView-master 基于ExpandableListView实现评论和回复的功能。 &gt; 说明 ...处理了NestedScrollView、ExpandableListView和CoordinatorLayout的嵌套问题 点击某条评论,即可@ta进行回复

    Android开发丶ExpandableListView实现可展开列表

    在Android开发中,`ExpandableListView`是一种非常实用的列表控件,它允许用户通过点击条目来展开或折叠子列表。这个控件是`ListView`的扩展,提供了更丰富的交互性和展示层次结构数据的能力。在本文中,我们将深入...

    安卓开发-一个ExpandableListView的例子,实现多级菜单分类展示.zip

    在Android应用开发中,`ExpandableListView`是一个非常实用的组件,它允许开发者创建具有可展开和折叠子项的列表视图,常用于构建多级菜单或者分类展示数据。在这个例子中,我们将深入探讨如何使用`...

    【Android】ExpandableListView二级列表效果

    在Android开发中,`ExpandableListView`是一种常用的控件,用于展示可以展开和折叠的列表,通常用于构建具有层级结构的数据展示。这个控件非常适合处理像菜单、目录或树状结构的数据,例如“二级列表”场景。下面...

    android ExpandableListView三级菜单的使用

    在Android开发中,`ExpandableListView`是一种常用的控件,它可以展示可展开和折叠的列表,通常用于构建具有层级结构的数据展示。在这个“android ExpandableListView三级菜单的使用”示例中,我们将深入探讨如何...

    安卓listview相关相关-一个ExpandableListView的例子实现多级菜单分类展示.rar

    在Android开发中,ListView是一种常用的组件,用于展示可滚动的列表数据。然而,当需要实现更复杂的层次结构,比如多级菜单分类时,ListView就显得力不从心了。这时,我们通常会使用ExpandableListView。这个压缩包...

    Android-使用ExpandableListView实现的购物车

    在Android应用开发中,`ExpandableListView`是一个非常实用的组件,它允许用户展示一个可展开和折叠的列表,常用于构建层次结构清晰的数据展示,例如在本例中被用作购物车的实现。购物车通常包含多个商品,每个商品...

    Android常用控件(能折叠的ListView)--ExpandableListView的使用模仿QQ好友列表

    本人博客:Android常用控件(能折叠的ListView)--ExpandableListView的使用模仿QQ好友列表的demo

    Android-用ListView模仿ExpandableListView

    在Android开发中,ListView是一种常用的组件,用于展示可滚动的列表数据。然而,有时我们希望实现更复杂的列表,比如带有可折叠子项的列表,这时ExpandableListView就派上用场了。不过,如果我们只需要使用ListView...

    Android-基于ExpandableListView实现评论和回复的功能

    在Android应用开发中,`ExpandableListView`是一个非常实用的组件,它允许用户展示具有层级结构的数据,非常适合用于实现评论和回复的展示功能。在这个项目中,“Android-基于ExpandableListView实现评论和回复的...

    Drag-Drop-ExpandableListView

    总的来说,`Drag-Drop-ExpandableListView`是一个优秀的开源项目,它扩展了Android标准组件的功能,让开发者能够轻松实现拖放排序的可扩展列表视图。通过深入理解和应用这个项目,开发者可以提升应用的用户体验,...

    可以扩展的listview--expandablelistview

    在Android开发中,`ExpandableListView`是一种非常实用的视图组件,它允许用户通过点击条目来展开或折叠子列表。这个控件通常用于显示层次结构的数据,比如分类目录、菜单或者像手机QQ分组这样的场景。在这个“可以...

    ExpandableListView实时更新UI数据

    在Android开发中,`ExpandableListView`是一种可扩展的列表视图,允许用户折叠和展开组,每个组下可以有多个子项。这种组件在显示层次结构数据时非常有用,例如菜单、目录或树状结构的数据。本篇将详细讲解如何在`...

    换种思路解决列表套列表-ExpandableListView的使用

    `ExpandableListView`是Android SDK提供的一种可扩展的列表视图,它可以显示一个父列表项,每个父列表项下还可以展开展示子列表项,这样就能清晰地展现层级关系。 1. **ExpandableListView的基本使用** - 初始化:...

Global site tag (gtag.js) - Google Analytics