Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create on click event for children in Expandable list

I am playing around with this example. http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/view/ExpandableList2.html

I cannot figure out how to attach a listener to the children elements so that I can trigger some action when the user taps on the phone number.

Any code or links would be greatly appreciated.

like image 707
user412164 Avatar asked Aug 05 '10 16:08

user412164


2 Answers

You need to subscribe to setOnChildClickListener

getExpandableListView().setOnChildClickListener(this);

and implement 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;
}
like image 183
Pentium10 Avatar answered Nov 15 '22 03:11

Pentium10


You should specify location of items in expandable list like this

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;
    }
});
like image 34
mert Avatar answered Nov 15 '22 04:11

mert