【问题标题】:Expandandable listview with child items at root根目录下带有子项的可扩展列表视图
【发布时间】:2014-02-12 12:51:34
【问题描述】:

我正在尝试制作具有以下结构的列表视图:

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

我将如何去完成这个?

我是否必须让一个小组像孩子一样行事,或者是否可以像这样正确地做到这一点:

我有一个对象 MenuRow

public class MenuRow {
public String title;

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

}

然后我有扩展 MenuRow 的 MenuGroup 和 MenuChild,这允许我执行以下操作:

    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)

【问题讨论】:

    标签: android listview expandablelistview


    【解决方案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 父项和子项数组列表

    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”的组还是子级,但我是不确定这是可能的
    猜你喜欢
    • 2023-03-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多