├── .gitignore ├── README.md ├── app ├── .gitignore ├── build.gradle ├── proguard-rules.pro └── src │ ├── androidTest │ └── java │ │ └── com │ │ └── example │ │ └── myapplication │ │ └── ExampleInstrumentedTest.java │ ├── main │ ├── AndroidManifest.xml │ ├── java │ │ └── com │ │ │ └── example │ │ │ └── myapplication │ │ │ └── MainActivity.java │ └── res │ │ ├── drawable-v24 │ │ └── ic_launcher_foreground.xml │ │ ├── drawable │ │ └── ic_launcher_background.xml │ │ ├── layout │ │ ├── activity_main.xml │ │ ├── item_test_1.xml │ │ ├── item_test_2.xml │ │ └── item_test_3.xml │ │ ├── mipmap-anydpi-v26 │ │ ├── ic_launcher.xml │ │ └── ic_launcher_round.xml │ │ ├── mipmap-hdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-mdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-xhdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-xxhdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-xxxhdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ └── values │ │ ├── colors.xml │ │ ├── strings.xml │ │ └── styles.xml │ └── test │ └── java │ └── com │ └── example │ └── myapplication │ └── ExampleUnitTest.java ├── build.gradle ├── gradle.properties ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat ├── settings.gradle └── treenodelibrary ├── .gitignore ├── build.gradle ├── proguard-rules.pro └── src ├── androidTest └── java │ └── com │ └── library │ └── treenode │ └── ExampleInstrumentedTest.java ├── main ├── AndroidManifest.xml ├── java │ └── treenode │ │ ├── SimpleTreeNodeAdapter.java │ │ ├── TreeNode.java │ │ ├── TreeNodeAdapter.java │ │ ├── TreeNodeDelegate.java │ │ ├── TreeNodeHelper.java │ │ └── ViewHolder.java └── res │ ├── layout │ └── layout_tree_node.xml │ └── values │ └── strings.xml └── test └── java └── com └── library └── treenode └── ExampleUnitTest.java /.gitignore: -------------------------------------------------------------------------------- 1 | *.iml 2 | .gradle 3 | .idea 4 | /local.properties 5 | /.idea/caches 6 | /.idea/libraries 7 | /.idea/modules.xml 8 | /.idea/workspace.xml 9 | /.idea/navEditor.xml 10 | /.idea/assetWizardSettings.xml 11 | .DS_Store 12 | /build 13 | /captures 14 | .externalNativeBuild 15 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # TreeNodeLibrary 2 | 3 | 多级列表适配器依赖库 4 | 5 | ListView 适配器请移步[MultiLevelLibrary](https://github.com/JianxunMaster/MultiLevelLibrary) 6 | 7 | # 添加依赖 8 | 9 | Step 1. Add the JitPack repository to your build file 10 | 11 | Add it in your root build.gradle at the end of repositories: 12 | 13 | allprojects { 14 | repositories { 15 | ... 16 | maven { url 'https://jitpack.io' } 17 | } 18 | } 19 | 20 | Step 2. Add the dependency 21 | 22 | dependencies { 23 | implementation 'com.github.JianxunMaster:TreeNodeLibrary:1.0.0' 24 | } 25 | 26 | 27 | [![](https://jitpack.io/v/JianxunMaster/TreeNodeLibrary.svg)](https://jitpack.io/#JianxunMaster/TreeNodeLibrary) 28 | 29 | 30 | 31 | # 简介 32 | 33 | 适用于RecyclerView的多级列表适配器,用法简单 34 | 内置选中状态,可设置选中数量 35 | 36 | # 适配器控件介绍 37 | 38 | 通用适配器 TreeNodeAdapter 39 | 简单适配器 SimpleTreeNodeAdapter 40 | 层级样式管理 TreeNodeDelegate 41 | 树节点 TreeNode 42 | 43 | # 用法介绍 44 | 45 | 简单说明: 46 | 47 | 本依赖库只对适配器封装,其他的设置照常使用 48 | 若只有一种item布局,使用SimpleTreeNodeAdapter 49 | 若有多种item布局,使用TreeNodeAdapter,依次添加item布局管理TreeNodeDelegate 50 | SimpleTreeNodeAdapter其实是TreeNodeAdapter添加了一个TreeNodeDelegate的实例。 51 | 52 | 具体使用如下: 53 | 54 | 1 数据源设置,注意泛型的使用,这里以String为例 55 | 56 | List> list = new ArrayList<>(); 57 | for (int i = 0; i < 10; i++) { 58 | TreeNode treeNode0 = new TreeNode<>("0级:" + i); 59 | list.add(treeNode0); 60 | for (int j = 0; j < 10; j++) { 61 | TreeNode treeNode1 = new TreeNode<>("1级:" + j); 62 | treeNode0.addChild(treeNode1); 63 | for (int k = 0; k < 10; k++) { 64 | TreeNode treeNode2 = new TreeNode<>("2级:" + k); 65 | treeNode1.addChild(treeNode2); 66 | } 67 | } 68 | } 69 | 70 | 2.1 设置相同布局的简单适配器 71 | 72 | recyclerView.setAdapter(new SimpleTreeNodeAdapter(this, R.layout.item_test_1, list) { 73 | @Override 74 | public void convert(final TreeNodeAdapter adapter, ViewHolder holder, final TreeNode treeNode) { 75 | LinearLayout testLayout = holder.getView(R.id.testLayout); 76 | testLayout.setPadding(treeNode.getLevel() * 50, 0, 0, 0); 77 | TextView textTv = holder.getView(R.id.textTv); 78 | textTv.setText(treeNode.getValue()); 79 | holder.getConvertView().setOnClickListener(new View.OnClickListener() { 80 | @Override 81 | public void onClick(View v) { 82 | adapter.expandOrCollapseTreeNode(treeNode); 83 | } 84 | }); 85 | } 86 | }); 87 | recyclerView.setLayoutManager(new LinearLayoutManager(this)); 88 | 89 | 2.2 设置多种布局的适配器 90 | 91 | TreeNodeAdapter adapter = new TreeNodeAdapter<>(this, list); 92 | adapter.addItemViewDelegate(new TreeNodeDelegate() { 93 | @Override 94 | public boolean isItemType(TreeNode treeNode) { 95 | return treeNode.isRoot(); 96 | } 97 | 98 | @Override 99 | public int getLayoutId() { 100 | return R.layout.item_test_1; 101 | } 102 | 103 | @Override 104 | public void convert(ViewHolder holder, final TreeNode treeNode) { 105 | LinearLayout testLayout = holder.getView(R.id.testLayout); 106 | testLayout.setPadding(treeNode.getLevel() * 50, 0, 0, 0); 107 | TextView textTv = holder.getView(R.id.textTv); 108 | textTv.setText(treeNode.getValue()); 109 | holder.getConvertView().setOnClickListener(new View.OnClickListener() { 110 | @Override 111 | public void onClick(View v) { 112 | adapter.expandOrCollapseTreeNode(treeNode); 113 | } 114 | }); 115 | } 116 | }); 117 | adapter.addItemViewDelegate(new TreeNodeDelegate() { 118 | @Override 119 | public boolean isItemType(TreeNode treeNode) { 120 | return !treeNode.isRoot() && !treeNode.isLeaf(); 121 | } 122 | 123 | @Override 124 | public int getLayoutId() { 125 | return R.layout.item_test_2; 126 | } 127 | 128 | @Override 129 | public void convert(ViewHolder holder, final TreeNode treeNode) { 130 | LinearLayout testLayout = holder.getView(R.id.testLayout); 131 | testLayout.setPadding(treeNode.getLevel() * 50, 0, 0, 0); 132 | TextView textTv = holder.getView(R.id.textTv); 133 | textTv.setText(treeNode.getValue()); 134 | holder.getConvertView().setOnClickListener(new View.OnClickListener() { 135 | @Override 136 | public void onClick(View v) { 137 | adapter.expandOrCollapseTreeNode(treeNode); 138 | } 139 | }); 140 | } 141 | }); 142 | adapter.addItemViewDelegate(new TreeNodeDelegate() { 143 | @Override 144 | public boolean isItemType(TreeNode treeNode) { 145 | return treeNode.isLeaf(); 146 | } 147 | 148 | @Override 149 | public int getLayoutId() { 150 | return R.layout.item_test_3; 151 | } 152 | 153 | @Override 154 | public void convert(ViewHolder holder, final TreeNode treeNode) { 155 | LinearLayout testLayout = holder.getView(R.id.testLayout); 156 | testLayout.setPadding(treeNode.getLevel() * 50, 0, 0, 0); 157 | TextView textTv = holder.getView(R.id.textTv); 158 | textTv.setText(treeNode.getValue()); 159 | holder.getConvertView().setOnClickListener(new View.OnClickListener() { 160 | @Override 161 | public void onClick(View v) { 162 | adapter.expandOrCollapseTreeNode(treeNode); 163 | } 164 | }); 165 | } 166 | }); 167 | recyclerView.setAdapter(adapter); 168 | recyclerView.setLayoutManager(new LinearLayoutManager(this)); 169 | 170 | # 适配器的其他功能 171 | 172 | /** 173 | * 添加视图样式 174 | * 175 | * @param delegate 样式 176 | */ 177 | public void addItemViewDelegate(TreeNodeDelegate delegate) 178 | 179 | /** 180 | * 移除视图样式 181 | * 182 | * @param delegate 样式 183 | */ 184 | public void removeItemViewDelegate(TreeNodeDelegate delegate) 185 | 186 | /** 187 | * 刷新 188 | */ 189 | public void refreshTreeNode() 190 | 191 | /** 192 | * 展开树节点 193 | * 194 | * @param treeNode 树节点 195 | */ 196 | public void expandTreeNode(TreeNode treeNode) 197 | 198 | /** 199 | * 收缩树节点 200 | * 201 | * @param treeNode 树节点 202 | */ 203 | public void collapseTreeNode(TreeNode treeNode) 204 | 205 | /** 206 | * 伸缩书树节点 207 | * 208 | * @param treeNode 树节点 209 | */ 210 | public void expandOrCollapseTreeNode(TreeNode treeNode) 211 | 212 | /** 213 | * 刷新树节点 214 | * 215 | * @param treeNode 树节点 216 | */ 217 | public void notifyTreeNode(TreeNode treeNode) 218 | 219 | /** 220 | * 展开所有层级的树节点 221 | */ 222 | public void expandAllTreeNode() 223 | 224 | /** 225 | * 收缩所有层级的树节点 226 | */ 227 | public void collapseAllTreeNode() 228 | 229 | /** 230 | * 展开层级内的树节点 231 | * 232 | * @param level 层级 233 | */ 234 | public void expandLevelTreeNode(int level) 235 | 236 | /** 237 | * 设置可选树节点最大数量 238 | * 239 | * @param maxSelectCount 数量 240 | */ 241 | public void setMaxSelectCount(int maxSelectCount) 242 | 243 | /** 244 | * 获取选中树节点集合 245 | * 246 | * @return 选中树节点集合 247 | */ 248 | public List> getSelectTreeNodeList() 249 | 250 | /** 251 | * 改变选中状态,并刷新 252 | * 当限制最多选中数量,超出的部分会把先加入的树节点移除 253 | * 254 | * @param treeNode 树节点 255 | * @param select 选中状态 256 | */ 257 | public void selectTreeNode(TreeNode treeNode, boolean select) 258 | 259 | /** 260 | * 改变树节点选中状态 261 | * 262 | * @param treeNode 树节点 263 | */ 264 | public void changeSelectTreeNode(TreeNode treeNode) 265 | 266 | # 欢迎加群讨论 267 | 268 | QQ群号:464635057 269 | 270 | 个人开发,维护不易。如有问题,请多谅解。 ^_^ 271 | -------------------------------------------------------------------------------- /app/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /app/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | 3 | android { 4 | compileSdkVersion 28 5 | buildToolsVersion '28.0.3' 6 | defaultConfig { 7 | applicationId "com.example.myapplication" 8 | minSdkVersion 15 9 | targetSdkVersion 28 10 | versionCode 1 11 | versionName "1.0" 12 | testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" 13 | } 14 | buildTypes { 15 | release { 16 | minifyEnabled false 17 | proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' 18 | } 19 | } 20 | } 21 | 22 | dependencies { 23 | implementation fileTree(dir: 'libs', include: ['*.jar']) 24 | testImplementation 'junit:junit:4.12' 25 | implementation 'com.android.support:appcompat-v7:28.0.0' 26 | testImplementation 'junit:junit:4.12' 27 | implementation 'com.android.support:recyclerview-v7:28.0.0' 28 | implementation project(path: ':treenodelibrary') 29 | } 30 | -------------------------------------------------------------------------------- /app/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # You can control the set of applied configuration files using the 3 | # proguardFiles setting in build.gradle. 4 | # 5 | # For more details, see 6 | # http://developer.android.com/guide/developing/tools/proguard.html 7 | 8 | # If your project uses WebView with JS, uncomment the following 9 | # and specify the fully qualified class name to the JavaScript interface 10 | # class: 11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 12 | # public *; 13 | #} 14 | 15 | # Uncomment this to preserve the line number information for 16 | # debugging stack traces. 17 | #-keepattributes SourceFile,LineNumberTable 18 | 19 | # If you keep the line number information, uncomment this to 20 | # hide the original source file name. 21 | #-renamesourcefileattribute SourceFile 22 | -------------------------------------------------------------------------------- /app/src/androidTest/java/com/example/myapplication/ExampleInstrumentedTest.java: -------------------------------------------------------------------------------- 1 | package com.example.myapplication; 2 | 3 | import android.content.Context; 4 | 5 | import androidx.test.InstrumentationRegistry; 6 | import androidx.test.runner.AndroidJUnit4; 7 | 8 | import org.junit.Test; 9 | import org.junit.runner.RunWith; 10 | 11 | import static org.junit.Assert.*; 12 | 13 | /** 14 | * Instrumented test, which will execute on an Android device. 15 | * 16 | * @see Testing documentation 17 | */ 18 | @RunWith(AndroidJUnit4.class) 19 | public class ExampleInstrumentedTest { 20 | @Test 21 | public void useAppContext() { 22 | // Context of the app under test. 23 | Context appContext = InstrumentationRegistry.getTargetContext(); 24 | 25 | assertEquals("com.example.myapplication", appContext.getPackageName()); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | -------------------------------------------------------------------------------- /app/src/main/java/com/example/myapplication/MainActivity.java: -------------------------------------------------------------------------------- 1 | package com.example.myapplication; 2 | 3 | import android.os.Bundle; 4 | import android.support.v7.app.AppCompatActivity; 5 | import android.support.v7.widget.LinearLayoutManager; 6 | import android.support.v7.widget.RecyclerView; 7 | import android.view.View; 8 | import android.widget.LinearLayout; 9 | import android.widget.TextView; 10 | 11 | import java.util.ArrayList; 12 | import java.util.List; 13 | 14 | import treenode.TreeNode; 15 | import treenode.TreeNodeAdapter; 16 | import treenode.TreeNodeDelegate; 17 | import treenode.ViewHolder; 18 | 19 | public class MainActivity extends AppCompatActivity { 20 | RecyclerView recyclerView; 21 | List> list = new ArrayList<>(); 22 | 23 | @Override 24 | protected void onCreate(Bundle savedInstanceState) { 25 | super.onCreate(savedInstanceState); 26 | setContentView(R.layout.activity_main); 27 | recyclerView = findViewById(R.id.testView); 28 | for (int i = 0; i < 10; i++) { 29 | TreeNode treeNode0 = new TreeNode<>("0级:" + i); 30 | list.add(treeNode0); 31 | for (int j = 0; j < 10; j++) { 32 | TreeNode treeNode1 = new TreeNode<>("1级:" + j); 33 | treeNode0.addChild(treeNode1); 34 | for (int k = 0; k < 10; k++) { 35 | TreeNode treeNode2 = new TreeNode<>("2级:" + k); 36 | treeNode1.addChild(treeNode2); 37 | } 38 | } 39 | } 40 | TreeNodeAdapter adapter = new TreeNodeAdapter<>(this, list); 41 | adapter.addItemViewDelegate(new TreeNodeDelegate() { 42 | @Override 43 | public boolean isItemType(TreeNode treeNode) { 44 | return treeNode.isRoot(); 45 | } 46 | 47 | @Override 48 | public int getLayoutId() { 49 | return R.layout.item_test_1; 50 | } 51 | 52 | @Override 53 | public void convert(ViewHolder holder, final TreeNode treeNode) { 54 | LinearLayout testLayout = holder.getView(R.id.testLayout); 55 | testLayout.setPadding(treeNode.getLevel() * 50, 0, 0, 0); 56 | TextView textTv = holder.getView(R.id.textTv); 57 | textTv.setText(treeNode.getValue()); 58 | holder.getConvertView().setOnClickListener(new View.OnClickListener() { 59 | @Override 60 | public void onClick(View v) { 61 | adapter.expandOrCollapseTreeNode(treeNode); 62 | } 63 | }); 64 | } 65 | }); 66 | adapter.addItemViewDelegate(new TreeNodeDelegate() { 67 | @Override 68 | public boolean isItemType(TreeNode treeNode) { 69 | return !treeNode.isRoot() && !treeNode.isLeaf(); 70 | } 71 | 72 | @Override 73 | public int getLayoutId() { 74 | return R.layout.item_test_2; 75 | } 76 | 77 | @Override 78 | public void convert(ViewHolder holder, final TreeNode treeNode) { 79 | LinearLayout testLayout = holder.getView(R.id.testLayout); 80 | testLayout.setPadding(treeNode.getLevel() * 50, 0, 0, 0); 81 | TextView textTv = holder.getView(R.id.textTv); 82 | textTv.setText(treeNode.getValue()); 83 | holder.getConvertView().setOnClickListener(new View.OnClickListener() { 84 | @Override 85 | public void onClick(View v) { 86 | adapter.expandOrCollapseTreeNode(treeNode); 87 | } 88 | }); 89 | } 90 | }); 91 | adapter.addItemViewDelegate(new TreeNodeDelegate() { 92 | @Override 93 | public boolean isItemType(TreeNode treeNode) { 94 | return treeNode.isLeaf(); 95 | } 96 | 97 | @Override 98 | public int getLayoutId() { 99 | return R.layout.item_test_3; 100 | } 101 | 102 | @Override 103 | public void convert(ViewHolder holder, final TreeNode treeNode) { 104 | LinearLayout testLayout = holder.getView(R.id.testLayout); 105 | testLayout.setPadding(treeNode.getLevel() * 50, 0, 0, 0); 106 | TextView textTv = holder.getView(R.id.textTv); 107 | textTv.setText(treeNode.getValue()); 108 | holder.getConvertView().setOnClickListener(new View.OnClickListener() { 109 | @Override 110 | public void onClick(View v) { 111 | adapter.expandOrCollapseTreeNode(treeNode); 112 | } 113 | }); 114 | } 115 | }); 116 | recyclerView.setAdapter(adapter); 117 | recyclerView.setLayoutManager(new LinearLayoutManager(this)); 118 | // recyclerView.setAdapter(new SimpleTreeNodeAdapter(this, R.layout.item_test_1, list) { 119 | // @Override 120 | // public void convert(final TreeNodeAdapter adapter, ViewHolder holder, final TreeNode treeNode) { 121 | // LinearLayout testLayout = holder.getView(R.id.testLayout); 122 | // testLayout.setPadding(treeNode.getLevel() * 50, 0, 0, 0); 123 | // TextView textTv = holder.getView(R.id.textTv); 124 | // textTv.setText(treeNode.getValue()); 125 | // holder.getConvertView().setOnClickListener(new View.OnClickListener() { 126 | // @Override 127 | // public void onClick(View v) { 128 | // adapter.expandOrCollapseTreeNode(treeNode); 129 | // } 130 | // }); 131 | // } 132 | // }); 133 | // recyclerView.setLayoutManager(new LinearLayoutManager(this)); 134 | } 135 | } 136 | -------------------------------------------------------------------------------- /app/src/main/res/drawable-v24/ic_launcher_foreground.xml: -------------------------------------------------------------------------------- 1 | 7 | 12 | 13 | 19 | 22 | 25 | 26 | 27 | 28 | 34 | 35 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/ic_launcher_background.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | 10 | 15 | 20 | 25 | 30 | 35 | 40 | 45 | 50 | 55 | 60 | 65 | 70 | 75 | 80 | 85 | 90 | 95 | 100 | 105 | 110 | 115 | 120 | 125 | 130 | 135 | 140 | 145 | 150 | 155 | 160 | 165 | 170 | 171 | -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_main.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | 8 | 12 | -------------------------------------------------------------------------------- /app/src/main/res/layout/item_test_1.xml: -------------------------------------------------------------------------------- 1 | 2 | 8 | 9 | 17 | -------------------------------------------------------------------------------- /app/src/main/res/layout/item_test_2.xml: -------------------------------------------------------------------------------- 1 | 2 | 8 | 9 | 17 | -------------------------------------------------------------------------------- /app/src/main/res/layout/item_test_3.xml: -------------------------------------------------------------------------------- 1 | 2 | 8 | 9 | 17 | -------------------------------------------------------------------------------- /app/src/main/res/mipmap-anydpi-v26/ic_launcher.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | -------------------------------------------------------------------------------- /app/src/main/res/mipmap-anydpi-v26/ic_launcher_round.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | -------------------------------------------------------------------------------- /app/src/main/res/mipmap-hdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JianxunMaster/TreeNodeLibrary/e0b38e4e3edc23ce7080a6938fe0f53a9271bae3/app/src/main/res/mipmap-hdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-hdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JianxunMaster/TreeNodeLibrary/e0b38e4e3edc23ce7080a6938fe0f53a9271bae3/app/src/main/res/mipmap-hdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-mdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JianxunMaster/TreeNodeLibrary/e0b38e4e3edc23ce7080a6938fe0f53a9271bae3/app/src/main/res/mipmap-mdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-mdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JianxunMaster/TreeNodeLibrary/e0b38e4e3edc23ce7080a6938fe0f53a9271bae3/app/src/main/res/mipmap-mdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JianxunMaster/TreeNodeLibrary/e0b38e4e3edc23ce7080a6938fe0f53a9271bae3/app/src/main/res/mipmap-xhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xhdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JianxunMaster/TreeNodeLibrary/e0b38e4e3edc23ce7080a6938fe0f53a9271bae3/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JianxunMaster/TreeNodeLibrary/e0b38e4e3edc23ce7080a6938fe0f53a9271bae3/app/src/main/res/mipmap-xxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JianxunMaster/TreeNodeLibrary/e0b38e4e3edc23ce7080a6938fe0f53a9271bae3/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JianxunMaster/TreeNodeLibrary/e0b38e4e3edc23ce7080a6938fe0f53a9271bae3/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JianxunMaster/TreeNodeLibrary/e0b38e4e3edc23ce7080a6938fe0f53a9271bae3/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /app/src/main/res/values/colors.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | #008577 4 | #00574B 5 | #D81B60 6 | 7 | -------------------------------------------------------------------------------- /app/src/main/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | My Application 3 | 4 | -------------------------------------------------------------------------------- /app/src/main/res/values/styles.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /app/src/test/java/com/example/myapplication/ExampleUnitTest.java: -------------------------------------------------------------------------------- 1 | package com.example.myapplication; 2 | 3 | import org.junit.Test; 4 | 5 | import static org.junit.Assert.*; 6 | 7 | /** 8 | * Example local unit test, which will execute on the development machine (host). 9 | * 10 | * @see Testing documentation 11 | */ 12 | public class ExampleUnitTest { 13 | @Test 14 | public void addition_isCorrect() { 15 | assertEquals(4, 2 + 2); 16 | } 17 | } -------------------------------------------------------------------------------- /build.gradle: -------------------------------------------------------------------------------- 1 | // Top-level build file where you can add configuration options common to all sub-projects/modules. 2 | 3 | buildscript { 4 | repositories { 5 | google() 6 | jcenter() 7 | 8 | } 9 | dependencies { 10 | classpath 'com.android.tools.build:gradle:3.4.1' 11 | 12 | // NOTE: Do not place your application dependencies here; they belong 13 | // in the individual module build.gradle files 14 | } 15 | } 16 | 17 | allprojects { 18 | repositories { 19 | google() 20 | jcenter() 21 | 22 | } 23 | } 24 | 25 | task clean(type: Delete) { 26 | delete rootProject.buildDir 27 | } 28 | -------------------------------------------------------------------------------- /gradle.properties: -------------------------------------------------------------------------------- 1 | # Project-wide Gradle settings. 2 | # IDE (e.g. Android Studio) users: 3 | # Gradle settings configured through the IDE *will override* 4 | # any settings specified in this file. 5 | # For more details on how to configure your build environment visit 6 | # http://www.gradle.org/docs/current/userguide/build_environment.html 7 | # Specifies the JVM arguments used for the daemon process. 8 | # The setting is particularly useful for tweaking memory settings. 9 | org.gradle.jvmargs=-Xmx1536m 10 | # When configured, Gradle will run in incubating parallel mode. 11 | # This option should only be used with decoupled projects. More details, visit 12 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects 13 | # org.gradle.parallel=true 14 | # AndroidX package structure to make it clearer which packages are bundled with the 15 | # Android operating system, and which are packaged with your app's APK 16 | # https://developer.android.com/topic/libraries/support-library/androidx-rn 17 | #android.useAndroidX=true 18 | # Automatically convert third-party libraries to use AndroidX 19 | #android.enableJetifier=true 20 | 21 | -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JianxunMaster/TreeNodeLibrary/e0b38e4e3edc23ce7080a6938fe0f53a9271bae3/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | #Wed Sep 11 23:41:26 CST 2019 2 | distributionBase=GRADLE_USER_HOME 3 | distributionPath=wrapper/dists 4 | zipStoreBase=GRADLE_USER_HOME 5 | zipStorePath=wrapper/dists 6 | distributionUrl=https\://services.gradle.org/distributions/gradle-5.1.1-all.zip 7 | -------------------------------------------------------------------------------- /gradlew: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env sh 2 | 3 | ############################################################################## 4 | ## 5 | ## Gradle start up script for UN*X 6 | ## 7 | ############################################################################## 8 | 9 | # Attempt to set APP_HOME 10 | # Resolve links: $0 may be a link 11 | PRG="$0" 12 | # Need this for relative symlinks. 13 | while [ -h "$PRG" ] ; do 14 | ls=`ls -ld "$PRG"` 15 | link=`expr "$ls" : '.*-> \(.*\)$'` 16 | if expr "$link" : '/.*' > /dev/null; then 17 | PRG="$link" 18 | else 19 | PRG=`dirname "$PRG"`"/$link" 20 | fi 21 | done 22 | SAVED="`pwd`" 23 | cd "`dirname \"$PRG\"`/" >/dev/null 24 | APP_HOME="`pwd -P`" 25 | cd "$SAVED" >/dev/null 26 | 27 | APP_NAME="Gradle" 28 | APP_BASE_NAME=`basename "$0"` 29 | 30 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 31 | DEFAULT_JVM_OPTS="" 32 | 33 | # Use the maximum available, or set MAX_FD != -1 to use that value. 34 | MAX_FD="maximum" 35 | 36 | warn () { 37 | echo "$*" 38 | } 39 | 40 | die () { 41 | echo 42 | echo "$*" 43 | echo 44 | exit 1 45 | } 46 | 47 | # OS specific support (must be 'true' or 'false'). 48 | cygwin=false 49 | msys=false 50 | darwin=false 51 | nonstop=false 52 | case "`uname`" in 53 | CYGWIN* ) 54 | cygwin=true 55 | ;; 56 | Darwin* ) 57 | darwin=true 58 | ;; 59 | MINGW* ) 60 | msys=true 61 | ;; 62 | NONSTOP* ) 63 | nonstop=true 64 | ;; 65 | esac 66 | 67 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar 68 | 69 | # Determine the Java command to use to start the JVM. 70 | if [ -n "$JAVA_HOME" ] ; then 71 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 72 | # IBM's JDK on AIX uses strange locations for the executables 73 | JAVACMD="$JAVA_HOME/jre/sh/java" 74 | else 75 | JAVACMD="$JAVA_HOME/bin/java" 76 | fi 77 | if [ ! -x "$JAVACMD" ] ; then 78 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME 79 | 80 | Please set the JAVA_HOME variable in your environment to match the 81 | location of your Java installation." 82 | fi 83 | else 84 | JAVACMD="java" 85 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 86 | 87 | Please set the JAVA_HOME variable in your environment to match the 88 | location of your Java installation." 89 | fi 90 | 91 | # Increase the maximum file descriptors if we can. 92 | if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then 93 | MAX_FD_LIMIT=`ulimit -H -n` 94 | if [ $? -eq 0 ] ; then 95 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then 96 | MAX_FD="$MAX_FD_LIMIT" 97 | fi 98 | ulimit -n $MAX_FD 99 | if [ $? -ne 0 ] ; then 100 | warn "Could not set maximum file descriptor limit: $MAX_FD" 101 | fi 102 | else 103 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" 104 | fi 105 | fi 106 | 107 | # For Darwin, add options to specify how the application appears in the dock 108 | if $darwin; then 109 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" 110 | fi 111 | 112 | # For Cygwin, switch paths to Windows format before running java 113 | if $cygwin ; then 114 | APP_HOME=`cygpath --path --mixed "$APP_HOME"` 115 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` 116 | JAVACMD=`cygpath --unix "$JAVACMD"` 117 | 118 | # We build the pattern for arguments to be converted via cygpath 119 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` 120 | SEP="" 121 | for dir in $ROOTDIRSRAW ; do 122 | ROOTDIRS="$ROOTDIRS$SEP$dir" 123 | SEP="|" 124 | done 125 | OURCYGPATTERN="(^($ROOTDIRS))" 126 | # Add a user-defined pattern to the cygpath arguments 127 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then 128 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" 129 | fi 130 | # Now convert the arguments - kludge to limit ourselves to /bin/sh 131 | i=0 132 | for arg in "$@" ; do 133 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` 134 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option 135 | 136 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition 137 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` 138 | else 139 | eval `echo args$i`="\"$arg\"" 140 | fi 141 | i=$((i+1)) 142 | done 143 | case $i in 144 | (0) set -- ;; 145 | (1) set -- "$args0" ;; 146 | (2) set -- "$args0" "$args1" ;; 147 | (3) set -- "$args0" "$args1" "$args2" ;; 148 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;; 149 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; 150 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; 151 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; 152 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; 153 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; 154 | esac 155 | fi 156 | 157 | # Escape application args 158 | save () { 159 | for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done 160 | echo " " 161 | } 162 | APP_ARGS=$(save "$@") 163 | 164 | # Collect all arguments for the java command, following the shell quoting and substitution rules 165 | eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS" 166 | 167 | # by default we should be in the correct project dir, but when run from Finder on Mac, the cwd is wrong 168 | if [ "$(uname)" = "Darwin" ] && [ "$HOME" = "$PWD" ]; then 169 | cd "$(dirname "$0")" 170 | fi 171 | 172 | exec "$JAVACMD" "$@" 173 | -------------------------------------------------------------------------------- /gradlew.bat: -------------------------------------------------------------------------------- 1 | @if "%DEBUG%" == "" @echo off 2 | @rem ########################################################################## 3 | @rem 4 | @rem Gradle startup script for Windows 5 | @rem 6 | @rem ########################################################################## 7 | 8 | @rem Set local scope for the variables with windows NT shell 9 | if "%OS%"=="Windows_NT" setlocal 10 | 11 | set DIRNAME=%~dp0 12 | if "%DIRNAME%" == "" set DIRNAME=. 13 | set APP_BASE_NAME=%~n0 14 | set APP_HOME=%DIRNAME% 15 | 16 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 17 | set DEFAULT_JVM_OPTS= 18 | 19 | @rem Find java.exe 20 | if defined JAVA_HOME goto findJavaFromJavaHome 21 | 22 | set JAVA_EXE=java.exe 23 | %JAVA_EXE% -version >NUL 2>&1 24 | if "%ERRORLEVEL%" == "0" goto init 25 | 26 | echo. 27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 28 | echo. 29 | echo Please set the JAVA_HOME variable in your environment to match the 30 | echo location of your Java installation. 31 | 32 | goto fail 33 | 34 | :findJavaFromJavaHome 35 | set JAVA_HOME=%JAVA_HOME:"=% 36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe 37 | 38 | if exist "%JAVA_EXE%" goto init 39 | 40 | echo. 41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 42 | echo. 43 | echo Please set the JAVA_HOME variable in your environment to match the 44 | echo location of your Java installation. 45 | 46 | goto fail 47 | 48 | :init 49 | @rem Get command-line arguments, handling Windows variants 50 | 51 | if not "%OS%" == "Windows_NT" goto win9xME_args 52 | 53 | :win9xME_args 54 | @rem Slurp the command line arguments. 55 | set CMD_LINE_ARGS= 56 | set _SKIP=2 57 | 58 | :win9xME_args_slurp 59 | if "x%~1" == "x" goto execute 60 | 61 | set CMD_LINE_ARGS=%* 62 | 63 | :execute 64 | @rem Setup the command line 65 | 66 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 67 | 68 | @rem Execute Gradle 69 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% 70 | 71 | :end 72 | @rem End local scope for the variables with windows NT shell 73 | if "%ERRORLEVEL%"=="0" goto mainEnd 74 | 75 | :fail 76 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 77 | rem the _cmd.exe /c_ return code! 78 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 79 | exit /b 1 80 | 81 | :mainEnd 82 | if "%OS%"=="Windows_NT" endlocal 83 | 84 | :omega 85 | -------------------------------------------------------------------------------- /settings.gradle: -------------------------------------------------------------------------------- 1 | include ':app', ':treenodelibrary' 2 | -------------------------------------------------------------------------------- /treenodelibrary/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /treenodelibrary/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.library' 2 | 3 | android { 4 | compileSdkVersion 28 5 | buildToolsVersion '28.0.3' 6 | 7 | 8 | defaultConfig { 9 | minSdkVersion 15 10 | targetSdkVersion 28 11 | versionCode 1 12 | versionName "1.0" 13 | 14 | testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" 15 | 16 | } 17 | 18 | buildTypes { 19 | release { 20 | minifyEnabled false 21 | proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' 22 | } 23 | } 24 | 25 | } 26 | 27 | dependencies { 28 | implementation fileTree(dir: 'libs', include: ['*.jar']) 29 | 30 | implementation 'com.android.support:appcompat-v7:28.0.0' 31 | testImplementation 'junit:junit:4.12' 32 | implementation 'com.android.support:recyclerview-v7:28.0.0' 33 | } 34 | -------------------------------------------------------------------------------- /treenodelibrary/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # You can control the set of applied configuration files using the 3 | # proguardFiles setting in build.gradle. 4 | # 5 | # For more details, see 6 | # http://developer.android.com/guide/developing/tools/proguard.html 7 | 8 | # If your project uses WebView with JS, uncomment the following 9 | # and specify the fully qualified class name to the JavaScript interface 10 | # class: 11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 12 | # public *; 13 | #} 14 | 15 | # Uncomment this to preserve the line number information for 16 | # debugging stack traces. 17 | #-keepattributes SourceFile,LineNumberTable 18 | 19 | # If you keep the line number information, uncomment this to 20 | # hide the original source file name. 21 | #-renamesourcefileattribute SourceFile 22 | -------------------------------------------------------------------------------- /treenodelibrary/src/androidTest/java/com/library/treenode/ExampleInstrumentedTest.java: -------------------------------------------------------------------------------- 1 | package com.library.treenode; 2 | 3 | import android.content.Context; 4 | 5 | import androidx.test.InstrumentationRegistry; 6 | import androidx.test.runner.AndroidJUnit4; 7 | 8 | import org.junit.Test; 9 | import org.junit.runner.RunWith; 10 | 11 | import static org.junit.Assert.*; 12 | 13 | /** 14 | * Instrumented test, which will execute on an Android device. 15 | * 16 | * @see Testing documentation 17 | */ 18 | @RunWith(AndroidJUnit4.class) 19 | public class ExampleInstrumentedTest { 20 | @Test 21 | public void useAppContext() { 22 | // Context of the app under test. 23 | Context appContext = InstrumentationRegistry.getTargetContext(); 24 | 25 | assertEquals("com.library.treenode.test", appContext.getPackageName()); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /treenodelibrary/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 3 | -------------------------------------------------------------------------------- /treenodelibrary/src/main/java/treenode/SimpleTreeNodeAdapter.java: -------------------------------------------------------------------------------- 1 | package treenode; 2 | 3 | import android.content.Context; 4 | import android.support.annotation.NonNull; 5 | 6 | 7 | import java.util.List; 8 | 9 | public abstract class SimpleTreeNodeAdapter extends TreeNodeAdapter { 10 | /** 11 | * 构造器 12 | * 13 | * @param context 上下文 14 | * @param rootList 树节点根列表(所有数据) 15 | */ 16 | public SimpleTreeNodeAdapter(Context context, int layoutId, @NonNull List> rootList) { 17 | super(context, rootList); 18 | final int layoutResId = layoutId; 19 | addItemViewDelegate(new TreeNodeDelegate() { 20 | @Override 21 | public boolean isItemType(TreeNode treeNode) { 22 | return true; 23 | } 24 | 25 | @Override 26 | public int getLayoutId() { 27 | return layoutResId; 28 | } 29 | 30 | @Override 31 | public void convert(ViewHolder holder, TreeNode treeNode) { 32 | SimpleTreeNodeAdapter.this.convert(adapter, holder, treeNode); 33 | } 34 | }); 35 | } 36 | 37 | public abstract void convert(TreeNodeAdapter adapter, ViewHolder holder, TreeNode treeNode); 38 | } 39 | -------------------------------------------------------------------------------- /treenodelibrary/src/main/java/treenode/TreeNode.java: -------------------------------------------------------------------------------- 1 | package treenode; 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | 6 | /** 7 | * 树节点 8 | */ 9 | public class TreeNode { 10 | private T value;// 数据 11 | private TreeNode parent;// 父类 12 | private List> children;// 子集 13 | private boolean expand;// 是否展开 14 | private boolean select;// 是否选中 15 | 16 | public TreeNode() { 17 | } 18 | 19 | public TreeNode(T value) { 20 | this.value = value; 21 | } 22 | 23 | public T getValue() { 24 | return value; 25 | } 26 | 27 | public void setValue(T value) { 28 | this.value = value; 29 | } 30 | 31 | public TreeNode getParent() { 32 | return parent; 33 | } 34 | 35 | public void setParent(TreeNode parent) { 36 | this.parent = parent; 37 | } 38 | 39 | public List> getChildren() { 40 | return children; 41 | } 42 | 43 | public void setChildren(List> children) { 44 | this.children = children; 45 | if (children != null) { 46 | for (TreeNode child : children) { 47 | if (child != null) { 48 | child.setParent(this); 49 | } 50 | } 51 | } 52 | } 53 | 54 | public void addChild(TreeNode child) { 55 | if (child == null) return; 56 | if (children == null) children = new ArrayList<>(); 57 | children.add(child); 58 | child.setParent(this); 59 | } 60 | 61 | public int getLevel() { 62 | return parent == null ? 0 : parent.getLevel() + 1; 63 | } 64 | 65 | public boolean isExpand() { 66 | return expand; 67 | } 68 | 69 | public void setExpand(boolean expand) { 70 | this.expand = expand; 71 | } 72 | 73 | public boolean isSelect() { 74 | return select; 75 | } 76 | 77 | public void setSelect(boolean select) { 78 | this.select = select; 79 | } 80 | 81 | public boolean isRoot() { 82 | return parent == null; 83 | } 84 | 85 | public boolean isLeaf() { 86 | return children == null || children.size() == 0; 87 | } 88 | } 89 | -------------------------------------------------------------------------------- /treenodelibrary/src/main/java/treenode/TreeNodeAdapter.java: -------------------------------------------------------------------------------- 1 | package treenode; 2 | 3 | import android.content.Context; 4 | import android.support.annotation.NonNull; 5 | import android.support.v7.widget.RecyclerView; 6 | import android.view.LayoutInflater; 7 | import android.view.View; 8 | import android.view.ViewGroup; 9 | 10 | 11 | import java.util.ArrayList; 12 | import java.util.List; 13 | 14 | /** 15 | * 树节点多级列表适配器 16 | * 17 | * @param 泛型,树节点数据的泛型 18 | */ 19 | public class TreeNodeAdapter extends RecyclerView.Adapter { 20 | protected Context context;// 上下文 21 | private LayoutInflater layoutInflater;// 视图渲染器 22 | private List> treeNodeDelegateList = new ArrayList<>();// 所有布局类型 23 | private List> showTreeNodeList = new ArrayList<>();// 当前显示的树节点 24 | private List> rootTreeNodeList;// 所有树节点 25 | private int maxSelectCount;// 多选个数 -->> 0:任意 非0:限制数量 26 | private List> selectTreeNodeList = new ArrayList<>();// 选中 27 | 28 | /** 29 | * 构造器 30 | * 31 | * @param context 上下文 32 | * @param rootList 树节点根列表(所有数据) 33 | */ 34 | public TreeNodeAdapter(Context context, @NonNull List> rootList) { 35 | this.context = context; 36 | this.layoutInflater = LayoutInflater.from(context); 37 | this.rootTreeNodeList = rootList; 38 | init(); 39 | } 40 | 41 | @NonNull 42 | @Override 43 | public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { 44 | TreeNodeDelegate holder = treeNodeDelegateList.get(viewType); 45 | View itemView = layoutInflater.inflate(holder.getLayoutId(), parent, false); 46 | return new ViewHolder(itemView); 47 | } 48 | 49 | @Override 50 | public void onBindViewHolder(@NonNull ViewHolder holder, int position) { 51 | int viewType = getItemViewType(position); 52 | TreeNodeDelegate delegate = treeNodeDelegateList.get(viewType); 53 | delegate.convert(holder, showTreeNodeList.get(position)); 54 | } 55 | 56 | @Override 57 | public int getItemCount() { 58 | return showTreeNodeList.size(); 59 | } 60 | 61 | @Override 62 | public int getItemViewType(int position) { 63 | TreeNode treeNode = showTreeNodeList.get(position); 64 | for (int i = 0; i < treeNodeDelegateList.size(); i++) { 65 | TreeNodeDelegate type = treeNodeDelegateList.get(i); 66 | if (type.isItemType(treeNode)) { 67 | return i; 68 | } 69 | } 70 | throw new IllegalArgumentException("No TreeNodeDelegate added that matches position=" + position + " in data source"); 71 | } 72 | 73 | /** 74 | * 添加视图样式 75 | * 76 | * @param delegate 样式 77 | */ 78 | public void addItemViewDelegate(TreeNodeDelegate delegate) { 79 | delegate.adapter = this; 80 | treeNodeDelegateList.add(delegate); 81 | } 82 | 83 | /** 84 | * 移除视图样式 85 | * 86 | * @param delegate 样式 87 | */ 88 | public void removeItemViewDelegate(TreeNodeDelegate delegate) { 89 | treeNodeDelegateList.remove(delegate); 90 | } 91 | 92 | /** 93 | * 刷新 94 | */ 95 | public void refreshTreeNode() { 96 | init(); 97 | notifyDataSetChanged(); 98 | } 99 | 100 | /** 101 | * 初始化展开的数据 102 | */ 103 | private void init() { 104 | showTreeNodeList.clear(); 105 | for (TreeNode tTreeNode : rootTreeNodeList) { 106 | showTreeNodeList.add(tTreeNode); 107 | if (tTreeNode.isExpand()) { 108 | showTreeNodeList.addAll(TreeNodeHelper.getExpendedChildren(tTreeNode)); 109 | } 110 | } 111 | } 112 | 113 | /** 114 | * 展开树节点 115 | * 116 | * @param treeNode 树节点 117 | */ 118 | public void expandTreeNode(TreeNode treeNode) { 119 | if (treeNode == null) return; 120 | treeNode.setExpand(!treeNode.isExpand()); 121 | int index = showTreeNodeList.indexOf(treeNode); 122 | List> children = TreeNodeHelper.getExpendedChildren(treeNode); 123 | if (index < 0 || index > showTreeNodeList.size() - 1 || children == null || children.size() == 0) 124 | return; 125 | showTreeNodeList.addAll(index + 1, children); 126 | notifyItemRangeInserted(index + 1, children.size()); 127 | notifyTreeNode(treeNode); 128 | } 129 | 130 | /** 131 | * 收缩树节点 132 | * 133 | * @param treeNode 树节点 134 | */ 135 | public void collapseTreeNode(TreeNode treeNode) { 136 | if (treeNode == null) return; 137 | treeNode.setExpand(!treeNode.isExpand()); 138 | int index = showTreeNodeList.indexOf(treeNode); 139 | List> children = TreeNodeHelper.getExpendedChildren(treeNode); 140 | if (index < 0 || index > showTreeNodeList.size() - 1 || children == null || children.size() == 0) 141 | return; 142 | showTreeNodeList.removeAll(children); 143 | notifyItemRangeRemoved(index + 1, children.size()); 144 | notifyTreeNode(treeNode); 145 | } 146 | 147 | /** 148 | * 伸缩书记诶单 149 | * 150 | * @param treeNode 树节点 151 | */ 152 | public void expandOrCollapseTreeNode(TreeNode treeNode) { 153 | if (treeNode.isExpand()) { 154 | collapseTreeNode(treeNode); 155 | } else { 156 | expandTreeNode(treeNode); 157 | } 158 | } 159 | 160 | /** 161 | * 刷新树节点 162 | * 163 | * @param treeNode 树节点 164 | */ 165 | public void notifyTreeNode(TreeNode treeNode) { 166 | int index = showTreeNodeList.indexOf(treeNode); 167 | if (index < 0 || index > showTreeNodeList.size() - 1) return; 168 | notifyItemChanged(showTreeNodeList.indexOf(treeNode)); 169 | } 170 | 171 | /** 172 | * 展开所有层级的树节点 173 | */ 174 | public void expandAllTreeNode() { 175 | TreeNodeHelper.expandAll(rootTreeNodeList); 176 | refreshTreeNode(); 177 | } 178 | 179 | /** 180 | * 收缩所有层级的树节点 181 | */ 182 | public void collapseAllTreeNode() { 183 | TreeNodeHelper.collapseAll(rootTreeNodeList); 184 | refreshTreeNode(); 185 | } 186 | 187 | /** 188 | * 展开层级内的树节点 189 | * 190 | * @param level 层级 191 | */ 192 | public void expandLevelTreeNode(int level) { 193 | TreeNodeHelper.expandLevel(rootTreeNodeList, level); 194 | refreshTreeNode(); 195 | } 196 | 197 | /** 198 | * 设置可选树节点最大数量 199 | * 200 | * @param maxSelectCount 数量 201 | */ 202 | public void setMaxSelectCount(int maxSelectCount) { 203 | this.maxSelectCount = maxSelectCount; 204 | } 205 | 206 | /** 207 | * 获取选中树节点集合 208 | * 209 | * @return 选中树节点集合 210 | */ 211 | public List> getSelectTreeNodeList() { 212 | return selectTreeNodeList; 213 | } 214 | 215 | /** 216 | * 改变选中状态,并刷新 217 | * 当限制最多选中数量,超出的部分会把先加入的树节点移除 218 | * 219 | * @param treeNode 树节点 220 | * @param select 选中状态 221 | */ 222 | public void selectTreeNode(TreeNode treeNode, boolean select) { 223 | treeNode.setSelect(select); 224 | if (select) { 225 | if (!selectTreeNodeList.contains(treeNode)) {// 添加并刷新 226 | selectTreeNodeList.add(treeNode); 227 | notifyTreeNode(treeNode); 228 | } 229 | if (maxSelectCount != 0) {// 多选且不限制数量 230 | if (selectTreeNodeList.size() > maxSelectCount) {// 删除首个已选并刷新 231 | TreeNode first = selectTreeNodeList.get(0); 232 | first.setSelect(false); 233 | selectTreeNodeList.remove(first); 234 | notifyTreeNode(first); 235 | } 236 | } 237 | } else { 238 | if (selectTreeNodeList.contains(treeNode)) {// 移除并刷新 239 | selectTreeNodeList.remove(treeNode); 240 | notifyTreeNode(treeNode); 241 | } 242 | } 243 | } 244 | 245 | /** 246 | * 改变树节点选中状态 247 | * 248 | * @param treeNode 树节点 249 | */ 250 | public void changeSelectTreeNode(TreeNode treeNode) { 251 | selectTreeNode(treeNode, !treeNode.isSelect()); 252 | } 253 | } 254 | -------------------------------------------------------------------------------- /treenodelibrary/src/main/java/treenode/TreeNodeDelegate.java: -------------------------------------------------------------------------------- 1 | package treenode; 2 | 3 | public abstract class TreeNodeDelegate { 4 | public TreeNodeAdapter adapter; 5 | /** 6 | * 是否是当前类型 7 | * 8 | * @param treeNode 树节点 9 | * @return 当前类型 10 | */ 11 | public abstract boolean isItemType(TreeNode treeNode); 12 | 13 | /** 14 | * 当前根视图布局id 15 | * 16 | * @return 布局id 17 | */ 18 | public abstract int getLayoutId(); 19 | 20 | /** 21 | * 视图适配 22 | * 23 | * @param holder 视图持有者 24 | * @param treeNode 树节点 25 | */ 26 | public abstract void convert(ViewHolder holder, TreeNode treeNode); 27 | } 28 | -------------------------------------------------------------------------------- /treenodelibrary/src/main/java/treenode/TreeNodeHelper.java: -------------------------------------------------------------------------------- 1 | package treenode; 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | 6 | public class TreeNodeHelper { 7 | public static List> getExpendedChildren(TreeNode parent) { 8 | List> childrenList = new ArrayList<>(); 9 | List> children = parent.getChildren(); 10 | if (children != null) { 11 | for (TreeNode child : children) { 12 | childrenList.add(child); 13 | if (child.isExpand()) { 14 | childrenList.addAll(getExpendedChildren(child)); 15 | } 16 | } 17 | } 18 | return childrenList; 19 | } 20 | 21 | public static void expandAll(List> parentList) { 22 | if (parentList == null) return; 23 | for (TreeNode parent : parentList) { 24 | parent.setExpand(true); 25 | expandAll(parent.getChildren()); 26 | } 27 | } 28 | 29 | public static void collapseAll(List> parentList) { 30 | if (parentList == null) return; 31 | for (TreeNode parent : parentList) { 32 | parent.setExpand(false); 33 | collapseAll(parent.getChildren()); 34 | } 35 | } 36 | 37 | public static void expandLevel(List> parentList, int level) { 38 | if (parentList == null) return; 39 | for (TreeNode parent : parentList) { 40 | if (parent.getLevel() < level) { 41 | parent.setExpand(true); 42 | expandLevel(parent.getChildren(), level); 43 | } 44 | } 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /treenodelibrary/src/main/java/treenode/ViewHolder.java: -------------------------------------------------------------------------------- 1 | package treenode; 2 | 3 | import android.support.annotation.NonNull; 4 | import android.support.v7.widget.RecyclerView; 5 | import android.util.SparseArray; 6 | import android.view.View; 7 | 8 | 9 | public class ViewHolder extends RecyclerView.ViewHolder { 10 | private SparseArray mViews; 11 | private View mConvertView; 12 | 13 | public ViewHolder(@NonNull View itemView) { 14 | super(itemView); 15 | mConvertView = itemView; 16 | mViews = new SparseArray<>(); 17 | } 18 | 19 | /** 20 | * 通过viewId获取控件 21 | * 22 | * @param viewId 控件id 23 | * @return 控件 24 | */ 25 | public T getView(int viewId) { 26 | View view = mViews.get(viewId); 27 | if (view == null) { 28 | view = mConvertView.findViewById(viewId); 29 | mViews.put(viewId, view); 30 | } 31 | return (T) view; 32 | } 33 | 34 | public View getConvertView() { 35 | return mConvertView; 36 | } 37 | 38 | } 39 | -------------------------------------------------------------------------------- /treenodelibrary/src/main/res/layout/layout_tree_node.xml: -------------------------------------------------------------------------------- 1 | 2 | 6 | 7 | 11 | 12 | -------------------------------------------------------------------------------- /treenodelibrary/src/main/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | TreeNodeLibrary 3 | 4 | -------------------------------------------------------------------------------- /treenodelibrary/src/test/java/com/library/treenode/ExampleUnitTest.java: -------------------------------------------------------------------------------- 1 | package com.library.treenode; 2 | 3 | import org.junit.Test; 4 | 5 | import static org.junit.Assert.*; 6 | 7 | /** 8 | * Example local unit test, which will execute on the development machine (host). 9 | * 10 | * @see Testing documentation 11 | */ 12 | public class ExampleUnitTest { 13 | @Test 14 | public void addition_isCorrect() { 15 | assertEquals(4, 2 + 2); 16 | } 17 | } --------------------------------------------------------------------------------