【发布时间】:2014-04-30 22:45:16
【问题描述】:
编辑
我找到了解决办法,请看下文。
原文:
我创建了一个ExpandableListView,它看起来像这样:
- 第 0 组 - 孩子 0、孩子 1、孩子 2
- 第 1 组 - 孩子 0、孩子 1、孩子 2、孩子 3
- 第 2 组 - 孩子 0、孩子 1、孩子 2
- 第 3 组 - 儿童 0、儿童 1、儿童 2、儿童 3、儿童 4
- ...
每个组视图有3~5个子视图,我强制展开所有组视图,
mExpandableListView = (ExpandableListView) mView.findViewById(R.id.expandable_list);
CollectionExpandableListAdapter adapter = new MyExpandableListAdapter(getActivity(), mGroups, mProdChild);
adapter.setInflater(LayoutInflater.from(getActivity().getBaseContext()), getActivity());
mExpandableListView.setAdapter(adapter);
mExpandableListView.setGroupIndicator(null);
for (int i = 0; i < groupList.size() - 1; i++) {
mExpandableListView.expandGroup(i);
}
另外,这是我的适配器代码:
public class MyExpandableListAdapter extends BaseExpandableListAdapter {
private ArrayList<String> mGroupItem;
private ArrayList<ProductListData> mTempChild = new ArrayList<ProductListData>();
private ArrayList<Object> mChildItem = new ArrayList<Object>();
private LayoutInflater mInflater;
private Activity mActivity;
private ImageLoader mImageLoader;
public CollectionExpandableListAdapter(Activity context, ArrayList<String> grList,
ArrayList<Object> childItem) {
mGroupItem = grList;
mChildItem = childItem;
mActivity = context;
mImageLoader = new ImageLoader(mActivity);
}
public void setInflater(LayoutInflater inflater, Activity activity) {
mInflater = inflater;
mActivity = activity;
}
@Override
public ProductListData getChild(int groupPosition, int childPosition) {
return ((ArrayList<ProductListData>) mChildItem.get(groupPosition)).get(childPosition);
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
@Override
public View getChildView(int groupPosition, final int childPosition, boolean isLastChild,
View convertView, ViewGroup parent) {
mTempChild = (ArrayList<ProductListData>) mChildItem.get(groupPosition);
if (convertView == null) {
convertView = mInflater.inflate(R.layout.adapter_collection_row_child, null);
}
...
return convertView;
}
@Override
public int getChildrenCount(int groupPosition) {
return ((ArrayList<ProductListData>) mChildItem.get(groupPosition)).size();
}
@Override
public Object getGroup(int groupPosition) {
return mGroupItem.get(groupPosition);
}
@Override
public int getGroupCount() {
return mGroupItem.size();
}
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup arg3) {
Log.d("LOG", "Group position : " + groupPosition);
if (convertView == null) {
convertView = mInflater.inflate(R.layout.group_row_layout, null);
}
...
return convertView;
}
@Override
public boolean hasStableIds() {
return false;
}
@Override
public boolean isChildSelectable(int arg0, int arg1) {
return false;
}
@Override
public void onGroupCollapsed(int groupPosition) {
super.onGroupCollapsed(groupPosition);
}
@Override
public void onGroupExpanded(int groupPosition) {
super.onGroupExpanded(groupPosition);
}
}
创建 expandableListView 工作正常,但是当我滚动列表时,getGroupView() 中的值一直为 0。在我的 logcat 中,我希望看到 groupPosition 为 0, 1, 2, 3, 4..(当我向下滚动时),但它实际上显示了 0, 0, 0, 1, 0, 2, 0, 3, 0, 4..,在每个 groupPosition 之前都有额外的 0。这只发生在第一次滚动时,所以如果我上下滚动几次,它不会显示额外的 0 值。
我用谷歌搜索并花了很多时间调试它,但我找不到任何解决方案。谁能帮帮我?
解决方案
我的问题出在Listview 的布局中。我把layout_height改成match_parent,bug就消失了!我找到了解决方案here。
【问题讨论】:
标签: android expandablelistview expandablelistadapter