【问题标题】:How to create on click event for children in Expandable list如何在可展开列表中为孩子创建点击事件
【发布时间】:2011-03-25 21:57:16
【问题描述】:

我正在玩这个例子。 http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/view/ExpandableList2.html

我不知道如何将侦听器附加到子元素,以便在用户点击电话号码时触发一些操作。

任何代码或链接将不胜感激。

【问题讨论】:

    标签: android events listener


    【解决方案1】:

    您需要订阅setOnChildClickListener

    getExpandableListView().setOnChildClickListener(this);
    

    并实现 OnChildClickListener

    @Override
    public boolean onChildClick(ExpandableListView parent, View v, int groupPosition,
            int childPosition, long id) {
        // use groupPosition and childPosition to locate the current item in the adapter
        return true;
    }
    

    【讨论】:

    • 这是否适用于我链接到的示例?似乎该示例没有实现 ExpandableListView
    • 当然可以,因为它扩展了ExpandableListActivity
    • 谢谢。您是否碰巧知道获取被点击孩子的价值或“标签”的正确方法是什么?
    • 您需要使用两个int值跳转到适配器的正确位置并从中获取值。检查光标你有moveToPosition(pos)的方法,然后你只需从光标中获取值。
    • 我们如何在不手动比较值的情况下确定组位置?因为我的可展开列表视图将有多达 100 个元素,每个 childclicklistener 调用不同的 URL。
    【解决方案2】:

    您应该在 ExpandableListActivity 扩展中覆盖 onChildClick

    【讨论】:

    • 覆盖后如何获取选中的子节点?
    【解决方案3】:

    您应该像这样在可扩展列表中指定项目的位置

    listView.setOnChildClickListener(new OnChildClickListener()
    {
        @Override
        public boolean onChildClick(ExpandableListView parent, View v, int group_position, int child_position, long id)
        {
            if(group_position==0 && child_position==0){
                TryFragment secondFragment = (TryFragment) SampleActivity.this.getFragmentManager().findFragmentById(R.id.tryFragment);
                secondFragment.getView().setVisibility(View.VISIBLE);
            } else if(group_position==2 && child_position==2){
                TryFragment secondFragment = (TryFragment) SampleActivity.this.getFragmentManager().findFragmentById(R.id.tryFragment);
                secondFragment.getView().setVisibility(View.VISIBLE);
            }
            return false;
        }
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-06-28
      • 2015-05-31
      • 1970-01-01
      • 1970-01-01
      • 2019-05-01
      • 1970-01-01
      • 2013-06-22
      • 1970-01-01
      相关资源
      最近更新 更多