Skip to content

Commit

Permalink
Add a reference of TreeView to binder
Browse files Browse the repository at this point in the history
  • Loading branch information
Xinyuan committed May 3, 2017
1 parent 35cc975 commit 3770a19
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 6 deletions.
2 changes: 1 addition & 1 deletion treeview_lib/src/main/java/me/texy/treeview/TreeNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
18 changes: 15 additions & 3 deletions treeview_lib/src/main/java/me/texy/treeview/TreeView.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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) {
Expand All @@ -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;
Expand Down Expand Up @@ -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;
}
Expand All @@ -98,7 +101,7 @@ public void expandAll() {
}


private void refreshTreeView() {
public void refreshTreeView() {
if (rootView != null) {
((TreeViewAdapter) rootView.getAdapter()).refreshView();
}
Expand Down Expand Up @@ -196,4 +199,13 @@ public void deselectAll() {
public List<TreeNode> getSelectedNodes() {
return TreeHelper.getSelectedNodes(root);
}

public boolean isItemSelectable() {
return itemSelectable;
}

public void setItemSelectable(boolean itemSelectable) {
this.itemSelectable = itemSelectable;
}

}
11 changes: 10 additions & 1 deletion treeview_lib/src/main/java/me/texy/treeview/TreeViewAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -285,4 +290,8 @@ public void deleteNode(TreeNode node) {
}
notifyItemRemoved(index);
}

public void setTreeView(TreeView treeView) {
this.treeView = treeView;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand All @@ -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
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

import android.view.View;

import me.texy.treeview.TreeNode;

/**
* Created by xinyuanzhong on 2017/4/27.
*/
Expand All @@ -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*/
}
}
20 changes: 20 additions & 0 deletions treeview_lib/src/main/java/me/texy/treeview/helper/TreeHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -283,4 +283,24 @@ public static List<TreeNode> 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<TreeNode> children = treeNode.getChildren();
for (TreeNode child : children) {
if (child.isSelected() || hasOneSelectedNodeAtLeast(child)) {
return true;
}
}
return false;
}
}

0 comments on commit 3770a19

Please sign in to comment.