diff --git a/treeview_lib/src/main/java/me/texy/treeview/TreeNode.java b/treeview_lib/src/main/java/me/texy/treeview/TreeNode.java index e3ed605..9fef977 100644 --- a/treeview_lib/src/main/java/me/texy/treeview/TreeNode.java +++ b/treeview_lib/src/main/java/me/texy/treeview/TreeNode.java @@ -59,7 +59,7 @@ public void addChild(TreeNode treeNode) { public void removeChild(TreeNode treeNode) { - if (treeNode == null || treeNode.getChildren().size() < 1) { + if (treeNode == null || getChildren().size() < 1) { return; } if (getChildren().indexOf(treeNode) != -1) { diff --git a/treeview_lib/src/main/java/me/texy/treeview/TreeView.java b/treeview_lib/src/main/java/me/texy/treeview/TreeView.java index 7428a5f..404f70a 100644 --- a/treeview_lib/src/main/java/me/texy/treeview/TreeView.java +++ b/treeview_lib/src/main/java/me/texy/treeview/TreeView.java @@ -23,10 +23,10 @@ import java.util.List; +import me.texy.treeview.animator.TreeItemAnimator; import me.texy.treeview.base.BaseNodeViewFactory; import me.texy.treeview.base.SelectableTreeAction; import me.texy.treeview.helper.TreeHelper; -import me.texy.treeview.animator.TreeItemAnimator; /** * Created by xinyuanzhong on 2017/4/20. @@ -43,6 +43,8 @@ public class TreeView implements SelectableTreeAction { private TreeViewAdapter adapter; + private boolean itemSelectable = true; + public void setItemAnimator(RecyclerView.ItemAnimator itemAnimator) { this.itemAnimator = itemAnimator; if (rootView != null && itemAnimator != null) { @@ -52,7 +54,7 @@ public void setItemAnimator(RecyclerView.ItemAnimator itemAnimator) { private RecyclerView.ItemAnimator itemAnimator; - public TreeView(@NonNull TreeNode root, @NonNull Context context,@NonNull BaseNodeViewFactory baseNodeViewFactory) { + public TreeView(@NonNull TreeNode root, @NonNull Context context, @NonNull BaseNodeViewFactory baseNodeViewFactory) { this.root = root; this.context = context; this.baseNodeViewFactory = baseNodeViewFactory; @@ -83,6 +85,7 @@ private RecyclerView buildRootView() { recyclerView.setLayoutManager(new LinearLayoutManager(context)); adapter = new TreeViewAdapter(context, root, baseNodeViewFactory); + adapter.setTreeView(this); recyclerView.setAdapter(adapter); return recyclerView; } @@ -98,7 +101,7 @@ public void expandAll() { } - private void refreshTreeView() { + public void refreshTreeView() { if (rootView != null) { ((TreeViewAdapter) rootView.getAdapter()).refreshView(); } @@ -196,4 +199,13 @@ public void deselectAll() { public List getSelectedNodes() { return TreeHelper.getSelectedNodes(root); } + + public boolean isItemSelectable() { + return itemSelectable; + } + + public void setItemSelectable(boolean itemSelectable) { + this.itemSelectable = itemSelectable; + } + } diff --git a/treeview_lib/src/main/java/me/texy/treeview/TreeViewAdapter.java b/treeview_lib/src/main/java/me/texy/treeview/TreeViewAdapter.java index afa8d97..301505e 100644 --- a/treeview_lib/src/main/java/me/texy/treeview/TreeViewAdapter.java +++ b/treeview_lib/src/main/java/me/texy/treeview/TreeViewAdapter.java @@ -56,6 +56,8 @@ public class TreeViewAdapter extends RecyclerView.Adapter { */ private View EMPTY_PARAMETER; + private TreeView treeView; + public TreeViewAdapter(Context context, TreeNode root, @NonNull BaseNodeViewFactory baseNodeViewFactory) { this.context = context; @@ -99,7 +101,9 @@ public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int level) { View view = LayoutInflater.from(context).inflate(baseNodeViewFactory .getNodeViewBinder(EMPTY_PARAMETER, level).getLayoutId(), parent, false); - return baseNodeViewFactory.getNodeViewBinder(view, level); + BaseNodeViewBinder nodeViewBinder = baseNodeViewFactory.getNodeViewBinder(view, level); + nodeViewBinder.setTreeView(treeView); + return nodeViewBinder; } @Override @@ -143,6 +147,7 @@ public void onClick(View v) { public void onClick(View v) { boolean checked = checkableView.isChecked(); selectNode(checked, treeNode); + ((CheckableNodeViewBinder) viewBinder).onNodeSelectedChanged(treeNode, checked); } }); } else { @@ -285,4 +290,8 @@ public void deleteNode(TreeNode node) { } notifyItemRemoved(index); } + + public void setTreeView(TreeView treeView) { + this.treeView = treeView; + } } diff --git a/treeview_lib/src/main/java/me/texy/treeview/base/BaseNodeViewBinder.java b/treeview_lib/src/main/java/me/texy/treeview/base/BaseNodeViewBinder.java index 3bab077..2c3cf35 100644 --- a/treeview_lib/src/main/java/me/texy/treeview/base/BaseNodeViewBinder.java +++ b/treeview_lib/src/main/java/me/texy/treeview/base/BaseNodeViewBinder.java @@ -18,17 +18,27 @@ import android.view.View; import me.texy.treeview.TreeNode; +import me.texy.treeview.TreeView; /** * Created by zxy on 17/4/23. */ public abstract class BaseNodeViewBinder extends RecyclerView.ViewHolder { + /** + * This reference of TreeView make BaseNodeViewBinder has the ability + * to expand node or select node. + */ + protected TreeView treeView; public BaseNodeViewBinder(View itemView) { super(itemView); } + public void setTreeView(TreeView treeView) { + this.treeView = treeView; + } + /** * Get node item layout id * @@ -42,6 +52,7 @@ public BaseNodeViewBinder(View itemView) { * @param treeNode Node data */ public abstract void bindView(TreeNode treeNode); + /** * if you do not want toggle the node when click whole item view,then you can assign a view to * trigger the toggle action @@ -53,7 +64,7 @@ public int getToggleTriggerViewId() { } /** - * Callback when a toggle action happened + * Callback when a toggle action happened (only by clicked) * * @param treeNode The toggled node * @param expand Expanded or collapsed diff --git a/treeview_lib/src/main/java/me/texy/treeview/base/CheckableNodeViewBinder.java b/treeview_lib/src/main/java/me/texy/treeview/base/CheckableNodeViewBinder.java index aae7f6b..d72a722 100644 --- a/treeview_lib/src/main/java/me/texy/treeview/base/CheckableNodeViewBinder.java +++ b/treeview_lib/src/main/java/me/texy/treeview/base/CheckableNodeViewBinder.java @@ -16,6 +16,8 @@ import android.view.View; +import me.texy.treeview.TreeNode; + /** * Created by xinyuanzhong on 2017/4/27. */ @@ -32,4 +34,14 @@ public CheckableNodeViewBinder(View itemView) { * @return */ public abstract int getCheckableViewId(); + + /** + * Do something when a node select or deselect(only triggered by clicked) + * + * @param treeNode + * @param selected + */ + public void onNodeSelectedChanged(TreeNode treeNode, boolean selected) { + /*empty*/ + } } diff --git a/treeview_lib/src/main/java/me/texy/treeview/helper/TreeHelper.java b/treeview_lib/src/main/java/me/texy/treeview/helper/TreeHelper.java index 785d758..ad40a18 100644 --- a/treeview_lib/src/main/java/me/texy/treeview/helper/TreeHelper.java +++ b/treeview_lib/src/main/java/me/texy/treeview/helper/TreeHelper.java @@ -283,4 +283,24 @@ public static List getSelectedNodes(TreeNode treeNode) { } return selectedNodes; } + + /** + * Return true when the node has one selected child(recurse all children) at least, + * otherwise return false + * + * @param treeNode + * @return + */ + public static boolean hasOneSelectedNodeAtLeast(TreeNode treeNode) { + if (treeNode == null || treeNode.getChildren().size() == 0) { + return false; + } + List children = treeNode.getChildren(); + for (TreeNode child : children) { + if (child.isSelected() || hasOneSelectedNodeAtLeast(child)) { + return true; + } + } + return false; + } }