我以更复杂的方式解决了这个问题,但它有效:
带有可扩展列表的布局文件片段:
<ExpandableListView
android:id="@+id/accordion"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:groupIndicator="@android:color/transparent"
android:scrollbars="none" >
</ExpandableListView>
列表组元素的布局
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/accordion_main_background"
android:minHeight="50dip"
android:orientation="vertical"
android:padding="@dimen/screenPromoTaskRootPadding" >
<TextView
android:id="@+id/accordionTitle"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginRight="10dip"
android:layout_toLeftOf="@+id/accordionIndicator"
android:background="@color/accordionOrangeBackColor"
android:paddingLeft="2dip"
android:paddingRight="2dip"
android:textColor="@color/blackTextColor"
android:textSize="@dimen/accordMainTextSize" />
<ImageView
android:id="@+id/accordionIndicator"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:src="@drawable/arrow_up_np" />
</RelativeLayout>
并在活动中。adaptDataCollection 只是一个包含 ExpandableListAdapter 数据的列表。它也是 Accordion 实现的片段,因此列表组将在选择一个元素后折叠其他元素。
ExpandableListView accordion = (ExpandableListView) findViewById(R.id.accordion);
accordion.setOnGroupClickListener(this);
@Override
public boolean onGroupClick(ExpandableListView paramExpandableListView, View paramView, int paramInt, long paramLong) {
ImageView icon=(ImageView)paramView.findViewById(R.id.accordionIndicator);
for (int i = 0; i < adapterDataCollection.size(); i++) {
if (i == paramInt) {
if (paramExpandableListView.isGroupExpanded(i)) {
paramExpandableListView.collapseGroup(i);
icon.setImageResource(R.drawable.arrow_up_np);
} else {
paramExpandableListView.expandGroup(i);
icon.setImageResource(R.drawable.arrow_down_np);
}
} else {
paramExpandableListView.collapseGroup(i);
icon.setImageResource(R.drawable.arrow_up_np);
}
}
paramExpandableListView.invalidate();
return true;
}