如何在可扩展列表视图中选择子项?

3

我已经提出了问题,但没有成功。所以我再次询问(顺便说一句抱歉)。

我仍然有这个问题:

如何访问ExpandableListView内部的项目?.

让我简要介绍一下。我在我的应用程序中遇到了这种情况:

enter image description here

我想要对第二组的第二项执行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) {

但是我不想自己点击该项,也找不到一种方法来选择该组中的特定项。
提前致谢。
2个回答

9

首先,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。


谢谢您的回复!不过,似乎getChildId(groupPosition,childPosition)方法不存在,并且我在http://developer.android.com/reference/android/widget/ExpandableListView.html#getSelectedId%28%29中找不到其他方法。 - Bob
是的,它起作用了!!实际上,在mGattServicesList.getChildId(groupPosition, childPosition)这一行中,我将其更改为:mGattServicesList.getExpandableListAdapter().getChildId(1,2); 并且完美地工作了,非常感谢 :) - Bob
很高兴能够帮助。我更新了我的答案以修复错误。 - Ifrit
我尝试了这个解决方案,在后端所有变量看起来都很好,但是UI没有刷新,选择也没有移动到正确的项目(根本没有移动)。你有任何刷新UI的建议吗? - ZP007

0

你不能为列表中的特定项设置监听器,但你可以按照自己的需求进行操作。只需检查groupPositionchildPosition是否符合你的要求,然后执行相应的操作(或者对于其他项执行适当的操作)。

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
}

好的,但是使用这个解决方案,我仍然需要自己点击到子元素对吗? - Bob
是的..我想在另一个函数中执行performClick(),以便它实际调用onChildClick()。 - Bob
啊,我误解了。我有一个想法。给我一秒钟。 - h0x0

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接