1

我真的不确定如何从 ExpandableListView 获取 groupPosition 和 childPosition。我尝试使用开关和外壳,但这不起作用,它似乎无法识别 groupPosition。那么如何获取 groupPosition 和 childPosition 呢?

@Override
public void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    // Set up our adapter
    mAdapter = new MyExpandableListAdapter();
    setListAdapter(mAdapter);
    registerForContextMenu(getExpandableListView());

    getExpandableListView().setOnChildClickListener(new OnChildClickListener()
    {
        public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id)
        {
            switch(groupPosition & childPosition)
            {
            case 2 & 2:
                Intent asariIntent = new Intent(Codex.this, CodexAsari.class);
                Codex.this.startActivity(asariIntent);
                break;

            case 1 & 0:
                Intent protheanIntent = new Intent(Codex.this, CodexProthean.class);
                Codex.this.startActivity(protheanIntent);
                break;

            case 1 & 1:
                Intent rachniIntent = new Intent(Codex.this, CodexRachni.class);
                Codex.this.startActivity(rachniIntent);
                break;
            }
            return false;
        }
    });
}

@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
    menu.setHeaderTitle("Sample menu");
    menu.add(0, 0, 0, "Sample action");
}

@Override
public boolean onContextItemSelected(MenuItem item) {
    ExpandableListContextMenuInfo info = (ExpandableListContextMenuInfo) item.getMenuInfo();

    String title = ((TextView) info.targetView).getText().toString();

    int type = ExpandableListView.getPackedPositionType(info.packedPosition);
    if (type == ExpandableListView.PACKED_POSITION_TYPE_CHILD) {
        int groupPos = ExpandableListView.getPackedPositionGroup(info.packedPosition); 
        int childPos = ExpandableListView.getPackedPositionChild(info.packedPosition); 
        Toast.makeText(this, title + ": Child " + childPos + " clicked in group " + groupPos,
                Toast.LENGTH_SHORT).show();
        return true;
    } else if (type == ExpandableListView.PACKED_POSITION_TYPE_GROUP) {
        int groupPos = ExpandableListView.getPackedPositionGroup(info.packedPosition); 
        Toast.makeText(this, title + ": Group " + groupPos + " clicked", Toast.LENGTH_SHORT).show();
        return true;
    }

    return false;
}

public class MyExpandableListAdapter extends BaseExpandableListAdapter
{
    public String[] groups = { "Aliens: Council Races", "Aliens: Extinct Races", "Aliens: Non-Council Races", "Aliens: Non-Sapient Creatures", "Citadel and Galactic Government", "Humanity and the Systems Alliance", "Known Associates" };
    private String[][] children = {
            { "Asari", "Salarians", "Turians" },
            { "Protheans", "Rachni" },
            { "Batarians", "Collectors", "Drell", "Elcor", "Geth", "Hanar", "Keepers", "Krogan", "Praetorians", "Quarians",
                "Reapers", "Reapers: Indoctrination", "Sovereign", "Volus", "Vorcha" },
            { "Husks", "Thresher Maws", "Varren" },
            { "Citadel", "Citadel Council", "Citadel Space", "Spectres" },
            { "Councilor Donnel Udina: Anderson Chosen Councilor", "Councilor Donnel Udina:  Undina Chosen Councilor", "Earth: Mass Effect", "Earth: Mass Effect 3", "First Contact War", "Systems Alliance", "Rise of the Alliance: Renegade", "Rise of the Alliance: Paragon" },
            { "Steven Hackett", "David Anderson", "Udina Chosen Councilor", "Dr. Liara T'Soni", "EDI", "Garrus Vakarian", "Kaidan Alenko", "Ashley Williams", "Tali'Zorah", "Dr. Mordin Solus", "Jacob Taylor", "Jeff 'Joker' Moreau", "Miranda Lawson", "Dr. Karin Chakwas" }
    };

    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.MATCH_PARENT, 64);

        TextView textView = new TextView(Codex.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;
    }



}

public int childPosition()
{
    return 0;
}
4

2 回答 2

0

我得说,我从未见过这样的案例陈述。也许您可以尝试以下方法,看看是否得到任何不同的结果:

getExpandableListView().setOnChildClickListener(new OnChildClickListener() {
    public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
        switch(groupPosition) {
        case 1: 
            switch (childPosition) {
            case 0: 
                Intent protheanIntent = new Intent(Codex.this, CodexProthean.class);
                Codex.this.startActivity(protheanIntent);
                break;              
            case 1:
                Intent rachniIntent = new Intent(Codex.this, CodexRachni.class);
                Codex.this.startActivity(rachniIntent);
                break;
            }
        case 2:
            switch (childPosition) {
            case 2: 
                Intent asariIntent = new Intent(Codex.this, CodexAsari.class);
                Codex.this.startActivity(asariIntent);
                break;
            }
        }
        return false;
    }
});
于 2012-11-14T00:55:23.523 回答
0

尝试getChildView()在您的MyExpandableListAdapter班级中覆盖。getChildView每当一个孩子被触摸时(或者当它的复选框被选中或未选中时——我不记得是哪个,或者是否有区别),就会被调用。在该方法中,您可以访问 groupPosition 和 childPosition。有关更多信息,请参阅在 ExpandableListActivity 中处理子项。

于 2012-11-14T00:55:42.580 回答