Expandandable listview with child items at root

后端 未结 1 1314
轮回少年
轮回少年 2020-12-22 08:23

I\'m trying to make a listview with the following structure:

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


        
相关标签:
1条回答
  • 2020-12-22 08:31

    I have already worked with such a problem and here's the approach i used:

    And created a class object to be used as a left menu item for both children and parent views

    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;
        }
    
    }
    

    and created my expandable list item which will contain a leftmenuitem parent and arraylist of children if found

    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;
        }   
    
    }
    

    Then i handledExpandableListView onChildClickListener and onGroupClickListener as following

    // 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;
                }
            });
    

    Then for the following

    ExpandableListView:
         group
         group
         group
              child
              child
         group
              child
              child
    

    You will create the following menu items:

    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 ));
    

    This way, you have handled both children and group positions to take different actions.

    I hope that this helps.

    0 讨论(0)
提交回复
热议问题