ExpandableListActivity可以展开或隐藏子项。用getExpandableListView().setIndicatorBounds(left, right)可以用来改变指示图标的位置,在getGroupView可以重写成你想要的显示效果,本程序实现在父项实现图文混排的效果。
setListAdapter(mAdapter);
registerForContextMenu(getExpandableListView());
getExpandableListView().setIndicatorBounds(260, 300); //设置指示图标的位置
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
// menu.setHeaderTitle("Sample menu");
// menu.add(0, 0, 0, R.string.expandable_list_sample_action);
}
@Override
public boolean onContextItemSelected(MenuItem item) {
ExpandableListContextMenuInfo info = (ExpandableListContextMenuInfo) item.getMenuInfo();
String title = ((TextView) info.targetView).getText().toString();
int type = ExpandableListView.getPackedPositionType(info.packedPosition);
if (type == ExpandableListView.PACKED_POSITION_TYPE_CHILD) {
int groupPos = ExpandableListView.getPackedPositionGroup(info.packedPosition);
int childPos = ExpandableListView.getPackedPositionChild(info.packedPosition);
Toast.makeText(this, title + ": Child " + childPos + " clicked in group " + groupPos,
Toast.LENGTH_SHORT).show();
return true;
} else if (type == ExpandableListView.PACKED_POSITION_TYPE_GROUP) {
int groupPos = ExpandableListView.getPackedPositionGroup(info.packedPosition);
Toast.makeText(this, title + ": Group " + groupPos + " clicked", Toast.LENGTH_SHORT).show();
return true;
}
return false;
}
/**
* A simple adapter which maintains an ArrayList of photo resource Ids.
* Each photo is displayed as an image. This adapter supports clearing the
* list of photos and adding a new photo.
*
*/
public class MyExpandableListAdapter extends BaseExpandableListAdapter {
// Sample data set. children[i] contains the children (String[]) for groups[i].
ViewHolder viewHolder;
private LayoutInflater mInflater;
private String[] groups = { "美食", "购物", "出行", "玩乐" };
private String[][] children = {
{ "粤菜", "湘菜", "川菜", "火锅", "小吃" },
{ "Ace", "Bandit", "Cha-Cha", "Deuce" },
{ "Fluffy", "Snuggles" },
{ "Goldy", "Bubbles" }
};
public MyExpandableListAdapter(Context context){
mInflater = LayoutInflater.from(context);
}
public Object getChild(int groupPosition, int childPosition) {
return children[groupPosition][childPosition];
}
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
public int getChildrenCount(int groupPosition) {
return children[groupPosition].length;
}
public TextView getGenericView() {
// Layout parameters for the ExpandableListView
AbsListView.LayoutParams lp = new AbsListView.LayoutParams(
ViewGroup.LayoutParams.FILL_PARENT, 64);
TextView textView = new TextView(DiscountSearch.this);
textView.setLayoutParams(lp);
// Center the text vertically
textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
// Set the text starting position
textView.setPadding(36, 0, 0, 0);
return textView;
}
public View getChildView(int groupPosition, int childPosition, boolean isLastChild,
View convertView, ViewGroup parent) {
TextView textView = getGenericView();
textView.setText(getChild(groupPosition, childPosition).toString());
return textView;
}
public Object getGroup(int groupPosition) {
return groups[groupPosition];
}
public int getGroupCount() {
return groups.length;
}
public long getGroupId(int groupPosition) {
return groupPosition;
}
public View getGroupView(int groupPosition, boolean isExpanded, View convertView,
ViewGroup parent) {
if(convertView == null){
convertView = mInflater.inflate(R.layout.shop_search, null);
convertView.setId(R.layout.shop_search);
viewHolder = new ViewHolder();
viewHolder.shopType = (TextView)convertView.findViewById(R.id.simple_list_item_icon_tv);
viewHolder.icon = (ImageView)convertView.findViewById(R.id.simple_list_item_icon_iv);
convertView.setTag(viewHolder);
}else{
viewHolder = (ViewHolder)convertView.getTag();
}
viewHolder.shopType.setText(getGroup(groupPosition).toString());
viewHolder.icon.setImageBitmap(mShoppingIcon);
// TextView textView = getGenericView();
// textView.setText(getGroup(groupPosition).toString());
// return textView;
return convertView;
}
public class ViewHolder {
TextView shopType;
ImageView icon;
}
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
public boolean hasStableIds() {
return true;
}
}
shop_search.xml如下
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<LinearLayout android:id="@+id/dyna_list"
android:layout_width="40dip" android:layout_height="40dip"
android:layout_marginLeft="5dip" android:orientation="horizontal">
<ImageView android:id="@+id/simple_list_item_icon_iv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center">
</ImageView>
</LinearLayout>
<TextView android:id="@+id/simple_list_item_icon_tv"
android:layout_width="fill_parent" android:layout_height="40dip"
android:textColor="@drawable/black" android:textSize="12sp"
android:paddingLeft="8dip" android:gravity ="center_vertical"
android:singleLine="false" android:ellipsize="marquee">
</TextView>
</LinearLayout>