2014-02-12 65 views
0

子項中,我試圖與以下結構的列表視圖:Expandandable列表視圖與根

Listview 
    -> child 
    -> child 
    -> group 
      -> child 
      -> child 
    -> child 

我怎麼會去acomplishing呢?

我一定要得到一組表現得像一個孩子或者是有可能做正確像這樣:

我有一個對象MenuRow

public class MenuRow { 
public String title; 

public MenuRow(String title) { 
    this.title = title; 
} 

} 

然後我有MENUGROUP和MenuChild擴展MenuRow,這可以讓我做到以下幾點:

ArrayList<MenuRow> menu = new ArrayList<MenuRow>(); 
    menu.add(new MenuChild("row 1")); 
    menu.add(new MenuChild("row 2")); 
    MenuGroup group = new MenuGroup("group"); 
    MenuChild groupChild = new MenuChild("group child"); 
    group.items.add(groupChild); 
    menu.add(group); 

我似乎無法弄清楚如何我應該去我在適配器樓內設有商務。

@Override 
    public Object getChild(int groupPosition, int childPosition) { 
     MenuRow row = menu.get(groupPosition); 
     if (row instanceof MenuGroup) { 
      ArrayList<MenuChild> chList = ((MenuGroup) row).getItems(); 
     } 
     else{ 
       return null; 
     } 

     return chList; 
    } 

例如,這種方法並不適合我的解決方案。我應該如何去實現我的目標?我需要使用不同的適配器嗎? (我目前的一個延伸BaseExpandableListAdapter)

回答

1

我已經有這樣的問題工作,這是我所使用的方法:

並創建一個類的對象,以用作兒童和家長的意見

左側的菜單項
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)); 

這樣一來,你不僅處理兒童和小組職位採取不同的行動。

我希望這有助於。

+0

是的我終於想出了相同的解決方案,我目前正在尋找一種方法,只有一個onclicklistener,然後確定該行是一個組或小孩'instanceof',但我不知道這是可能的 –