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

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

2、该组件层次结构关系如下:

java.lang.Object

android.view.View

android.view.ViewGroup

androidwidget.AdapterView<Textendsandroid.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、创建布局文件

<?xmlversion="1.0"encoding="utf-8"?>

<LinearLayoutxmlns: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文件

packagexiao.fuyan.testapp;

importandroid.app.Activity;

importandroid.graphics.Color;

importandroid.os.Bundle;

importandroid.view.Gravity;

importandroid.view.View;

importandroid.view.ViewGroup;

importandroid.widget.AbsListView;

importandroid.widget.BaseExpandableListAdapter;

importandroid.widget.ExpandableListAdapter;

importandroid.widget.ExpandableListView;

importandroid.widget.ImageView;

importandroid.widget.LinearLayout;

importandroid.widget.TextView;

/**

*Createdbyxiaoon2017/1/6.

*/

publicclassExpandableActivityextendsActivity{

@Override

protectedvoidonCreate(BundlesavedInstanceState){

super.onCreate(savedInstanceState);

setContentView(R.layout.expandable_xml);

//

ExpandableListAdapteradapter=newBaseExpandableListAdapter(){

int[]logos=newint[]{R.mipmap.ic_launcher,R.mipmap.ic_launcher,R.mipmap.ic_launcher};

privateString[]generalsTypes=newString[]{"1","2","3"};

privateString[][]generals=newString[][]{

{"a","b","c","d"},

{"e","f","g","h"},

{"i","j","k","l"}

};

privateint[][]generalsLogos=newint[][]{

{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}

};

TextViewgetTextView(){

AbsListView.LayoutParamslp=newAbsListView.LayoutParams(

ViewGroup.LayoutParams.FILL_PARENT,64

);

TextViewtextView=newTextView(ExpandableActivity.this);

textView.setLayoutParams(lp);

textView.setGravity(Gravity.CENTER_VERTICAL);

textView.setPadding(36,0,0,0);

textView.setTextSize(16);

textView.setTextColor(Color.BLACK);

returntextView;

}

@Override

publicintgetGroupCount(){

returngeneralsTypes.length;

}

@Override

publicintgetChildrenCount(intgroupPosition){

returngenerals[groupPosition].length;

}

@Override

publicObjectgetGroup(intgroupPosition){

returngeneralsTypes[groupPosition];

}

@Override

publicObjectgetChild(intgroupPosition,intchildPosition){

returngenerals[groupPosition][childPosition];

}

@Override

publiclonggetGroupId(intgroupPosition){

returngroupPosition;

}

@Override

publiclonggetChildId(intgroupPosition,intchildPosition){

returnchildPosition;

}

@Override

publicbooleanhasStableIds(){

returntrue;

}

@Override

publicViewgetGroupView(intgroupPosition,booleanisExpanded,ViewconvertView,ViewGroupparent){

LinearLayoutll=newLinearLayout(ExpandableActivity.this);

ll.setOrientation(LinearLayout.HORIZONTAL);

ImageViewlogo=newImageView(ExpandableActivity.this);

logo.setImageResource(logos[groupPosition]);

logo.setPadding(20,0,0,0);

ll.addView(logo);

TextViewtextView=getTextView();

textView.setTextColor(Color.BLACK);

textView.setText(getGroup(groupPosition).toString());

ll.addView(textView);

returnll;

}

@Override

publicViewgetChildView(intgroupPosition,intchildPosition,booleanisLastChild,ViewconvertView,ViewGroupparent){

LinearLayoutll=newLinearLayout(ExpandableActivity.this);

ll.setOrientation(LinearLayout.HORIZONTAL);

ImageViewgeneralLogo=newImageView(ExpandableActivity.this);

generalLogo.setImageResource(generalsLogos[groupPosition][childPosition]);

ll.addView(generalLogo);

TextViewtextView=getTextView();

textView.setText(getChild(groupPosition,childPosition).toString());

ll.addView(textView);

returnll;

}

@Override

publicbooleanisChildSelectable(intgroupPosition,intchildPosition){

returntrue;

}

};

ExpandableListViewexpandableListView=(ExpandableListView)

findViewById(R.id.list);

expandableListView.setAdapter(adapter);

}

}

相关推荐