<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/ract_bg"
android:orientation="vertical" >
<ExpandableListView
android:id="@+id/listview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:childDivider="#885D7081"
android:choiceMode="singleChoice"
android:divider="#88455869"
android:dividerHeight="1px"
android:groupIndicator="@null"
android:listSelector="#00000000"
android:scrollingCache="false" />
</LinearLayout>
说明:需要定义android:listSelector="#00000000"关闭点击的效果,android:choiceMode="singleChoice"设为单选模式
charge_expandable_fragment_group_item.xml
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="60dp"
android:background="#88506475"
android:gravity="center_vertical"
android:minHeight="60dp"
android:orientation="horizontal" >
<ImageView
android:id="@+id/group_indicator"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:src="@drawable/expander_arrow_collapse" />
<TextView
android:id="@+id/group_textview"
style="@style/WhiteLargeNormalStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="left|center_vertical"
android:text="父元素" />
</LinearLayout>
charge_expandable_fragment_child_item.xml
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="80dp"
android:background="@drawable/selector_expandable_child_item"
android:gravity="center_vertical"
android:minHeight="80dp"
android:orientation="horizontal" >
<TextView
android:id="@+id/child_textview"
style="@style/WhiteLargeNormalStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_weight="1"
android:gravity="left|center_vertical"
android:text="子元素" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="5dp"
android:src="@drawable/arrow_right" />
</LinearLayout>
selector_expandable_child_item.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/child_item2" android:state_activated="true"/>
<item android:drawable="@drawable/child_item1"/>
</selector>
代码部分:
final private ExpandableListView.OnChildClickListener mOnClickListener = new ExpandableListView.OnChildClickListener() {
@Override
public boolean onChildClick(ExpandableListView parent, View v,
int groupPosition, int childPosition, long id) {
// 设置选中背景
setItemChecked(groupPosition, childPosition);
// TODO 执行选中后的操作
return true;
}
};
private void setItemChecked(int groupPosition, int childPosition) {
if (mListView == null) {
return;
}
this.mGroupPosition = groupPosition;
this.mChildPosition = childPosition;
int numberOfGroupThatIsOpened = 0;
for (int i = 0; i < groupPosition; i++) {
if (mListView.isGroupExpanded(i)) {
numberOfGroupThatIsOpened += mAdapter.getChildrenCount(i);
}
}
int position = numberOfGroupThatIsOpened + groupPosition
+ childPosition + 1;
System.out.println("groupPosition=" + groupPosition
+ ", childPosition=" + childPosition + ", position="
+ position + ", isItemChecked="
+ mListView.isItemChecked(position));
if (!mListView.isItemChecked(position)) {
mListView.setItemChecked(position, true);
}
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = mInflater.inflate(
R.layout.charge_expandable_fragment_group_item,
null);
}
ImageView indicator = (ImageView) convertView
.findViewById(R.id.group_indicator);
if (isExpanded) {
// 设置展开后的group标头图片
indicator
.setImageResource(R.drawable.expander_arrow_expand);
// 在折叠后再次展开,之前的选中会消失,此处需要设置一次
if (groupPosition == mGroupPosition) {
setItemChecked(mGroupPosition, mChildPosition);
}
} else {
// 折叠后的group标头图片
indicator
.setImageResource(R.drawable.expander_arrow_collapse);
}
TextView text = (TextView) convertView
.findViewById(R.id.group_textview);
text.setText(groups[groupPosition]);
return convertView;
}