ExpandableListView,如何将子范围最小限制为1 [0,如果没有项目]

时间:2015-01-26 12:47:56

标签: android android-layout

我想在ExpandableListView的崩溃状态下显示一个子节点(在没有项目的情况下为0)。 Android正在提供collapseGroup,但它会折叠所有项目。我希望除了一个元素之外崩溃孩子。

任何线索或帮助都将受到高度赞赏。

1 个答案:

答案 0 :(得分:1)

如果在collapseGroup上折叠 ExpandableListView 子组时,如果显示一个项目(如果为空则为null)而不是,则必须实现与自定义 ExpandableListConnector相关的自己的ExpandableListView 实现"覆盖" collapseGroupexpandGroup方法。例如collapseGroup必须是:

 boolean collapseGroup(int groupPos) {
     ExpandableListPosition elGroupPos = ExpandableListPosition.obtain(
             ExpandableListPosition.GROUP, groupPos, -1, -1);
     PositionMetadata pm = getFlattenedPos(elGroupPos);
     elGroupPos.recycle();
     // Group will be collapsed until there will be presented 1 or null items
     if (pm == null && mExpGroupMetadataList.size() <= 1) return false;

     boolean retValue = collapseGroup(pm);
     pm.recycle();
     return retValue;
 }

 boolean collapseGroup(PositionMetadata posMetadata) {
     /*
      * Collapsing requires removal from mExpGroupMetadataList 
      */

     /*
      * If it is null, it must be already collapsed. This group metadata
      * object should have been set from the search that returned the
      * position metadata object.
      */
     // Group will be collapsed until there will be presented 1 or null items
     if (posMetadata.groupMetadata == null && mExpGroupMetadataList.size() <= 1) return false;

     // Remove the group from the list of expanded groups 
     mExpGroupMetadataList.remove(posMetadata.groupMetadata);

     // Refresh the metadata
     refreshExpGroupMetadataList(false, false);

     // Notify of change
     notifyDataSetChanged();

     // Give the callback
     mExpandableListAdapter.onGroupCollapsed(posMetadata.groupMetadata.gPos);

     return true;
 }

必须为expandGroup实施类似的逻辑。