1、用法类似ListView,同样是在布局文件中先定义出此控件,ExpandableListVIew
<?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/expandable"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#ffffff"
android:cacheColorHint="#FFFFFF"
android:listSelector="#FFFFFF">
</ExpandableListView>
</LinearLayout>
简单说下的是 android:cacheColorHint="#00000000",这个设置可以去除拖动view时背景变成黑色的效果
android:listSelector="#00000000" ,可以去除选中时的黄色底色
2、自定义ExapandableListView的适配器,继承BaseExpandableListViewAdapter类,并重写相关方法。
主要是我们在getChildView和getGroupView中进行设计我们现实Group和它子项的模式。然后绑定适配器,同时可以监听控件的事件。
<span style="font-size:12px;">package com.drision.test;
import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.AbsListView;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.Toast;
import android.widget.ExpandableListView.OnChildClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
public class ExpandableListViewActivity extends Activity {
private ExpandableListView expandable;
private ExpandableAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
this.setContentView(R.layout.expandable_activity);
expandable = (ExpandableListView) this.findViewById(R.id.expandable);
adapter = new ExpandableAdapter();
expandable.setAdapter(adapter);
expandable.setOnChildClickListener(new OnChildClickListener(){
@Override
public boolean onChildClick(ExpandableListView parent, View v,
int groupPosition, int childPosition, long id) {
Toast.makeText(ExpandableListViewActivity.this, "你点击了" + adapter.getChild(groupPosition, childPosition),
Toast.LENGTH_SHORT).show();
return false;
}
});
}
class ExpandableAdapter extends BaseExpandableListAdapter{
//设置组的图片
private int [] groupImage = new int []{R.drawable.lock_icon,R.drawable.lock_icon_2};
//设置组视图的文字
private String [] groupName = new String[]{"我的好友","陌生人"};
//设置子视图的图片
private int [][] childImage = new int [][]{{R.drawable.woyao_add},{R.drawable.xietongapp}};
//设置子视图的标题
private String [][] childName = new String [][]{{"张三"},{"李斯"}};
private Context _this = ExpandableListViewActivity.this;
TextView getTextView() {
AbsListView.LayoutParams lp = new AbsListView.LayoutParams(
ViewGroup.LayoutParams.FILL_PARENT, 64);
TextView textView = new TextView(
_this);
textView.setLayoutParams(lp);
textView.setGravity(Gravity.CENTER_VERTICAL);
textView.setPadding(36, 0, 0, 0);
textView.setTextSize(20);
textView.setTextColor(Color.BLACK);
return textView;
}
@Override
public Object getChild(int groupPosition, int childPosition) {
return childName[groupPosition][childPosition];
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
@Override
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
LinearLayout ll = new LinearLayout(_this);
ll.setOrientation(0);
ImageView iv = new ImageView(_this);
iv.setImageResource(childImage[groupPosition][childPosition]);
ll.addView(iv);
TextView textView = getTextView();
textView.setText(getChild(groupPosition, childPosition)
.toString());
ll.addView(textView);
return ll;
}
@Override
public int getChildrenCount(int groupPosition) {
return childName[groupPosition].length;
}
@Override
public Object getGroup(int groupPosition) {
return groupName[groupPosition];
}
@Override
public int getGroupCount() {
return groupName.length;
}
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
// 创建一个布局,然后进行图片和文字的显示
LinearLayout linear = new LinearLayout(_this);
linear.setOrientation(0);
ImageView iv = new ImageView(_this);
iv.setImageResource(groupImage[groupPosition]);
iv.setPadding(50, 0, 0, 0);
linear.addView(iv);
TextView textView = getTextView();
textView.setTextColor(Color.BLACK);
textView.setText(getGroup(groupPosition).toString());
linear.addView(textView);
return linear;
}
@Override
public boolean hasStableIds() {
return true;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}
}
</span>