您的位置:首页 > 移动开发 > Android开发

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

2017-03-28 10:36 483 查看
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);

    }

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: