开发者

Android Expandable List - OnClickListener

I'm having trouble with setting an OnClickListener to list items in my ExpandableListActivity. The code structure matches the following example:

    ExpandableListAdapter mAdapter;

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // Assign the adapter
    mAdapter = new MyExpandableListAdapter();
    setListAdapter(mAdapter);
}

public class MyExpandableListAdapter extends BaseExpandableListAdapter {
    private String[] groups = { "foo", "bar"};
    private String[][] children = {
                {"fooA", "barA"},
        {"fooB", "barB"}
            };

    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 getGenericVi开发者_开发技巧ew() {
        // Layout parameters for the ExpandableListView
        AbsListView.LayoutParams lp = new AbsListView.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT, 64);

        TextView textView = new TextView(Calculations.this);
        textView.setLayoutParams(lp);
        // Center the text vertically
        textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
        // Set the text starting position
        textView.setPadding(64, 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());
        textView.setPadding(98, 0, 0, 0);
        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;
    }
}

Once I'm able to set the listener, I'll most likely do something like this:

// Listener
{
    switch(groupPosition) {
    case 0:
        switch(childPosition) {
        case 0:
            selected = 00;
            return true;
        }
    }
}// Close listener

Please tell me if I'm not being clear enough.


You should override onChildClick() in your activity.

boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) Override this for receiving callbacks when a child has been clicked.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜