我真的不确定如何从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;
}
答案 0 :(得分: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;
}
});
答案 1 :(得分:0)
尝试覆盖getChildView()
课程中的MyExpandableListAdapter
。每当触及一个孩子时(或当其复选框被选中或取消选中时,我都会调用getChildView
- 我不记得是哪个,或者是否存在差异)。在该方法中,您可以访问groupPosition和childPosition。有关详细信息,请参阅Working with children in ExpandableListActivity。