2013-11-10 61 views
0

我正在使用顯示元素列表的GUI。確定可摺疊樹中的某個節點是否隱藏

所有的元素都在一個維迭代數組,因此顯示它們通常會是這個樣子:

foreach (Element e: elements) { 
     display.Display(e); 
    } 

我現在需要一種方法來組織一個樹狀結構中的元素就像這個例子: enter image description here

在我的系統中,「文件夾」元素和「文件」元素沒有區別,但我可以訪問元素的「深度」和「isExpanded」值。

如何根據迭代前一個元素的數據來確定是否顯示元素?

回答

0

我想我已經想通了,但可能有某些情況下搞砸了:

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; 
    } 
}  
相關問題