diff --git a/app/src/main/java/com/chad/baserecyclerviewadapterhelper/activity/node/NodeTreeUseActivity.java b/app/src/main/java/com/chad/baserecyclerviewadapterhelper/activity/node/NodeTreeUseActivity.java index 3e6a62e2a..6f3e65cc4 100644 --- a/app/src/main/java/com/chad/baserecyclerviewadapterhelper/activity/node/NodeTreeUseActivity.java +++ b/app/src/main/java/com/chad/baserecyclerviewadapterhelper/activity/node/NodeTreeUseActivity.java @@ -1,6 +1,7 @@ package com.chad.baserecyclerviewadapterhelper.activity.node; import android.os.Bundle; +import android.widget.Toast; import androidx.recyclerview.widget.LinearLayoutManager; import androidx.recyclerview.widget.RecyclerView; @@ -39,11 +40,15 @@ protected void onCreate(Bundle savedInstanceState) { @Override public void run() { SecondNode seNode = new SecondNode(new ArrayList(), "Second Node(This is added)"); + SecondNode seNode2 = new SecondNode(new ArrayList(), "Second Node(This is added)"); + List nodes = new ArrayList<>(); + nodes.add(seNode); + nodes.add(seNode2); //第一个夫node,位置为子node的3号位置 - adapter.nodeAddData(adapter.getData().get(0), 3, seNode); - Tips.show("新增node"); + adapter.nodeAddData(adapter.getData().get(0), 2, nodes); + Tips.show("新插入了两个node", Toast.LENGTH_LONG); } - }, 3000); + }, 2000); } private List getEntity() { diff --git a/library/src/main/java/com/chad/library/adapter/base/BaseNodeAdapter.kt b/library/src/main/java/com/chad/library/adapter/base/BaseNodeAdapter.kt index b0352a37a..21c6ff7a5 100644 --- a/library/src/main/java/com/chad/library/adapter/base/BaseNodeAdapter.kt +++ b/library/src/main/java/com/chad/library/adapter/base/BaseNodeAdapter.kt @@ -218,6 +218,19 @@ abstract class BaseNodeAdapter(data: MutableList? = null) } } + fun nodeAddData(parentNode: BaseNode, childIndex: Int, newData: Collection) { + parentNode.childNode?.let { + it.addAll(childIndex, newData) + + if (parentNode is BaseExpandNode && !parentNode.isExpanded) { + return + } + val parentIndex = this.data.indexOf(parentNode) + val pos = parentIndex + 1 + childIndex + addData(parentIndex + pos, newData) + } + } + /** * 对指定的父node下对子node进行移除 * @param parentNode BaseNode 夫node