我的ExpandableListView在我的Galaxy Note 3上正常运行,如下图所示:
但是在我的Nexus 7和Galaxy Note 10.1上,当我展开列表以显示孩子时,会出现一堆额外的空间。 (在我点击父母之前,空格不会出现。)
来自Nexus 7:
我的第一个猜测是我的布局资源有些不一致(我有不同的sw-600和sw-700布局资源)但是删除更大的屏幕资源,行为是一样的。我还在我的适配器类中注释了getGroupView和getChildView方法中的所有内容,而不是夸大布局,但仍然是这种行为。所以现在我不知道造成这种行为的原因。我想其他重要的细节是我在AlertDialog中这样做,但它并没有解释为什么它似乎只发生在更大的设备上。
初始化ExpandableListAdapter和AlertDialog:
private void showServicesDialog(final ArrayList<Service> services) {
View dView = activity.getLayoutInflater().inflate(R.layout.expandable_list_view, null);
View header = activity.getLayoutInflater().inflate(R.layout.service_list_header, null);
AlertDialog servicesDialog = DialogHelper.createLayoutAlertDialog(activity, null, dView,
R.string.cancel, R.string.add_services,
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
if (selectedServices.size() > 0) {
addServicesToBackend();
}
}
});
ExpandableListView list = (ExpandableListView) dView.findViewById(R.id.expandableListView);
ServicesExpandableListAdapter adapter = new ServicesExpandableListAdapter(activity, services);
list.setGroupIndicator(null);
list.setSelector(R.drawable.list_selector_bg);
list.setDivider(null);
list.addHeaderView(header);
list.setAdapter(adapter);
servicesDialog.show();
}
从我的Adapter类:
public ServicesExpandableListAdapter(Activity act, ArrayList<Service> s) {
activity = (MyActivity) act;
services = s;
itemChecked = new ArrayList<Boolean>();
for (int i = 0; i < services.size(); i++)
itemChecked.add(false);
}
@Override
public Object getChild(int groupPosition, int childPosition) {
return services.get(groupPosition);
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
@Override
public int getChildrenCount(int groupPosition) {
return 1;
}
@Override
public Object getGroup(int groupPosition) {
return services.get(groupPosition);
}
@Override
public int getGroupCount() {
return services.size();
}
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
@Override
public boolean hasStableIds() {
return false;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return false;
}
private static class ParentViewHolder {
TextView nameTV, categoryTV, expandTV, typeTV, amountTV, warningTV;
CheckBox checkbox;
}
@Override
public View getGroupView(final int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
ParentViewHolder holder = null;
final Service service = services.get(groupPosition);
if (convertView == null) {
convertView = activity.getLayoutInflater().inflate(R.layout.service_list_parent_item, null);
holder = new ParentViewHolder();
holder.nameTV = (TextView) convertView.findViewById(R.id.nameTV);
holder.typeTV = (TextView) convertView.findViewById(R.id.typeTV);
holder.categoryTV = (TextView) convertView.findViewById(R.id.categoryTV);
holder.expandTV = (TextView) convertView.findViewById(R.id.expandTV);
holder.amountTV = (TextView) convertView.findViewById(R.id.amountTV);
holder.checkbox = (CheckBox) convertView.findViewById(R.id.selectCB);
holder.warningTV = (TextView) convertView.findViewById(R.id.warningTV);
convertView.setTag(holder);
} else {
holder = (ParentViewHolder) convertView.getTag();
}
//content commented out
return convertView;
}
private static class ChildViewHolder {
TextView typeTV, priceTV, infoTV;
CheckBox weekdaysCB, saturdaysCB, sundaysCB, holidaysCB;
}
@Override
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
ChildViewHolder holder = null;
if (convertView == null) {
convertView = activity.getLayoutInflater().inflate(R.layout.service_list_child_item, null);
holder = new ChildViewHolder();
holder.typeTV = (TextView) convertView.findViewById(R.id.typeTV);
holder.priceTV = (TextView) convertView.findViewById(R.id.priceTV);
holder.infoTV = (TextView) convertView.findViewById(R.id.infoTV);
holder.weekdaysCB = (CheckBox) convertView.findViewById(R.id.weekdaysCB);
holder.saturdaysCB = (CheckBox) convertView.findViewById(R.id.saturdaysCB);
holder.sundaysCB = (CheckBox) convertView.findViewById(R.id.sundaysCB);
holder.holidaysCB = (CheckBox) convertView.findViewById(R.id.holidaysCB);
convertView.setTag(holder);
} else {
holder = (ChildViewHolder) convertView.getTag();
}
//content commented out
return convertView;
}
service_list_parent_item.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<View
android:id="@+id/topDivider"
android:layout_width="match_parent"
android:layout_height="1px"
android:layout_alignParentTop="true"
android:background="@color/gray_divider" />
<TextView
android:id="@+id/nameTV"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_marginLeft="5dp"
android:layout_marginRight="20dp"
android:layout_marginTop="5dp"
android:layout_toLeftOf="@+id/warningTV"
android:text="Service name"
android:textAppearance="@style/regularText" />
<TextView
android:id="@+id/typeTV"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/nameTV"
android:layout_below="@+id/nameTV"
android:layout_marginBottom="3dp"
android:layout_marginRight="5dp"
android:background="@color/holo_light_purple"
android:drawableLeft="@drawable/ic_clock_tiny_white" />
<TextView
android:id="@+id/categoryTV"
style="@style/regularTextVerySmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/typeTV"
android:layout_marginBottom="3dp"
android:layout_marginRight="5dp"
android:layout_toLeftOf="@+id/expandTV"
android:layout_toRightOf="@+id/typeTV"
android:ellipsize="end"
android:lines="1"
android:text="Category Name that is super long" />
<TextView
android:id="@+id/warningTV"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="@+id/selectCB"
android:layout_marginRight="5dp"
android:background="@drawable/action_bar_selector"
android:drawableRight="@drawable/ic_warning_gray"
android:clickable="false"
android:focusable="false"
android:focusableInTouchMode="false"
android:padding="5dp"
android:visibility="invisible" />
<CheckBox
android:id="@+id/selectCB"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:focusable="false"
android:focusableInTouchMode="false"
android:padding="5dp" />
<TextView
android:id="@+id/amountTV"
android:layout_width="35dp"
android:layout_height="35dp"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="10dp"
android:layout_marginTop="5dp"
android:background="@drawable/blue_button_selector"
android:clickable="false"
android:focusable="false"
android:focusableInTouchMode="false"
android:gravity="center"
android:text="0.0"
android:textColor="@color/white"
android:textSize="15sp"
android:textStyle="bold" />
<TextView
android:id="@+id/expandTV"
style="@style/regularTextVerySmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignRight="@+id/bottomDivider"
android:drawableRight="@drawable/ic_expand_arrow_gray"
android:gravity="bottom"
android:paddingBottom="5dp"
android:text="@string/expand"
android:textColor="@color/lighter_gray_text" />
<View
android:id="@+id/bottomDivider"
android:layout_width="90dp"
android:layout_height="3dp"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_marginRight="60dp"
android:background="@color/blue" />
</RelativeLayout>
service_list_child_item.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/gray_tasklist" >
<View
android:id="@+id/topDivider"
android:layout_width="match_parent"
android:layout_height="3dp"
android:layout_alignParentTop="true"
android:background="@color/blue" />
<View
android:id="@+id/centerView"
android:layout_width="1px"
android:layout_height="1px"
android:layout_below="@+id/topDivider"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp" />
<TextView
android:id="@+id/typeTV"
style="@style/regularTextVerySmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/topDivider"
android:layout_marginTop="10dp"
android:layout_toLeftOf="@+id/centerView"
android:drawableLeft="@drawable/ic_clock_tiny_purple"
android:drawablePadding="4dp"
android:gravity="center_vertical"
android:text="@string/time_service"
android:textColor="@color/holo_light_purple" />
<TextView
android:id="@+id/priceTV"
style="@style/regularTextSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/typeTV"
android:layout_marginLeft="35dp"
android:layout_toRightOf="@+id/centerView"
android:text="35e/hr"
android:textColor="@color/blue" />
<RelativeLayout
android:id="@+id/relativeLayout1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/typeTV"
android:layout_centerHorizontal="true"
android:layout_marginTop="5dp" >
<CheckBox
android:id="@+id/weekdaysCB"
style="@style/regularTextVerySmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:enabled="false"
android:text="@string/weekdays" />
<CheckBox
android:id="@+id/holidaysCB"
style="@style/regularTextVerySmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/weekdaysCB"
android:layout_below="@+id/weekdaysCB"
android:enabled="false"
android:text="@string/holidays" />
<CheckBox
android:id="@+id/saturdaysCB"
style="@style/regularTextVerySmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/weekdaysCB"
android:layout_toRightOf="@+id/weekdaysCB"
android:enabled="false"
android:text="@string/saturdays" />
<CheckBox
android:id="@+id/sundaysCB"
style="@style/regularTextVerySmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/holidaysCB"
android:layout_alignLeft="@+id/saturdaysCB"
android:enabled="false"
android:text="@string/sundays" />
</RelativeLayout>
<TextView
android:id="@+id/infoTV"
style="@style/regularTextVerySmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/relativeLayout1"
android:layout_centerHorizontal="true"
android:gravity="center_horizontal"
android:padding="5dp" />
</RelativeLayout>
最后,expandable_list_view.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/gray_tasklist"
android:orientation="vertical" >
<ExpandableListView
android:id="@+id/expandableListView"
style="@style/listView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:divider="@color/gray_divider"
android:dividerHeight="1px"
android:childDivider="#00000000"/>
</LinearLayout>