我已經有這樣的問題工作,這是我所使用的方法:
並創建一個類的對象,以用作兒童和家長的意見
左側的菜單項
public class LeftMenuItem {
int mTextId;
int mImageId;
//Action to be taken after clicking on the item
String mAction;
public LeftMenuItem(int textId, int imageId,String action) {
this.mTextId = textId;
this.mImageId = imageId;
this.mAction = action;
}
public int getTextId() {
return mTextId;
}
public void setTextId(int textId) {
this.mTextId = textId;
}
public int getImageId() {
return mImageId;
}
public void setImageId(int imageId) {
this.mImageId = imageId;
}
public String getAction() {
return mAction;
}
public void setAction(String action) {
this.mAction = action;
}
}
,創造了我的擴展列表項目將包含兒童的父母leftmenuitem和ArrayList如果發現
public class ExpandableLeftMenuItem {
LeftMenuItem mParentItem;
ArrayList<LeftMenuItem> mChildItems;
public ExpandableLeftMenuItem(LeftMenuItem parentItem,
ArrayList<LeftMenuItem> childItems) {
this.mParentItem = parentItem;
this.mChildItems = childItems;
}
public LeftMenuItem getParentItem() {
return mParentItem;
}
public void setParentItem(LeftMenuItem parentItem) {
this.mParentItem = parentItem;
}
public ArrayList<LeftMenuItem> getChildItems() {
return mChildItems;
}
public void setChildItems(ArrayList<LeftMenuItem> childItems) {
this.mChildItems = childItems;
}
}
然後我處理ExpandableListView
onChildClickListener和onGroupClickListener如下
// In Case of clicking on an item that has children, then get the action
// of this child item and show the screen with this action
mLeftMenu.setOnChildClickListener(new OnChildClickListener() {
@Override
public boolean onChildClick(ExpandableListView parent, View view, int groupPosition, int childPosition, long id) {
if (!mLeftMenuItems.get(groupPosition).getChildItems().isEmpty()) {
String action = ((LeftMenuItem) mLeftMenuItems.get(groupPosition).getChildItems().get(childPosition)).getAction();
setSelectedSection(action);
handleLeftMenuClick(action);
return true;
}
return false;
}
});
// In Case of clicking on an item that has no children, then get the
// action of this item and show the screen with this action
mLeftMenu.setOnGroupClickListener(new OnGroupClickListener() {
@Override
public boolean onGroupClick(ExpandableListView parent, View view, int groupPosition, long id) {
if (mLeftMenuItems.get(groupPosition).getChildItems().isEmpty()) {
String action = ((LeftMenuItem) mLeftMenuItems.get(groupPosition).getParentItem()).getAction();
setSelectedSection(action);
handleLeftMenuClick(action);
// Return true to stop parent from expanding or collapsing group
return true;
}
// Return true to handle click as parent by expanding or collapsing group
return false;
}
});
然後以下
ExpandableListView:
group
group
group
child
child
group
child
child
您將創建下列菜單項:
ArrayList<ExpandableLeftMenuItem> mMenuItems = new ArrayList<ExpandableLeftMenuItem>();
mMenuItems.add(new ExpandableLeftMenuItem(new LeftMenuItem(parent1Text,parent1Image,action1),new ArrayList<LeftMenuItem>()));
mMenuItems.add(new ExpandableLeftMenuItem(new LeftMenuItem(parent2Text,parent2Image,action2),new ArrayList<LeftMenuItem>()));
ArrayList<LeftMenuItem> parent3Children = new ArrayList<LeftMenuItem>();
parent3Children.add(new LeftMenuItem(parent3Child1TextId,parent3Child1ImageId,parent3Child1action));
parent3Children.add(new LeftMenuItem(parent3Child2TextId,parent3Child2ImageId,parent3Child2action));
mMenuItems.add(new ExpandableLeftMenuItem(new LeftMenuItem(parent1Text,parent1Image,action1),parent3Children));
這樣一來,你不僅處理兒童和小組職位採取不同的行動。
我希望這有助於。
是的我終於想出了相同的解決方案,我目前正在尋找一種方法,只有一個onclicklistener,然後確定該行是一個組或小孩'instanceof',但我不知道這是可能的 –