Expandandable列表视图与在根子项 [英] Expandandable listview with child items at root

查看:144
本文介绍了Expandandable列表视图与在根子项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图让具有以下结构的列表视图:

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

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

我怎么会去acomplishing呢?

How would I go about acomplishing this?

我一定要得到一组行事像一个孩子或者是有可能做正确像这样:

Do I have to get a group to act like a child or is it possible to do it properly like so:

我有一个对象MenuRow

I have an object MenuRow

public class MenuRow {
public String title;

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

}

然后我有MENUGROUP和MenuChild扩展MenuRow,这可以让我做到以下几点:

then I have the MenuGroup and MenuChild that extend MenuRow, this allows me to do the following:

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

我似乎无法弄清楚如何,我应该去了解我的适配器楼内设有商务。

I can't seem to figure how I should go about my buisness in the adapter.

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

this method for example isn't really adapted to my solution. How should I go about trying to accomplish my goal? do I need to use a different adapter? (my current one extend BaseExpandableListAdapter)

推荐答案

我已经使用过这样的问题,下面是我使用的方法:

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

}

和创建我的扩展列表项目将包含儿童的leftmenuitem父母和ArrayList,如果发现

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

}

然后我办理 ExpandableListView onChildClickListener和onGroupClickListener如下

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

那么以下

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

这样的话,你已经处理儿童和集团的立场,以采取不同的行动。

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

我希望这会有所帮助。

这篇关于Expandandable列表视图与在根子项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆