Adding listener to child nodes in Expandable List Item

纵然是瞬间 提交于 2019-12-11 02:28:48

问题


I have used a code given in tutorial http://mylifewithandroid.blogspot.com/2010/12/expandable-list-and-checkboxes.html I have attached an image of the ExpandableListView

SimpleExpandableListAdapter expListAdapter =
        new SimpleExpandableListAdapter(
            this,
            createGroupList(),  
            R.layout.child_row, 
            new String[] { "CityName" },
            new int[] { R.id.childname },       
            createChildList(),  
            R.layout.child_row, 
            new String[] { "citydetails", "city" }, 
            new int[] { R.id.childname, R.id.city}  
        );

    setListAdapter( expListAdapter );

I need some code example on how to add listeners to individual child nodes for example clicking on Address should take the user to another activity. Looking forward to your reply. thanks.


回答1:


You can also have your activity override the the onChildClick method:

@Override
public boolean onChildClick(ExpandableListView expandableListView, View view, int groupPosition, int childPosition, long id) {
    //implement logic to start the appropriate activity for this child.
}

From the android dev site: onChildClick.

I also recommend downloading the API Demos. You can learn more on setting it up here.




回答2:


AFAIK, you cannot add listener's to your child views using the example above. You will have to have a custom adapter. In this custom adapter's getChildView and getGroupView you can provide your own listeners. Eg.

public View getGroupView(
            int groupPosition, 
            boolean isExpanded,
            View convertView, 
            ViewGroup parent) {

        ViewGroup viewGroup = null;
        if (convertView == null) {
            LayoutInflater li = (LayoutInflater) getApplication().getSystemService(LAYOUT_INFLATER_SERVICE);
            if (li != null) {
                viewGroup = (ViewGroup) li.inflate(R.layout.listitems_group, parent, false);
            }
        } else {
            viewGroup = (ViewGroup) convertView;
        }

            Button button = (Button) viewGroup.findViewById(R.id.group_button);
            button.setOnClickListener(new OnClickListener() {
                 void onClick(View v) {
                     // Do what you want here
                 }
            });
            return viewGroup;
}

Here I have buttons in my GroupView which I want to add onClick listener's for.



来源:https://stackoverflow.com/questions/8588701/adding-listener-to-child-nodes-in-expandable-list-item

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!