我已经问过这个问题了,但没有成功。所以我再问一次(抱歉)。
我还有个问题:
How to access items inside ExpandableListView?。
让我继续。我的应用程序中有这样的情况:

我想在第二组的2dn项目上做一个performClick()。目前我所能做的就是用performClick()来使用这一行代码展开第二个组:
mGattServicesList.performItemClick(mGattServicesList.getChildAt(1), 1, mGattServicesList.getItemIdAtPosition(1));知知
private ExpandableListView mGattServicesList;对于组内的项,难道没有非常简单的performClick()方法吗?
我想做这件事,因为我有一个
private final ExpandableListView.OnChildClickListener servicesListClickListner =
new ExpandableListView.OnChildClickListener() {
@Override
public boolean onChildClick(ExpandableListView parent, View v, int groupPosition,
int childPosition, long id) {但是我不想自己点击这个项目,我也没有办法在这个组中选择这个特定的项目。
提前谢谢你
发布于 2015-02-05 02:19:28
首先,ExpandableListView支持一种简单的方法来扩展您需要的组:
mGattServicesList.expandGroup(groupPosition);以编程方式单击一个项目有点棘手。使用performItemClick()方法是正确的,但是在如何使用它方面有点偏离。我假设您没有使用标头。这使事情变得更加复杂。
首先,您需要获得要单击的视图。奇怪的是,这不是一个要求。您可以使用空视图安全地调用performItemClick()。唯一的缺点是您的子单击监听器也将收到一个空视图。
//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()。您将看到这些步骤类似于检索视图。因此,实际上,您不需要进一步输入again...but来显示您需要的内容:
//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位置。
发布于 2015-02-04 17:58:24
在列表中不能有特定项目的侦听器,但您可以做自己想做的事情。只需检查groupPosition和childPosition是否是您想要的操作,然后执行操作(或其他项适用的其他操作)。
在ExpandableListView.OnChildClickListener中
@Override
public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
//groupPosition tells you what group the clicked child came from
//childPosition tells you what child was clicked
if (groupPosition == 2 && childPosition == 2) {
//so this code only executes if the 2nd child in the 2nd group is clicked
}
//you can ignore the other items or do something else when they are clicked
}https://stackoverflow.com/questions/28328010
复制相似问题