问题描述:

I'm working on a GUI that displays a list of elements.

All the elements are in a one dimensional iterable array, so displaying them would normally look something like this:

 foreach (Element e: elements) {

display.Display(e);

}

I now need a way to organize the elements in a tree structure like in this example:

In my system, there is no distinction between "folder" elements and "file" elements, but I can access an element's 'depth' and 'isExpanded' values.

How can I determine whether an element should be displayed based on data taken from iterating through previous elements?

网友答案:

I think I've figured it out, but there may be some cases that mess it up:

bool prevIsCollapsed = false;
int collapsedPropertyDepth = 0;

// iterate through each property of this component
for (Property p : properties)
{
    int depth = property.depth;
    if (prevIsCollapsed && depth > collapsedPropertyDepth)
    {
        // dont display this property
        continue;
    }

    if (!property.isExpanded)
    {
        prevIsCollapsed = true;
        collapsedPropertyDepth = depth;
    }
    else
    {
        prevIsCollapsed = false;
    }
}    
相关阅读:
Top