如何在可扩展列表中为子项的click事件创建

时间:2010-08-05 16:17:20

标签: android events listener

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

我无法弄清楚如何将监听器附加到子元素,以便在用户点击电话号码时触发一些操作。

非常感谢任何代码或链接。

3 个答案:

答案 0 :(得分:20)

您需要订阅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;
}

答案 1 :(得分:2)

您应该在可扩展列表中指定项目的位置,如此

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;
    }
});

答案 2 :(得分:1)

您应该在ExpandableListActivity扩展程序中覆盖onChildClick