【问题标题】:How to handle Android SimpleExpandableListAdapter entry selection如何处理 Android SimpleExpandableListAdapter 条目选择
【发布时间】:2010-03-25 00:44:53
【问题描述】:

我使用 SimpleExpandableListAdapter 类型在我的 Android 应用程序中创建了一个可扩展列表。 But I'm at a complete loss as to how I detect events when one of the child entries has been selected/clicked.

我已经尝试了所有常用的 OnClickListener/OnChildClickListener 等,但似乎无法找到(通过实验或半小时谷歌搜索)正确的处理程序例程应该是什么。

非常感谢任何帮助。

【问题讨论】:

    标签: android event-handling handler


    【解决方案1】:

    应该是:

    list.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
        public void onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
            Object o = (Object)adapter.getChild(groupPosition, childPosition);
            // perform work on child object here
        }
    }  
    

    不过,听起来你尝试过这个...ExpandableListView.OnChildClickListener 说它实际上就是这样做的方法。

    另外,您是否为您的 ListAdapter 定义了 allItemsAreEnabled() 和/或 isEnabled() 方法?您不应该这样做,但也许它们当前已定义并返回错误的值?

    【讨论】:

    • 我发现我可以只声明 onClick 而无需通过 setOn... 函数注册对象。这似乎与我迄今为止使用的所有其他类不同。
    【解决方案2】:

    还有……

    如果您碰巧使用扩展 BaseExpandableListAdapter 的类,那么它们是默认实现的方法,您必须为其设置返回布尔值。

        @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        // TODO Auto-generated method stub
        return true;
    }
    

    默认情况下,此方法返回 false,切换为 true(如果是这种情况),您的 OnChildClickListener 应该开始正确解析。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-06-09
      • 2015-07-11
      • 2018-06-26
      • 1970-01-01
      • 2018-02-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多