在Android中的ExpanableListView上附加监听器

时间:2011-05-07 10:41:02

标签: android

我是android开发的新手,在获得学士学位后获得了一份工作。 几天前我开始研究android,因为我的团队负责人为我分配了与GUI相关的任务。我从我的一位朋友那里听说这个网站,我在android中遇到了可扩展列表视图的问题。我从中获取示例代码谷歌搜索,我在下面发布。 当我点击群组孩子时,我需要有关如何添加监听器的帮助。 我想使用BaseExpandableListAdapter,我无法附加Child Click Listener。

有什么想法吗?

 import android.app.ExpandableListActivity;
    import android.os.Bundle;
    import android.view.Gravity;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.AbsListView;
    import android.widget.BaseExpandableListAdapter;
    import android.widget.ExpandableListAdapter;
    import android.widget.TextView;
    import android.widget.ExpandableListView.OnChildClickListener;
    public class ExpandableList1 extends ExpandableListActivity implements
        OnChildClickListener {
    ExpandableListAdapter mAdapter;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mAdapter = new MyExpandableListAdapter();
        setListAdapter(mAdapter);
        getExpandableListView().setOnChildClickListener(this);
    }
    public class MyExpandableListAdapter extends BaseExpandableListAdapter {
        private String[] groups = { "People Names", "Dog Names", "Cat Names",
                "Fish Names" };
        private String[][] children = {
                { "Arnold", "Barry", "Chuck", "David" },
                { "Ace", "Bandit", "Cha-Cha", "Deuce" },
                { "Fluffy", "Snuggles" }, { "Goldy", "Bubbles" } };

        public Object getChild(int groupPosition, int childPosition) {
            return children[groupPosition][childPosition];
        }
        public long getChildId(int groupPosition, int childPosition) {
            return childPosition;
        }
        public int getChildrenCount(int groupPosition) {
            return children[groupPosition].length;
        }
        public TextView getGenericView() {
            AbsListView.LayoutParams lp = new AbsListView.LayoutParams(
                    ViewGroup.LayoutParams.WRAP_CONTENT, 64);
            TextView textView = new TextView(ExpandableList1.this);
            textView.setLayoutParams(lp);
            textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
            textView.setPadding(36, 0, 0, 0);
            return textView;
        }
        public View getChildView(int groupPosition, int childPosition,
                boolean isLastChild, View convertView, ViewGroup parent) {
            TextView textView = getGenericView();
            textView.setText(getChild(groupPosition, childPosition).toString());
            return textView;
        }
        public Object getGroup(int groupPosition) {
            return groups[groupPosition];
        }
        public int getGroupCount() {
            return groups.length;
        }
        public long getGroupId(int groupPosition) {
            return groupPosition;
        }
        public View getGroupView(int groupPosition, boolean isExpanded,
                View convertView, ViewGroup parent) {
            TextView textView = getGenericView();
            textView.setText(getGroup(groupPosition).toString());
            return textView;
        }
        public boolean isChildSelectable(int groupPosition, int childPosition) {
            return true;
        }
        public boolean hasStableIds() {
            return true;
        }
    }
}

1 个答案:

答案 0 :(得分:2)

您可以将OnChildClickListener用于ExpandableListView

ExpandListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
    public void onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
        Object e = (Object)adapter.getChild(groupPosition, childPosition);
        //doing some work for child
    }
}  

严肃地说,你最好先搜索android引用

http://developer.android.com/reference/android/widget/ExpandableListView.OnChildClickListener.html