Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added capability to add new node at any position (not just last) #135

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ android {
minifyEnabled false
}
}
buildToolsVersion '25.0.0'
}

dependencies {
Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ buildscript {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:1.0.0'
classpath 'com.android.tools.build:gradle:2.3.3'

// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
Expand Down
4 changes: 2 additions & 2 deletions gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#Wed Apr 10 15:27:10 PDT 2013
#Mon Sep 18 14:38:39 IST 2017
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-2.2.1-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-3.3-all.zip
1 change: 1 addition & 0 deletions library/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ android {
minifyEnabled false
}
}
buildToolsVersion '25.0.0'
}

dependencies {
Expand Down
17 changes: 17 additions & 0 deletions library/src/main/java/com/unnamed/b/atv/model/TreeNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,23 @@ public TreeNode addChild(TreeNode childNode) {
return this;
}

/**
* Adds a child node at give position
* @param childNode
* @param position Fist position is 0. If -1, child is inserted at the end
* @return
*/
public TreeNode addChild(TreeNode childNode, int position) {
childNode.mParent = this;
childNode.mId = generateId();

if(position == -1 || position > children.size())
position = childNode.size();

children.add(position, childNode);
return this;
}

public TreeNode addChildren(TreeNode... nodes) {
for (TreeNode n : nodes) {
addChild(n);
Expand Down
35 changes: 30 additions & 5 deletions library/src/main/java/com/unnamed/b/atv/view/AndroidTreeView.java
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ private void expandNode(final TreeNode node, boolean includeSubnodes) {
parentViewHolder.toggle(true);

for (final TreeNode n : node.getChildren()) {
addNode(parentViewHolder.getNodeItemsView(), n);
addNode(parentViewHolder.getNodeItemsView(), n, -1);

if (n.isExpanded() || includeSubnodes) {
expandNode(n, includeSubnodes);
Expand All @@ -253,10 +253,20 @@ private void expandNode(final TreeNode node, boolean includeSubnodes) {

}

private void addNode(ViewGroup container, final TreeNode n) {
/**
* Add the view corresponding to TreeNode to the View Group
* @param container
* @param n
*/
private void addNode(ViewGroup container, final TreeNode n, int index) {
final TreeNode.BaseNodeViewHolder viewHolder = getViewHolderForNode(n);
final View nodeView = viewHolder.getView();
container.addView(nodeView);

if(index == -1)
container.addView(nodeView);
else
container.addView(nodeView, index);

if (mSelectionModeEnabled) {
viewHolder.toggleSelectionMode(mSelectionModeEnabled);
}
Expand Down Expand Up @@ -467,11 +477,26 @@ public boolean willChangeBounds() {
//-----------------------------------------------------------------
//Add / Remove

/**
* Add the node in the end
* @param parent
* @param nodeToAdd
*/
public void addNode(TreeNode parent, final TreeNode nodeToAdd) {
parent.addChild(nodeToAdd);
addNode(parent, nodeToAdd, -1);
}

/**
* Adds the node at given position
* @param parent
* @param nodeToAdd
* @param position if -1, node is add in the last
*/
public void addNode(TreeNode parent, final TreeNode nodeToAdd, int position) {
parent.addChild(nodeToAdd, position);
if (parent.isExpanded()) {
final TreeNode.BaseNodeViewHolder parentViewHolder = getViewHolderForNode(parent);
addNode(parentViewHolder.getNodeItemsView(), nodeToAdd);
addNode(parentViewHolder.getNodeItemsView(), nodeToAdd, position);
}
}

Expand Down