ExpandableListVivew是ListView的子类,它在普通ListView的基础上进行了扩展,它把应用中的列表项分为几组,每组里又可包含多个列表项。ExpandableListVivew的用法与普通ListView的用法非常相似,只是ExpandableListVivew显示的列表项应该由ExpandableAdapter提供。
ExpandableListVivew的代码实现:
在XML文件:
先建一个ExpandableListView XML文件
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/main_bg"
>
<ExpandableListView
android:id="@+id/main_expandableListView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
</LinearLayout>
再建一个ExpandableAdapter元素的XML文件
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:id="@+id/main_activity_account_layout_textview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:textColor="#000000"
android:textSize="8pt"/>
</LinearLayout>
在java文件中:
private ExpandableListView expandListView;
private ExpandableListAdapter adapter;
private ExpandableListContextMenuInfo info;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
/**实例化ExpandableListView 对象*/ expandListView=(ExpandableListView)findViewById(R.id.main_expandableListView);
/**一个baseExpandableListAdapter适配器*/
adapter=new BaseExpandableListAdapter() {
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
@Override
public boolean hasStableIds() {
return true;
}
@Override/**获得groupView*/
public View getGroupView(final int groupPosition, boolean isExpanded,View convertView, ViewGroup parent) {
LayoutInflater inflater=LayoutInflater.from(context);
View view=
inflater.inflate(R.layout.main_activity_category_layout,null);
return view;
}
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
@Override
public int getGroupCount() {
return list_category.size();
}
@Override
public Object getGroup(int groupPosition) {
Category cg=list_category.get(groupPosition);
return cg.getName();
}
@Override
public int getChildrenCount(int groupPosition) {
Category cg=list_category.get(groupPosition);
list_account=cdao.deepList_LoadAccountName(context, cg.getId());
return list_account.size();
}
@Override/**获得chileView*/
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
LayoutInflater inflater=LayoutInflater.from(context);
View view=inflater.inflate(R.layout.main_activiyt_account_layout, null);
TextView textview=(TextView)view.findViewById(R.id.main_activity_account_layout_textview);
return view;
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
@Override
public Object getChild(int groupPosition, int childPosition) {
Category cg=list_category.get(groupPosition);
list_account=cdao.deepList_LoadAccountName(context,cg.getId());
Account ac=list_account.get(childPosition);
return ac.getName();
}
@Override
public void notifyDataSetChanged() {
super.notifyDataSetChanged();
}
};
expandListView.setAdapter(adapter);
//注册一个上下文菜单
registerForContextMenu(expandListView);
//对childView进行点击监听
expandListView.setOnChildClickListener(new
ExpandableListView.OnChildClickListener() {
@Override
public boolean onChildClick(ExpandableListView parent, View v,int groupPosition, int childPosition, long id) {
return true;
}
});
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
/**获得长按对象的所有信息*/
info=(ExpandableListContextMenuInfo)menuInfo;
//得到父位置和子位置
System.out.println(ExpandableListView.getPackedPositionGroup(info.packedPosition)+"->father");
System.out.println(ExpandableListView.getPackedPositionChild(info.packedPosition)+"->child");
/**获得长按的类型,父型或子型*/
int type=ExpandableListView.getPackedPositionType(info.packedPosition);
refreshCategoryData();
if(type==ExpandableListView.PACKED_POSITION_TYPE_GROUP){menu.add(1,CATEGORY_UPDATE,1,R.string.category_update_title);
menu.add(1,CATEGORY_DELETE,2,R.string.category_delete_title);
}else if(type==ExpandableListView.PACKED_POSITION_TYPE_CHILD){
menu.add(1,ACCOUNT_UPDATE,1,R.string.account_update_title);
menu.add(1,ACCOUNT_DELETE,2,R.string.account_delete_title);
}
super.onCreateContextMenu(menu, v, menuInfo);
}
Android中替换ExpandableListView控件前面的箭头图标 首先,自定义一个expandablelistviewselector.xml文件放到layout,具体内容如下:
<?xml version="1.0" encoding="utf-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android"><itemandroid:state_expanded="true"
//down存放已有下拉时的图片
android:drawable="@drawable/expandablelistviewindicatordown" />
//存放还没有下拉时的图片<item android:drawable="@drawable/expandablelistviewindicator" /></selector>
其次,在程序的主体部分编写类似如下的代码:
expandList.setGroupIndicator(this.getResources().getDrawable(R.layout.expandablelistviewselector));