首先,ExpandableListView
支持一种简单的方式来展开你需要的组:
mGattServicesList.expandGroup(groupPosition);
要通过程序点击一个项目有一些技巧。使用
performItemClick()
方法是正确的思路,但你在如何使用它方面有些错误。我假设您没有使用头文件,并且这会进一步复杂化事情。
首先,您需要获取要点击的视图。奇怪的是,这不是必需的。您可以安全地使用 null 视图调用
performItemClick()
。唯一的缺点是您的子项单击侦听器也将收到 null 视图。
//First we need to pack the child's two position identifiers
long packedPos = ExpandableListView.getPackedPositionForChild(int groupPosition, int childPosition)
//Then we convert to a flat position to use with certain ListView methods
int flatPos = mGattServicesList.getFlatListPosition(packedPos)
//Now adjust the position based on how far the user has scrolled the list.
int adjustedPos = flatPos - mGattServicesList.getFirstVisiblePosition()
//If all is well, the adjustedPos should never be < 0
View childToClick = mGattServicesList.getChildAt(adjustedPos)
现在我们需要位置和ID来提供给
performItemclick()
。你会发现这些步骤与检索视图类似。所以,你不必再次输入这个...但为了展示你需要什么:
//You can just reuse the same variables used above to find the View
long packedPos = ExpandableListView.getPackedPositionForChild(int groupPosition, int childPosition)
int flatPos = mGattServicesList.getFlatListPosition(packedPos)
//Getting the ID for our child
long id = mGattServicesList.getExpandableListAdapter().getChildId(groupPosition, childPosition)
最后,您可以调用performItemClick()
:
performItemClick(childToClick, flatPos, id)
首先声明,我没有使用IDE来检查这段代码,因此可能存在一些语法错误,这些错误可能会导致编译失败。但总的来说,应该能够传达以编程方式单击子视图的不那么容易的步骤。
最后注意一点,您提供的图片显示组和子项计数从1开始。请注意,它们实际上被认为是基于零的位置。因此,第一组位于位置0,每个组的第一个子项位于位置0。