├── .idea ├── .name ├── copyright │ └── profiles_settings.xml ├── encodings.xml ├── vcs.xml ├── modules.xml ├── runConfigurations.xml ├── compiler.xml ├── gradle.xml └── misc.xml ├── app ├── .gitignore ├── src │ ├── main │ │ ├── res │ │ │ ├── values │ │ │ │ ├── strings.xml │ │ │ │ ├── colors.xml │ │ │ │ └── styles.xml │ │ │ ├── mipmap-mdpi │ │ │ │ ├── expand.png │ │ │ │ ├── collapse.png │ │ │ │ └── ic_launcher.png │ │ │ ├── mipmap-hdpi │ │ │ │ ├── ic_launcher.png │ │ │ │ ├── login_rempass.png │ │ │ │ └── login_rempass_press.png │ │ │ ├── mipmap-xhdpi │ │ │ │ └── ic_launcher.png │ │ │ ├── mipmap-xxhdpi │ │ │ │ └── ic_launcher.png │ │ │ ├── mipmap-xxxhdpi │ │ │ │ └── ic_launcher.png │ │ │ ├── drawable │ │ │ │ ├── text_selector.xml │ │ │ │ ├── login_checkbox_selector.xml │ │ │ │ ├── common_btn_bg_selector.xml │ │ │ │ ├── login_submit_btn.xml │ │ │ │ └── login_submit_btn1.xml │ │ │ └── layout │ │ │ │ ├── select_dept_layout.xml │ │ │ │ └── tree_listview_item.xml │ │ ├── AndroidManifest.xml │ │ └── java │ │ │ └── com │ │ │ └── heqing │ │ │ └── treeviewtest │ │ │ └── bean │ │ │ └── my_tree_view │ │ │ ├── Dept.java │ │ │ ├── NodeHelper.java │ │ │ ├── Node.java │ │ │ ├── TreeTestActivity.java │ │ │ └── NodeTreeAdapter.java │ ├── test │ │ └── java │ │ │ └── com │ │ │ └── heqing │ │ │ └── treeviewtest │ │ │ └── ExampleUnitTest.java │ └── androidTest │ │ └── java │ │ └── com │ │ └── heqing │ │ └── treeviewtest │ │ └── ApplicationTest.java ├── build.gradle └── proguard-rules.pro ├── settings.gradle ├── effect_picture └── effect.jpg ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── .gitignore ├── README.md ├── gradle.properties ├── gradlew.bat └── gradlew /.idea/.name: -------------------------------------------------------------------------------- 1 | TreeViewTest -------------------------------------------------------------------------------- /app/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /settings.gradle: -------------------------------------------------------------------------------- 1 | include ':app' 2 | -------------------------------------------------------------------------------- /effect_picture/effect.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/heqinghqocsh/TreeView/HEAD/effect_picture/effect.jpg -------------------------------------------------------------------------------- /.idea/copyright/profiles_settings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | -------------------------------------------------------------------------------- /app/src/main/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | TreeViewTest 3 | 4 | -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/heqinghqocsh/TreeView/HEAD/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /app/src/main/res/mipmap-mdpi/expand.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/heqinghqocsh/TreeView/HEAD/app/src/main/res/mipmap-mdpi/expand.png -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *.iml 2 | .gradle 3 | /local.properties 4 | /.idea/workspace.xml 5 | /.idea/libraries 6 | .DS_Store 7 | /build 8 | /captures 9 | -------------------------------------------------------------------------------- /app/src/main/res/mipmap-mdpi/collapse.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/heqinghqocsh/TreeView/HEAD/app/src/main/res/mipmap-mdpi/collapse.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-hdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/heqinghqocsh/TreeView/HEAD/app/src/main/res/mipmap-hdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-mdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/heqinghqocsh/TreeView/HEAD/app/src/main/res/mipmap-mdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/heqinghqocsh/TreeView/HEAD/app/src/main/res/mipmap-xhdpi/ic_launcher.png -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # TreeView 2 | Android打造一个高性能无限层级树形控件 3 | #效果图 4 | ![image](https://github.com/heqinghqocsh/TreeView/blob/master/effect_picture/effect.jpg) 5 | -------------------------------------------------------------------------------- /app/src/main/res/mipmap-hdpi/login_rempass.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/heqinghqocsh/TreeView/HEAD/app/src/main/res/mipmap-hdpi/login_rempass.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/heqinghqocsh/TreeView/HEAD/app/src/main/res/mipmap-xxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/heqinghqocsh/TreeView/HEAD/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-hdpi/login_rempass_press.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/heqinghqocsh/TreeView/HEAD/app/src/main/res/mipmap-hdpi/login_rempass_press.png -------------------------------------------------------------------------------- /.idea/encodings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /.idea/vcs.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | #Sun Aug 07 13:41:00 CST 2016 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-2.10-all.zip 7 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/text_selector.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/login_checkbox_selector.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/common_btn_bg_selector.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/login_submit_btn.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 8 | -------------------------------------------------------------------------------- /.idea/modules.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /app/src/main/res/values/colors.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | #3F51B5 4 | #303F9F 5 | #FF4081 6 | 7 | #00bac5 8 | #5ecdd4 9 | 10 | #00bac5 11 | 12 | -------------------------------------------------------------------------------- /app/src/test/java/com/heqing/treeviewtest/ExampleUnitTest.java: -------------------------------------------------------------------------------- 1 | package com.heqing.treeviewtest; 2 | 3 | import org.junit.Test; 4 | 5 | import static org.junit.Assert.*; 6 | 7 | /** 8 | * To work on unit tests, switch the Test Artifact in the Build Variants view. 9 | */ 10 | public class ExampleUnitTest { 11 | @Test 12 | public void addition_isCorrect() throws Exception { 13 | assertEquals(4, 2 + 2); 14 | } 15 | } -------------------------------------------------------------------------------- /app/src/main/res/values/styles.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/login_submit_btn1.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /app/src/main/res/layout/select_dept_layout.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 12 | 13 | -------------------------------------------------------------------------------- /.idea/runConfigurations.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 11 | 12 | -------------------------------------------------------------------------------- /app/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | 3 | android { 4 | compileSdkVersion 23 5 | buildToolsVersion "23.0.1" 6 | 7 | defaultConfig { 8 | applicationId "com.heqing.treeviewtest" 9 | minSdkVersion 15 10 | targetSdkVersion 23 11 | versionCode 1 12 | versionName "1.0" 13 | } 14 | buildTypes { 15 | release { 16 | minifyEnabled false 17 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 18 | } 19 | } 20 | } 21 | 22 | dependencies { 23 | compile fileTree(dir: 'libs', include: ['*.jar']) 24 | testCompile 'junit:junit:4.12' 25 | compile 'com.android.support:appcompat-v7:23.1.1' 26 | } 27 | -------------------------------------------------------------------------------- /app/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # By default, the flags in this file are appended to flags specified 3 | # in D:\Develop_Software\Android\sdk/tools/proguard/proguard-android.txt 4 | # You can edit the include path and order by changing the proguardFiles 5 | # directive in build.gradle. 6 | # 7 | # For more details, see 8 | # http://developer.android.com/guide/developing/tools/proguard.html 9 | 10 | # Add any project specific keep options here: 11 | 12 | # If your project uses WebView with JS, uncomment the following 13 | # and specify the fully qualified class name to the JavaScript interface 14 | # class: 15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 16 | # public *; 17 | #} 18 | -------------------------------------------------------------------------------- /app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 3 | 4 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | -------------------------------------------------------------------------------- /.idea/compiler.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | -------------------------------------------------------------------------------- /gradle.properties: -------------------------------------------------------------------------------- 1 | # Project-wide Gradle settings. 2 | 3 | # IDE (e.g. Android Studio) users: 4 | # Gradle settings configured through the IDE *will override* 5 | # any settings specified in this file. 6 | 7 | # For more details on how to configure your build environment visit 8 | # http://www.gradle.org/docs/current/userguide/build_environment.html 9 | 10 | # Specifies the JVM arguments used for the daemon process. 11 | # The setting is particularly useful for tweaking memory settings. 12 | # Default value: -Xmx10248m -XX:MaxPermSize=256m 13 | # org.gradle.jvmargs=-Xmx2048m -XX:MaxPermSize=512m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8 14 | 15 | # When configured, Gradle will run in incubating parallel mode. 16 | # This option should only be used with decoupled projects. More details, visit 17 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects 18 | # org.gradle.parallel=true -------------------------------------------------------------------------------- /.idea/gradle.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 23 | 24 | -------------------------------------------------------------------------------- /app/src/main/res/layout/tree_listview_item.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 14 | 15 | 16 | 27 | 28 | 33 | 34 | 35 | 36 | 45 | 46 | -------------------------------------------------------------------------------- /app/src/main/java/com/heqing/treeviewtest/bean/my_tree_view/Dept.java: -------------------------------------------------------------------------------- 1 | package com.heqing.treeviewtest.bean.my_tree_view; 2 | 3 | /** 4 | * Created by HQOCSHheqing on 2016/8/2. 5 | * 6 | * @description 部门类(继承Node),此处的泛型Integer是因为ID和parentID都为int 7 | * ,如果为String传入泛型String即可 8 | */ 9 | public class Dept extends Node{ 10 | 11 | private int id;//部门ID 12 | private int parentId;//父亲节点ID 13 | private String name;//部门名称 14 | 15 | public Dept() { 16 | } 17 | 18 | public Dept(int id, int parentId, String name) { 19 | this.id = id; 20 | this.parentId = parentId; 21 | this.name = name; 22 | } 23 | 24 | /** 25 | * 此处返回节点ID 26 | * @return 27 | */ 28 | @Override 29 | public Integer get_id() { 30 | return id; 31 | } 32 | 33 | /** 34 | * 此处返回父亲节点ID 35 | * @return 36 | */ 37 | @Override 38 | public Integer get_parentId() { 39 | return parentId; 40 | } 41 | 42 | @Override 43 | public String get_label() { 44 | return name; 45 | } 46 | 47 | @Override 48 | public boolean parent(Node dest) { 49 | if (id == ((Integer)dest.get_parentId()).intValue()){ 50 | return true; 51 | } 52 | return false; 53 | } 54 | 55 | @Override 56 | public boolean child(Node dest) { 57 | if (parentId == ((Integer)dest.get_id()).intValue()){ 58 | return true; 59 | } 60 | return false; 61 | } 62 | 63 | public int getId() { 64 | return id; 65 | } 66 | 67 | public void setId(int id) { 68 | this.id = id; 69 | } 70 | 71 | public int getParentId() { 72 | return parentId; 73 | } 74 | 75 | public void setParentId(int parentId) { 76 | this.parentId = parentId; 77 | } 78 | 79 | public String getName() { 80 | return name; 81 | } 82 | 83 | public void setName(String name) { 84 | this.name = name; 85 | } 86 | } 87 | -------------------------------------------------------------------------------- /app/src/main/java/com/heqing/treeviewtest/bean/my_tree_view/NodeHelper.java: -------------------------------------------------------------------------------- 1 | package com.heqing.treeviewtest.bean.my_tree_view; 2 | 3 | import com.heqing.treeviewtest.R; 4 | 5 | import java.util.ArrayList; 6 | import java.util.List; 7 | 8 | /** 9 | * Created by HQOCSHheqing on 2016/8/2. 10 | * 11 | * @description 节点帮助类 12 | */ 13 | public class NodeHelper { 14 | 15 | /** 16 | * 传入所有的要展示的节点数据 17 | * @param nodeList 返回值是所有的根节点 18 | * @return 19 | */ 20 | public static List sortNodes(List nodeList){ 21 | 22 | List rootNodes = new ArrayList<>(); 23 | int size = nodeList.size(); 24 | Node m; 25 | Node n; 26 | //两个for循环整理出所有数据之间的斧子关系,最后会构造出一个森林(就是可能有多棵树) 27 | for (int i = 0;i < size;i++){ 28 | m = nodeList.get(i); 29 | for (int j = i+1;j < size;j++){ 30 | n = nodeList.get(j); 31 | if (m.parent(n)){ 32 | m.get_childrenList().add(n); 33 | n.set_parent(m); 34 | }else if (m.child(n)){ 35 | n.get_childrenList().add(m); 36 | m.set_parent(n); 37 | } 38 | } 39 | } 40 | //找出所有的树根,同事设置相应的图标(后面你会发现其实这里的主要 41 | // 作用是设置叶节点和非叶节点的图标) 42 | for (int i = 0;i < size;i++){ 43 | m = nodeList.get(i); 44 | if (m.isRoot()){ 45 | rootNodes.add(m); 46 | } 47 | setNodeIcon(m); 48 | } 49 | nodeList.clear();//此时所有的关系已经整理完成了 50 | // ,森林构造完成,可以清空之前的数据,释放内存,提高性能 51 | // ,如果之前的数据还有用的话就不清空 52 | nodeList = rootNodes;//返回所有的根节点 53 | rootNodes = null; 54 | return nodeList; 55 | } 56 | 57 | /** 58 | * 设置图标 59 | * @param node 60 | */ 61 | private static void setNodeIcon(Node node){ 62 | if (!node.isLeaf()){ 63 | if (node.isExpand()){ 64 | node.set_icon(R.mipmap.collapse); 65 | }else{ 66 | node.set_icon(R.mipmap.expand); 67 | } 68 | }else{ 69 | node.set_icon(-1); 70 | } 71 | } 72 | } 73 | -------------------------------------------------------------------------------- /.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 19 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 46 | -------------------------------------------------------------------------------- /app/src/main/java/com/heqing/treeviewtest/bean/my_tree_view/Node.java: -------------------------------------------------------------------------------- 1 | package com.heqing.treeviewtest.bean.my_tree_view; 2 | 3 | import com.heqing.treeviewtest.R; 4 | 5 | import java.util.ArrayList; 6 | import java.util.List; 7 | 8 | /** 9 | * Created by HQOCSHheqing on 2016/8/2. 10 | * 11 | * @description 节点抽象类(泛型T主要是考虑到ID和parentID有可能是int型也有可能是String型 12 | * 即这里可以传入Integer或者String,具体什么类型由子类指定 13 | ,因为这两种类型比较是否相等的方式不同:一个是用 “==”,一个是用 equals() 函数) 14 | */ 15 | public abstract class Node { 16 | 17 | private int _level = -1;//当前节点的层级,初始值-1 后面会讲到 18 | private List _childrenList = new ArrayList<>();//所有的孩子节点 19 | private Node _parent;//父亲节点 20 | private int _icon;//图标资源ID 21 | private boolean isExpand = false;//当前状态是否展开 22 | 23 | 24 | public abstract T get_id();//得到当前节点ID 25 | public abstract T get_parentId();//得到当前节点的父ID 26 | public abstract String get_label();//要显示的内容 27 | public abstract boolean parent(Node dest);//判断当前节点是否是dest的父亲节点 28 | public abstract boolean child(Node dest);//判断当前节点是否是dest的孩子节点 29 | 30 | 31 | public int get_level() { 32 | if (_level == -1){//如果是 -1 的话就递归获取 33 | //因为是树形结构,所以此处想要得到当前节点的层级 34 | //,必须递归调用,但是递归效率低下,如果每次都递归获取会严重影响性能,所以我们把第一次 35 | //得到的结果保存起来避免每次递归获取 36 | int level = _parent == null ? 1 : _parent.get_level()+1; 37 | _level = level; 38 | return _level; 39 | } 40 | return _level; 41 | } 42 | 43 | public void set_level(int _level) { 44 | this._level = _level; 45 | } 46 | 47 | public List get_childrenList() { 48 | return _childrenList; 49 | } 50 | 51 | public void set_childrenList(List _childrenList) { 52 | this._childrenList = _childrenList; 53 | } 54 | 55 | public Node get_parent() { 56 | return _parent; 57 | } 58 | 59 | public void set_parent(Node _parent) { 60 | this._parent = _parent; 61 | } 62 | 63 | public int get_icon() { 64 | return _icon; 65 | } 66 | 67 | public void set_icon(int _icon) { 68 | this._icon = _icon; 69 | } 70 | 71 | public boolean isExpand() { 72 | return isExpand; 73 | } 74 | 75 | public void setIsExpand(boolean isExpand) { 76 | this.isExpand = isExpand; 77 | if (isExpand){ 78 | _icon = R.mipmap.collapse; 79 | }else{ 80 | _icon = R.mipmap.expand; 81 | } 82 | } 83 | 84 | public boolean isRoot(){ 85 | return _parent == null; 86 | } 87 | 88 | public boolean isLeaf(){ 89 | return _childrenList.size() <= 0; 90 | } 91 | 92 | } 93 | -------------------------------------------------------------------------------- /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 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 12 | set DEFAULT_JVM_OPTS= 13 | 14 | set DIRNAME=%~dp0 15 | if "%DIRNAME%" == "" set DIRNAME=. 16 | set APP_BASE_NAME=%~n0 17 | set APP_HOME=%DIRNAME% 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 Windowz variants 50 | 51 | if not "%OS%" == "Windows_NT" goto win9xME_args 52 | if "%@eval[2+2]" == "4" goto 4NT_args 53 | 54 | :win9xME_args 55 | @rem Slurp the command line arguments. 56 | set CMD_LINE_ARGS= 57 | set _SKIP=2 58 | 59 | :win9xME_args_slurp 60 | if "x%~1" == "x" goto execute 61 | 62 | set CMD_LINE_ARGS=%* 63 | goto execute 64 | 65 | :4NT_args 66 | @rem Get arguments from the 4NT Shell from JP Software 67 | set CMD_LINE_ARGS=%$ 68 | 69 | :execute 70 | @rem Setup the command line 71 | 72 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 73 | 74 | @rem Execute Gradle 75 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% 76 | 77 | :end 78 | @rem End local scope for the variables with windows NT shell 79 | if "%ERRORLEVEL%"=="0" goto mainEnd 80 | 81 | :fail 82 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 83 | rem the _cmd.exe /c_ return code! 84 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 85 | exit /b 1 86 | 87 | :mainEnd 88 | if "%OS%"=="Windows_NT" endlocal 89 | 90 | :omega 91 | -------------------------------------------------------------------------------- /app/src/main/java/com/heqing/treeviewtest/bean/my_tree_view/TreeTestActivity.java: -------------------------------------------------------------------------------- 1 | package com.heqing.treeviewtest.bean.my_tree_view; 2 | 3 | import android.app.Activity; 4 | import android.os.Bundle; 5 | import android.widget.ListView; 6 | 7 | import com.heqing.treeviewtest.R; 8 | 9 | import java.util.ArrayList; 10 | import java.util.LinkedList; 11 | import java.util.List; 12 | 13 | /** 14 | * Created by HQOCSHheqing on 2016/8/4. 15 | * 16 | * @description 17 | */ 18 | public class TreeTestActivity extends Activity{ 19 | 20 | private ListView mListView; 21 | private NodeTreeAdapter mAdapter; 22 | private LinkedList mLinkedList = new LinkedList<>(); 23 | 24 | @Override 25 | protected void onCreate(Bundle savedInstanceState) { 26 | super.onCreate(savedInstanceState); 27 | setContentView(R.layout.select_dept_layout); 28 | mListView = (ListView)findViewById(R.id.id_tree); 29 | mAdapter = new NodeTreeAdapter(this,mListView,mLinkedList); 30 | mListView.setAdapter(mAdapter); 31 | initData(); 32 | } 33 | 34 | private void initData(){ 35 | List data = new ArrayList<>(); 36 | addOne(data); 37 | mLinkedList.addAll(NodeHelper.sortNodes(data)); 38 | mAdapter.notifyDataSetChanged(); 39 | } 40 | 41 | private void addOne(List data){ 42 | data.add(new Dept(1, 0, "总公司"));//可以直接注释掉此项,即可构造一个森林 43 | data.add(new Dept(2, 1, "一级部一级部门一级部门一级部门门级部门一级部门级部门一级部门一级部门门级部一级")); 44 | data.add(new Dept(3, 1, "一级部门")); 45 | data.add(new Dept(4, 1, "一级部门")); 46 | 47 | data.add(new Dept(222, 5, "二级部门--测试1")); 48 | data.add(new Dept(223, 5, "二级部门--测试2")); 49 | 50 | data.add(new Dept(5, 1, "一级部门")); 51 | 52 | data.add(new Dept(224, 5, "二级部门--测试3")); 53 | data.add(new Dept(225, 5, "二级部门--测试4")); 54 | 55 | data.add(new Dept(6, 1, "一级部门")); 56 | data.add(new Dept(7, 1, "一级部门")); 57 | data.add(new Dept(8, 1, "一级部门")); 58 | data.add(new Dept(9, 1, "一级部门")); 59 | data.add(new Dept(10, 1, "一级部门")); 60 | 61 | for (int i = 2;i <= 10;i++){ 62 | for (int j = 0;j < 10;j++){ 63 | data.add(new Dept(1+(i - 1)*10+j,i, "二级部门"+j)); 64 | } 65 | } 66 | 67 | for (int i = 0;i < 5;i++){ 68 | data.add(new Dept(101+i,11, "三级部门"+i)); 69 | } 70 | 71 | for (int i = 0;i < 5;i++){ 72 | data.add(new Dept(106+i,22, "三级部门"+i)); 73 | } 74 | for (int i = 0;i < 5;i++){ 75 | data.add(new Dept(111+i,33, "三级部门"+i)); 76 | } 77 | for (int i = 0;i < 5;i++){ 78 | data.add(new Dept(115+i,44, "三级部门"+i)); 79 | } 80 | 81 | for (int i = 0;i < 5;i++){ 82 | data.add(new Dept(401+i,101, "四级部门"+i)); 83 | } 84 | } 85 | 86 | 87 | } 88 | -------------------------------------------------------------------------------- /app/src/androidTest/java/com/heqing/treeviewtest/ApplicationTest.java: -------------------------------------------------------------------------------- 1 | package com.heqing.treeviewtest; 2 | 3 | import android.app.Application; 4 | import android.test.ApplicationTestCase; 5 | 6 | import com.heqing.treeviewtest.bean.bean.FileBean; 7 | import com.heqing.treeviewtest.bean.my_tree_view.Dept; 8 | import com.heqing.treeviewtest.bean.my_tree_view.NodeHelper; 9 | import com.heqing.treeviewtest.bean.my_tree_view.Node; 10 | 11 | import java.util.ArrayList; 12 | import java.util.List; 13 | 14 | /** 15 | * Testing Fundamentals 16 | */ 17 | public class ApplicationTest extends ApplicationTestCase { 18 | public ApplicationTest() { 19 | super(Application.class); 20 | } 21 | 22 | public void test(){ 23 | List deptList = new ArrayList<>(); 24 | Dept dept; 25 | for (int i = 0;i<10;i++){ 26 | for (int j = 0;j < 5;j++){ 27 | dept = new Dept(); 28 | dept.setId(i); 29 | dept.setParentId(i); 30 | dept.setName(""); 31 | deptList.add(dept); 32 | } 33 | } 34 | for (int j = 0;j < 10;j++){ 35 | dept = new Dept(); 36 | dept.setId(j); 37 | dept.setParentId(j); 38 | dept.setName(""); 39 | deptList.add(dept); 40 | } 41 | NodeHelper.sortNodes(deptList, 2); 42 | 43 | 44 | } 45 | 46 | public void testSplit(){ 47 | String tmp = "1111111111111111111111"; 48 | tmp.split("A"); 49 | } 50 | 51 | public void testDept(){ 52 | List mDatas2 = new ArrayList<>(); 53 | // mDatas2.add(new Dept(1, 0, "总公司")); 54 | mDatas2.add(new Dept(2, 1, "一级部门")); 55 | mDatas2.add(new Dept(3, 1, "一级部门")); 56 | mDatas2.add(new Dept(4, 1, "一级部门")); 57 | 58 | mDatas2.add(new Dept(222, 5, "二级部门--测试1")); 59 | mDatas2.add(new Dept(223, 5, "二级部门--测试2")); 60 | 61 | mDatas2.add(new Dept(5, 1, "一级部门")); 62 | 63 | mDatas2.add(new Dept(224, 5, "二级部门--测试3")); 64 | mDatas2.add(new Dept(225, 5, "二级部门--测试4")); 65 | 66 | mDatas2.add(new Dept(6, 1, "一级部门")); 67 | mDatas2.add(new Dept(7, 1, "一级部门")); 68 | mDatas2.add(new Dept(8, 1, "一级部门")); 69 | mDatas2.add(new Dept(9, 1, "一级部门")); 70 | mDatas2.add(new Dept(10, 1, "一级部门")); 71 | 72 | for (int i = 2;i <= 10;i++){ 73 | for (int j = 0;j < 10;j++){ 74 | mDatas2.add(new Dept(1+(i - 1)*10+j,i, "二级部门")); 75 | } 76 | } 77 | 78 | for (int i = 0;i < 5;i++){ 79 | mDatas2.add(new Dept(101+i,11, "三级部门")); 80 | } 81 | for (int i = 0;i < 5;i++){ 82 | mDatas2.add(new Dept(106+i,22, "三级部门")); 83 | } 84 | for (int i = 0;i < 5;i++){ 85 | mDatas2.add(new Dept(111+i,33, "三级部门")); 86 | } 87 | for (int i = 0;i < 5;i++){ 88 | mDatas2.add(new Dept(115+i,44, "三级部门")); 89 | } 90 | 91 | List nodeList = NodeHelper.sortNodes(mDatas2, 2); 92 | nodeList.clear(); 93 | 94 | 95 | 96 | 97 | } 98 | 99 | } -------------------------------------------------------------------------------- /gradlew: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | ############################################################################## 4 | ## 5 | ## Gradle start up script for UN*X 6 | ## 7 | ############################################################################## 8 | 9 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 10 | DEFAULT_JVM_OPTS="" 11 | 12 | APP_NAME="Gradle" 13 | APP_BASE_NAME=`basename "$0"` 14 | 15 | # Use the maximum available, or set MAX_FD != -1 to use that value. 16 | MAX_FD="maximum" 17 | 18 | warn ( ) { 19 | echo "$*" 20 | } 21 | 22 | die ( ) { 23 | echo 24 | echo "$*" 25 | echo 26 | exit 1 27 | } 28 | 29 | # OS specific support (must be 'true' or 'false'). 30 | cygwin=false 31 | msys=false 32 | darwin=false 33 | case "`uname`" in 34 | CYGWIN* ) 35 | cygwin=true 36 | ;; 37 | Darwin* ) 38 | darwin=true 39 | ;; 40 | MINGW* ) 41 | msys=true 42 | ;; 43 | esac 44 | 45 | # Attempt to set APP_HOME 46 | # Resolve links: $0 may be a link 47 | PRG="$0" 48 | # Need this for relative symlinks. 49 | while [ -h "$PRG" ] ; do 50 | ls=`ls -ld "$PRG"` 51 | link=`expr "$ls" : '.*-> \(.*\)$'` 52 | if expr "$link" : '/.*' > /dev/null; then 53 | PRG="$link" 54 | else 55 | PRG=`dirname "$PRG"`"/$link" 56 | fi 57 | done 58 | SAVED="`pwd`" 59 | cd "`dirname \"$PRG\"`/" >/dev/null 60 | APP_HOME="`pwd -P`" 61 | cd "$SAVED" >/dev/null 62 | 63 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar 64 | 65 | # Determine the Java command to use to start the JVM. 66 | if [ -n "$JAVA_HOME" ] ; then 67 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 68 | # IBM's JDK on AIX uses strange locations for the executables 69 | JAVACMD="$JAVA_HOME/jre/sh/java" 70 | else 71 | JAVACMD="$JAVA_HOME/bin/java" 72 | fi 73 | if [ ! -x "$JAVACMD" ] ; then 74 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME 75 | 76 | Please set the JAVA_HOME variable in your environment to match the 77 | location of your Java installation." 78 | fi 79 | else 80 | JAVACMD="java" 81 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 82 | 83 | Please set the JAVA_HOME variable in your environment to match the 84 | location of your Java installation." 85 | fi 86 | 87 | # Increase the maximum file descriptors if we can. 88 | if [ "$cygwin" = "false" -a "$darwin" = "false" ] ; then 89 | MAX_FD_LIMIT=`ulimit -H -n` 90 | if [ $? -eq 0 ] ; then 91 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then 92 | MAX_FD="$MAX_FD_LIMIT" 93 | fi 94 | ulimit -n $MAX_FD 95 | if [ $? -ne 0 ] ; then 96 | warn "Could not set maximum file descriptor limit: $MAX_FD" 97 | fi 98 | else 99 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" 100 | fi 101 | fi 102 | 103 | # For Darwin, add options to specify how the application appears in the dock 104 | if $darwin; then 105 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" 106 | fi 107 | 108 | # For Cygwin, switch paths to Windows format before running java 109 | if $cygwin ; then 110 | APP_HOME=`cygpath --path --mixed "$APP_HOME"` 111 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` 112 | JAVACMD=`cygpath --unix "$JAVACMD"` 113 | 114 | # We build the pattern for arguments to be converted via cygpath 115 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` 116 | SEP="" 117 | for dir in $ROOTDIRSRAW ; do 118 | ROOTDIRS="$ROOTDIRS$SEP$dir" 119 | SEP="|" 120 | done 121 | OURCYGPATTERN="(^($ROOTDIRS))" 122 | # Add a user-defined pattern to the cygpath arguments 123 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then 124 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" 125 | fi 126 | # Now convert the arguments - kludge to limit ourselves to /bin/sh 127 | i=0 128 | for arg in "$@" ; do 129 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` 130 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option 131 | 132 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition 133 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` 134 | else 135 | eval `echo args$i`="\"$arg\"" 136 | fi 137 | i=$((i+1)) 138 | done 139 | case $i in 140 | (0) set -- ;; 141 | (1) set -- "$args0" ;; 142 | (2) set -- "$args0" "$args1" ;; 143 | (3) set -- "$args0" "$args1" "$args2" ;; 144 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;; 145 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; 146 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; 147 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; 148 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; 149 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; 150 | esac 151 | fi 152 | 153 | # Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules 154 | function splitJvmOpts() { 155 | JVM_OPTS=("$@") 156 | } 157 | eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS 158 | JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME" 159 | 160 | exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@" 161 | -------------------------------------------------------------------------------- /app/src/main/java/com/heqing/treeviewtest/bean/my_tree_view/NodeTreeAdapter.java: -------------------------------------------------------------------------------- 1 | package com.heqing.treeviewtest.bean.my_tree_view; 2 | 3 | import android.content.Context; 4 | import android.view.LayoutInflater; 5 | import android.view.View; 6 | import android.view.ViewGroup; 7 | import android.widget.AdapterView; 8 | import android.widget.BaseAdapter; 9 | import android.widget.ImageView; 10 | import android.widget.LinearLayout; 11 | import android.widget.ListView; 12 | import android.widget.TextView; 13 | import android.widget.Toast; 14 | 15 | import com.heqing.treeviewtest.R; 16 | 17 | import java.util.LinkedList; 18 | import java.util.List; 19 | 20 | /** 21 | * Created by HQOCSHheqing on 2016/8/3. 22 | * 23 | * @description 适配器类,就是listview最常见的适配器写法 24 | */ 25 | public class NodeTreeAdapter extends BaseAdapter{ 26 | 27 | //大家经常用ArrayList,但是这里为什么要使用LinkedList 28 | // ,后面大家会发现因为这个list会随着用户展开、收缩某一项而频繁的进行增加、删除元素操作, 29 | // 因为ArrayList是数组实现的,频繁的增删性能低下,而LinkedList是链表实现的,对于频繁的增删 30 | //操作性能要比ArrayList好。 31 | private LinkedList nodeLinkedList; 32 | private LayoutInflater inflater; 33 | private int retract;//缩进值 34 | private Context context; 35 | 36 | public NodeTreeAdapter(Context context,ListView listView,LinkedList linkedList){ 37 | inflater = LayoutInflater.from(context); 38 | this.context = context; 39 | nodeLinkedList = linkedList; 40 | listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { 41 | @Override 42 | public void onItemClick(AdapterView parent, View view, int position, long id) { 43 | expandOrCollapse(position); 44 | } 45 | }); 46 | //缩进值,大家可以将它配置在资源文件中,从而实现适配 47 | retract = (int)(context.getResources().getDisplayMetrics().density*10+0.5f); 48 | } 49 | 50 | /** 51 | * 展开或收缩用户点击的条目 52 | * @param position 53 | */ 54 | private void expandOrCollapse(int position){ 55 | Node node = nodeLinkedList.get(position); 56 | if (node != null && !node.isLeaf()){ 57 | boolean old = node.isExpand(); 58 | if (old){ 59 | List nodeList = node.get_childrenList(); 60 | int size = nodeList.size(); 61 | Node tmp = null; 62 | for (int i = 0;i < size;i++){ 63 | tmp = nodeList.get(i); 64 | if (tmp.isExpand()){ 65 | collapse(tmp,position+1); 66 | } 67 | nodeLinkedList.remove(position+1); 68 | } 69 | }else{ 70 | nodeLinkedList.addAll(position + 1, node.get_childrenList()); 71 | } 72 | node.setIsExpand(!old); 73 | notifyDataSetChanged(); 74 | } 75 | } 76 | /** 77 | * 递归收缩用户点击的条目 78 | * 因为此中实现思路是:当用户展开某一条时,就将该条对应的所有子节点加入到nodeLinkedList 79 | * ,同时控制缩进,当用户收缩某一条时,就将该条所对应的子节点全部删除,而当用户跨级缩进时 80 | * ,就需要递归缩进其所有的孩子节点,这样才能保持整个nodeLinkedList的正确性,同时这种实 81 | * 现方式避免了每次对所有数据进行处理然后插入到一个list,最后显示出来,当数据量一大,就会卡顿, 82 | * 所以这种只改变局部数据的方式性能大大提高。 83 | * @param position 84 | */ 85 | private void collapse(Node node,int position){ 86 | node.setIsExpand(false); 87 | List nodes = node.get_childrenList(); 88 | int size = nodes.size(); 89 | Node tmp = null; 90 | for (int i = 0;i < size;i++){ 91 | tmp = nodes.get(i); 92 | if (tmp.isExpand()){ 93 | collapse(tmp,position+1); 94 | } 95 | nodeLinkedList.remove(position+1); 96 | } 97 | } 98 | 99 | @Override 100 | public int getCount() { 101 | return nodeLinkedList.size(); 102 | } 103 | 104 | @Override 105 | public Object getItem(int position) { 106 | return nodeLinkedList.get(position); 107 | } 108 | 109 | @Override 110 | public long getItemId(int position) { 111 | return position; 112 | } 113 | 114 | @Override 115 | public View getView(int position, View convertView, ViewGroup parent) { 116 | final ViewHolder holder; 117 | if (convertView == null){ 118 | convertView = inflater.inflate(R.layout.tree_listview_item,null); 119 | holder = new ViewHolder(); 120 | holder.imageView = (ImageView)convertView.findViewById(R.id.id_treenode_icon); 121 | holder.label = (TextView)convertView.findViewById(R.id.id_treenode_label); 122 | holder.confirm = (LinearLayout)convertView.findViewById(R.id.id_confirm); 123 | convertView.setTag(holder); 124 | }else{ 125 | holder = (ViewHolder)convertView.getTag(); 126 | } 127 | Node node = nodeLinkedList.get(position); 128 | holder.label.setText(node.get_label()); 129 | if(node.get_icon() == -1){ 130 | holder.imageView.setVisibility(View.INVISIBLE); 131 | }else{ 132 | holder.imageView.setVisibility(View.VISIBLE); 133 | holder.imageView.setImageResource(node.get_icon()); 134 | } 135 | holder.confirm.setTag(position); 136 | holder.confirm.setOnClickListener(new View.OnClickListener() { 137 | @Override 138 | public void onClick(View v) { 139 | Toast.makeText(context,"选中:"+v.getTag(),Toast.LENGTH_SHORT).show(); 140 | } 141 | }); 142 | convertView.setPadding(node.get_level()*retract,5,5,5); 143 | return convertView; 144 | } 145 | 146 | static class ViewHolder{ 147 | public ImageView imageView; 148 | public TextView label; 149 | public LinearLayout confirm; 150 | } 151 | 152 | } 153 | --------------------------------------------------------------------------------