Skip to content

Commit

Permalink
add nodeAddData();
Browse files Browse the repository at this point in the history
can't run
  • Loading branch information
limuyang committed Jan 2, 2020
1 parent dd6bbba commit aab0b7b
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,14 @@
import com.chad.baserecyclerviewadapterhelper.entity.node.tree.FirstNode;
import com.chad.baserecyclerviewadapterhelper.entity.node.tree.SecondNode;
import com.chad.baserecyclerviewadapterhelper.entity.node.tree.ThirdNode;
import com.chad.baserecyclerviewadapterhelper.utils.Tips;
import com.chad.library.adapter.base.entity.node.BaseNode;

import java.util.ArrayList;
import java.util.List;

public class NodeTreeUseActivity extends BaseActivity {
private RecyclerView mRecyclerView;
private RecyclerView mRecyclerView;
private NodeTreeAdapter adapter = new NodeTreeAdapter();

@Override
Expand All @@ -32,6 +33,15 @@ protected void onCreate(Bundle savedInstanceState) {
mRecyclerView.setAdapter(adapter);

adapter.setNewData(getEntity());

mRecyclerView.postDelayed(new Runnable() {
@Override
public void run() {
SecondNode seNode = new SecondNode(new ArrayList<BaseNode>(), "this is Add Second Node");
adapter.nodeAddData(adapter.getData().get(0), 1,seNode);
Tips.show("新增node");
}
}, 3000);
}

private List<BaseNode> getEntity() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,42 @@ abstract class BaseNodeAdapter(data: MutableList<BaseNode>? = null)
super.setDiffNewData(diffResult, flatData(newData))
}

/*************************** Node 数据操作 ***************************/

/**
* 对指定的父node,添加子node
* @param parentNode BaseNode 父node
* @param data BaseNode 子node
*/
fun nodeAddData(parentNode: BaseNode, data: BaseNode) {
val parentIndex = this.data.indexOf(parentNode)
var childIndex = 0
parentNode.childNode?.let {
it.add(data)
childIndex = it.size

addData(parentIndex + childIndex, data)
}
}

/**
* 对指定的父node,在指定位置添加添加子node
* @param parentNode BaseNode 父node
* @param position Int 此位置是相对于其childNodes数据的位置!并不是整个data
* @param data BaseNode 添加的数据
*/
fun nodeAddData(parentNode: BaseNode, position: Int, data: BaseNode) {
val parentIndex = this.data.indexOf(parentNode)
var pos = 0
parentNode.childNode?.let {
it.add(position, data)
pos = parentIndex + position + 1

addData(parentIndex + pos, data)
}

}

/*************************** 重写数据设置方法 END ***************************/

/**
Expand Down

0 comments on commit aab0b7b

Please sign in to comment.