所以,我创建了一个自定义的ExpandableListAdapter,因为我想要一个可扩展的列表,它可以有多个子节点,基本上是一个双层列表。它一直工作得很好,只要它是活动中的第一个也是唯一的视图。但是,我最近在其所在的LinearLayout中添加了一些其他视图(一组TextView),并且突然它完全停止显示,而没有给出任何错误。
首先,我想知道是否由于某种原因需要成为活动中的第一个/唯一一个视图,如果是的话,是否有任何方法来解决这个问题。代码如下。
expListView = (ExpandableListView) this.findViewById(R.id.finalcalculation_list_view);
final ExpandableListAdapter expListAdapter = new ExpandableListAdapter(
this, groupList, totalCollection);
expListView.setAdapter(expListAdapter);
<?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="match_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<!-- Here are some TextViews -->
</LinearLayout>
<ScrollView android:id="@+id/finalcalculation_scrollview"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<ExpandableListView
android:id="@+id/finalcalculation_list_view"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ExpandableListView>
</ScrollView>
</LinearLayout>
以下是它最初的工作原理:
<?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="match_parent"
android:orientation="vertical" >
<ExpandableListView
android:id="@+id/finalcalculation_list_view"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ExpandableListView>
</LinearLayout>
不幸的是,这是我的ExpandableListAdapter的大型代码转储。
public class ExpandableListAdapter extends BaseExpandableListAdapter {
private Activity context;
private Map<String, List<String>> collections;
private List<String> groups;
public ExpandableListAdapter(Activity context, List<String> groups,
Map<String, List<String>> collections) {
this.context = context;
this.collections = collections;
this.groups = groups;
}
public Object getChild(int groupPosition, int childPosition) {
return collections.get(groups.get(groupPosition)).get(childPosition);
}
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
public View getChildView(final int groupPosition, final int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
final String laptop = (String) getChild(groupPosition, childPosition);
LayoutInflater inflater = context.getLayoutInflater();
if (convertView == null) {
convertView = inflater.inflate(R.layout.finalcalculation_child_item, null);
}
TextView item = (TextView) convertView.findViewById(R.id.finalcalculation_child_item_textview);
item.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setMessage("Do you want to remove?");
builder.setCancelable(false);
builder.setPositiveButton("Yes",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
List<String> child =
collections.get(groups.get(groupPosition));
child.remove(childPosition);
notifyDataSetChanged();
}
});
builder.setNegativeButton("No",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog alertDialog = builder.create();
alertDialog.show();
}
});
item.setText(laptop);
return convertView;
}
public int getChildrenCount(int groupPosition) {
return collections.get(groups.get(groupPosition)).size();
}
public Object getGroup(int groupPosition) {
return groups.get(groupPosition);
}
public int getGroupCount() {
return groups.size();
}
public long getGroupId(int groupPosition) {
return groupPosition;
}
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
String itemName = (String) getGroup(groupPosition);
if (convertView == null) {
LayoutInflater groupInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = groupInflater.inflate(R.layout.finalcalculation_group_item, null);
}
TextView item = (TextView) convertView.findViewById(R.id.finalcalculation_group_item_textview);
item.setTypeface(null, Typeface.BOLD);
item.setText(itemName);
return convertView;
}
public boolean hasStableIds() {
return true;
}
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}
我尝试从
更改getChildView和getGroupView的InflatorsconvertView = inflater.inflate(R.layout.finalcalculation_child_item, null);
到两个
convertView = inflater.inflate(R.layout.finalcalculation_child_item, parent, false); //AND
convertView = inflater.inflate(R.layout.finalcalculation_child_item, parent, true); //AND
但它要么不起作用,要么引发错误。
非常感谢所有帮助和意见。
答案 0 :(得分:4)
嵌套layout_height
的{{1}}设置为LinearLayout
,这会导致嵌套match_parent
与父LinearLayout
的高度相同并隐藏LinearLayout
。它应该是ScrollView
或固定大小。此外,您不需要wrap_content
。
尝试这样的事情:
ScrollView