【问题标题】:How to select child in ExpandableListView?如何在 ExpandableListView 中选择子项?
【发布时间】:2015-02-04 17:49:29
【问题描述】:

我已经问过这个问题但没有成功。所以我又问了(抱歉顺便说一句)。

我仍然有这个问题:

How to access items inside ExpandableListView?

让我继续。我的应用中有这种情况:

我想对第二组的 2dn 项目进行 performClick()。 目前我所能做的就是使用这行代码通过 performClick() 扩展第二组:

 mGattServicesList.performItemClick(mGattServicesList.getChildAt(1), 1, mGattServicesList.getItemIdAtPosition(1));

知道

private ExpandableListView mGattServicesList;

难道没有一种非常简单的方法可以对组内的项目执行Click() 吗?

我想这样做是因为我有一个像这样的听众

private final ExpandableListView.OnChildClickListener servicesListClickListner =
        new ExpandableListView.OnChildClickListener() {
            @Override
            public boolean onChildClick(ExpandableListView parent, View v, int groupPosition,
                                        int childPosition, long id) {

但我不想自己点击该项目,也找不到在此组中选择此特定项目的方法。

提前谢谢你

【问题讨论】:

    标签: android listview expandablelistview


    【解决方案1】:

    首先,ExpandableListView 支持一种简单的方法来扩展您需要的组:

    mGattServicesList.expandGroup(groupPosition);
    

    以编程方式单击一个项目有点棘手。使用performItemClick() 方法您走在正确的轨道上,但您对如何使用它有点偏离。我会假设你没有使用标题。这让事情变得更加复杂。

    首先你需要获得被点击的View。奇怪的是,这不是一个要求。您可以使用空视图安全地调用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()。您将看到这些步骤类似于检索视图。所以说真的,您不必再次输入此内容……而是显示您需要的内容:

    //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) 方法不存在,我在developer.android.com/reference/android/widget/… 中找不到其他方法
    • 是的,这行得通!实际上,在 mGattServicesList.getChildId(groupPosition, childPosition); 行中我将其更改为: mGattServicesList.getExpandableListAdapter().getChildId(1,2);并且工作得很好,非常感谢:)
    • 很高兴能帮上忙。更新了我的答案以修复错误。
    • 我尝试了这个解决方案,在后端所有变量看起来都不错,但是 UI 没有刷新,选择没有移动到正确的项目(根本没有移动)。您对刷新 UI 有什么建议吗?
    【解决方案2】:

    您不能为列表中的特定项目设置监听器,但您可以为所欲为。只需检查 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
    }
    

    【讨论】:

    • 好的,但是有了这个解决方案,我还是得自己点击孩子吧?
    • 是的..我想在另一个函数中执行Click(),以便它实际调用 onChildClick()
    • 啊,我的误会。我有个主意。给我一秒钟。
    猜你喜欢
    • 1970-01-01
    • 2020-09-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-07
    • 1970-01-01
    • 1970-01-01
    • 2019-05-23
    相关资源
    最近更新 更多