bmelnychuk / AndroidTreeView

AndroidTreeView. TreeView implementation for android
Apache License 2.0
3k stars 620 forks source link

How to collapse other node expect current node clicked #156

Open vinhgroup opened 6 years ago

vinhgroup commented 6 years ago

i used this liblary. everything ok. but how to How to collapse other node expect current node clicked.

thanks in advance

brunopizzo commented 5 years ago

I found a solution

1) you create a class like that

public class UtilityHolder {

public static void collapseOthers(TreeNode mNode, AndroidTreeView treeView){
    if(mNode.isExpanded()) {
        TreeNode parent = mNode.getParent();
        for (TreeNode node: parent.getChildren()) {
            if(node != mNode){
                treeView.collapseNode(node);
                collapseChildern(node,treeView);
            }

        }
    }
}

private static void collapseChildern(TreeNode mNode, AndroidTreeView treeView) {
    if (!mNode.getChildren().isEmpty()) {
        for (TreeNode node : mNode.getChildren()) {
            treeView.collapseNode(node);
            collapseChildern(node,treeView);
        }
    }
}

}

2) you call the method collapseChildern in your Holder

@Override public void toggle(boolean active) { UtilityHolder.collapseOthers(mNode,treeView); }