我真的不确定如何从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;
}
最佳答案
尝试覆盖getChildView()
类中的MyExpandableListAdapter
。每当触摸孩子时(或者选中或取消选中其复选框时,都会调用getChildView
-我不记得是哪个,还是有区别)。在该方法中,您可以访问groupPosition和childPosition。有关更多信息,请参见Working with children in ExpandableListActivity。
关于java - 如何从android中的ExpandableListView获取onChildClick,groupPosition和childPosition?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/13371230/