Android:多级ExpandableListView多次显示子项

时间:2017-10-27 00:48:47

标签: android expandablelistview expandablelistadapter

我已经创建了一个3级(Root, Parent, Child)自定义ExpandableListView和相关的适配器,这要归功于其中的一些教程。我在显示列表的Parent级别时遇到问题,而RootChild级别正确显示。我发现Parent级别的视图重复N次,其中N是Parent组中元素的数量。

我想要的: Three categories under Canon

我得到了什么: The three categories under Canon are duplicated 3 times

Root级别:

@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, 
                         View convertView, ViewGroup parent) {

/* this list is the header of the Parent level, and I know it has the correct number of elements */
List<String> lensTypeList = listDataChild.get(_listDataHeader.get(groupPosition));

/* the Parent level of the ExpandableListView */
LensSecondLevel lensSecondLevel = new LensSecondLevel(ManageLensesActivity.this);

/* all params other than lensTypeList work correctly in the later levels */
lensSecondLevel.setAdapter(new SecondLevelListViewAdapter(ManageLensesActivity.this, lensTypeList, lensMap, lensPositionIndicesMap));
getChildrenCount(int groupPosition)级别的

Root每次都会返回正确的数字,尽管创建了多少个子项(Parent)。我不知道为什么getGroupViewParent级别重复调用。

Parent级别:

@Override
public Object getGroup(int groupPosition)
{
    /* _listDataHeader is the same ArrayList<String> lensTypeList from above */
    return this._listDataHeader.get(groupPosition);
}

你不应该传递ArrayList<String>作为ExpandableListView第二级的标题吗?如果我将第二级String作为标题传递,我只能达到预期的结果,但这会破坏我查找Child级别视图的逻辑,因为groupPosition始终是{{ 1}}自0

提前感谢您的任何帮助/建议。 :)

1 个答案:

答案 0 :(得分:0)

在这里找到答案:https://stackoverflow.com/a/35208411/5004391

我真的不明白为什么,但我必须对getChildrenCount(int groupPosition)级别的Root进行硬编码才能返回1,而不是实际的子级数。在我的应用程序的其他ExpandableListView中,我总是返回子项数,它按预期工作。

相关问题