final ExpandableListView expandableListView = (ExpandableListView) fragView.findViewById(R.id.callLogList);
expandableListView.setGroupIndicator(null);
expandableListView.setClickable(true);
expandableListView.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() {
@Override
public void onGroupExpand(int groupPosition) {
if (prev != -1 && prev != groupPosition) {
expandableListView.collapseGroup(prev);
}
if (prev != -1 && prev != groupPosition) {
ImageView prevTriangle = (ImageView) expandableListView.getChildAt(prev).findViewById(R.id.triangleTop);
prevTriangle.setVisibility(View.INVISIBLE);
}
ImageView triangle = (ImageView) expandableListView.getChildAt(groupPosition).findViewById(R.id.triangleTop);
if (triangle != null) {
triangle.setVisibility(View.VISIBLE);
}
prev = groupPosition;
}
});
expandableListView.setOnGroupCollapseListener(new ExpandableListView.OnGroupCollapseListener() {
@Override
public void onGroupCollapse(int groupPosition) {
ImageView triangle = (ImageView) expandableListView.getChildAt(groupPosition).findViewById(R.id.triangleTop);
triangle.setVisibility(View.INVISIBLE);
}
});
//列表视图的XML:
<ExpandableListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="20dp"
android:id="@+id/callLogList"
android:divider="@android:color/transparent"
android:dividerHeight="0dp"
android:childDivider="@android:color/transparent"
/>
//用于子视图和父视图的XML
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="@dimen/call_log_list_item_actionset_height"
android:layout_alignParentBottom="true"
android:id="@+id/callLogListItemActionSet"
android:visibility="visible"
android:orientation="vertical"
android:padding="0dp"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:background="#8a8a8a"
android:gravity="end"
>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_marginRight="20dp"
android:layout_marginEnd="20dp"
>
<ImageView
android:layout_width="40dp"
android:layout_height="40dp"
android:background="@drawable/ic_call_white_48dp"
android:layout_centerInParent="true"
android:id="@+id/callLogListActionItemActionSetPhone"
/>
</RelativeLayout>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_marginRight="20dp"
android:layout_marginEnd="20dp"
>
<ImageView
android:layout_width="40dp"
android:layout_height="40dp"
android:background="@drawable/contact_circle_white"
android:id="@+id/callLogListItemActionSetPerson"
android:layout_centerVertical="true"
android:layout_centerInParent="true"
/>
</RelativeLayout>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_marginRight="10dp"
android:layout_marginEnd="10dp">
<ImageView
android:layout_width="40dp"
android:layout_height="40dp"
android:background="@drawable/ic_log_entry_white"
android:id="@+id/callLogDetailLink"
android:layout_centerVertical="true"
android:layout_centerInParent="true"
/>
</RelativeLayout>
</LinearLayout>
<ImageView
android:layout_width="22dp"
android:layout_height="22dp"
android:src="@drawable/log_detail_navbar_triangle_bottom"
/>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@android:color/transparent">
<ImageView
android:id="@+id/triangleTop"
android:visibility="invisible"
android:layout_width="22dp"
android:layout_height="22dp"
android:layout_marginTop="45dp"
android:src="@drawable/log_detail_navbar_triangle_top"
/>
<!-- Contact Image -->
<ImageView
android:layout_width="40dp"
android:layout_height="40dp"
android:id="@+id/callLogImage"
android:src="@drawable/ic_contact_grey"
android:layout_alignParentTop="true"
/>
<!-- Contact Name -->
<TextView
android:layout_width="wrap_content"
android:layout_height="30dp"
android:text="First Middle Last"
android:textSize="15sp"
android:textColor="@android:color/white"
android:background="@android:color/transparent"
android:singleLine="true"
android:paddingLeft="15dp"
android:layout_toRightOf="@+id/callLogImage"
android:id="@+id/callLogName"
android:paddingTop="7dp"
/>
<!-- Call Time -->
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="12:00 PM"
android:textColor="@android:color/white"
android:layout_below="@+id/callLogName"
android:layout_toRightOf="@+id/callLogImage"
android:paddingLeft="15dp"
android:textSize="13sp"
android:id="@+id/callLogDate"
/>
<!-- Call Number -->
<TextView
android:layout_width="wrap_content"
android:layout_height="30dp"
android:text="(111) 111-1111"
android:textColor="@color/white"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_marginRight="7dp"
android:singleLine="true"
android:paddingTop="7dp"
android:id="@+id/callLogNumber"
/>
<!-- Call Duration -->
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="5 min 10 sec"
android:textColor="@color/white"
android:id="@+id/callLogDuration"
android:layout_below="@+id/callLogNumber"
android:layout_alignParentRight="true"
android:layout_marginRight="7dp"
android:singleLine="true"
/>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="@dimen/call_log_list_item_actionset_height"
android:layout_alignParentBottom="true"
android:background="@color/background_material_light"
android:id="@+id/callLogListItemActionSet"
android:visibility="gone"
>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
>
<ImageView
android:layout_width="24dp"
android:layout_height="24dp"
android:background="@drawable/ic_call_blue600_24dp"
android:layout_centerInParent="true"
/>
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
>
<ImageView
android:layout_width="24dp"
android:layout_height="24dp"
android:background="@drawable/ic_person_blue600_24dp"
android:layout_centerVertical="true"
android:layout_centerInParent="true"
/>
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
>
<ImageView
android:layout_width="24dp"
android:layout_height="24dp"
android:background="@drawable/ic_note_add_blue600_24dp"
android:layout_centerVertical="true"
android:layout_centerInParent="true"
/>
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
>
<ImageView
android:layout_width="24dp"
android:layout_height="24dp"
android:background="@drawable/ic_attach_file_blue600_24dp"
android:layout_centerVertical="true"
android:layout_centerInParent="true"
/>
</RelativeLayout>
</LinearLayout>
这是我的代码。我试图在父视图中定位ImageView并在可见/不可见之间切换。除了扩展比在其上方扩展的父级低一个位置的父级之外,它的效果很好。此时,ImageView引用为null,但仅限于此情况。
**当我说父母较低时,我的意思是它的小组位置高一个,或者上一个+ 1。