├── app
├── .gitignore
├── src
│ ├── main
│ │ ├── res
│ │ │ ├── values
│ │ │ │ ├── strings.xml
│ │ │ │ ├── colors.xml
│ │ │ │ └── styles.xml
│ │ │ ├── drawable
│ │ │ │ ├── down_icon.png
│ │ │ │ ├── right_icon.png
│ │ │ │ └── ic_launcher_background.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
│ │ │ ├── mipmap-anydpi-v26
│ │ │ │ ├── ic_launcher.xml
│ │ │ │ └── ic_launcher_round.xml
│ │ │ ├── layout
│ │ │ │ ├── activity_main.xml
│ │ │ │ └── member_item.xml
│ │ │ └── drawable-v24
│ │ │ │ └── ic_launcher_foreground.xml
│ │ ├── AndroidManifest.xml
│ │ └── java
│ │ │ └── com
│ │ │ └── example
│ │ │ └── administrator
│ │ │ └── treeview
│ │ │ ├── MainActivity.java
│ │ │ └── treeView
│ │ │ ├── TreeListView.java
│ │ │ ├── Node.java
│ │ │ └── TreeAdapter.java
│ ├── test
│ │ └── java
│ │ │ └── com
│ │ │ └── example
│ │ │ └── administrator
│ │ │ └── treeview
│ │ │ └── ExampleUnitTest.java
│ └── androidTest
│ │ └── java
│ │ └── com
│ │ └── example
│ │ └── administrator
│ │ └── treeview
│ │ └── ExampleInstrumentedTest.java
├── proguard-rules.pro
└── build.gradle
├── settings.gradle
├── treeView.gif
├── gradle
└── wrapper
│ ├── gradle-wrapper.jar
│ └── gradle-wrapper.properties
├── .gitignore
├── README.md
├── gradle.properties
├── gradlew.bat
└── gradlew
/app/.gitignore:
--------------------------------------------------------------------------------
1 | /build
2 |
--------------------------------------------------------------------------------
/settings.gradle:
--------------------------------------------------------------------------------
1 | include ':app'
2 |
--------------------------------------------------------------------------------
/treeView.gif:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Black-Mango/treeListView/HEAD/treeView.gif
--------------------------------------------------------------------------------
/app/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 | TreeView
3 |
4 |
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Black-Mango/treeListView/HEAD/gradle/wrapper/gradle-wrapper.jar
--------------------------------------------------------------------------------
/app/src/main/res/drawable/down_icon.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Black-Mango/treeListView/HEAD/app/src/main/res/drawable/down_icon.png
--------------------------------------------------------------------------------
/app/src/main/res/drawable/right_icon.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Black-Mango/treeListView/HEAD/app/src/main/res/drawable/right_icon.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-hdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Black-Mango/treeListView/HEAD/app/src/main/res/mipmap-hdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-mdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Black-Mango/treeListView/HEAD/app/src/main/res/mipmap-mdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Black-Mango/treeListView/HEAD/app/src/main/res/mipmap-xhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Black-Mango/treeListView/HEAD/app/src/main/res/mipmap-xxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Black-Mango/treeListView/HEAD/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-hdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Black-Mango/treeListView/HEAD/app/src/main/res/mipmap-hdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-mdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Black-Mango/treeListView/HEAD/app/src/main/res/mipmap-mdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Black-Mango/treeListView/HEAD/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Black-Mango/treeListView/HEAD/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Black-Mango/treeListView/HEAD/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | *.iml
2 | .gradle
3 | /local.properties
4 | /.idea/workspace.xml
5 | /.idea/libraries
6 | .DS_Store
7 | /build
8 | /captures
9 | .externalNativeBuild
10 |
--------------------------------------------------------------------------------
/app/src/main/res/values/colors.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | #3F51B5
4 | #303F9F
5 | #FF4081
6 |
7 |
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.properties:
--------------------------------------------------------------------------------
1 | #Thu May 02 18:02:53 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-4.1-all.zip
7 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # treeListView
2 | 带复选框,可全选可折叠的树形组织结构图/目录/类word文档结构图
3 | 演示gif如下所示,时长25秒,图片大小2.54M,可能第一次加载过程有点卡
4 | 复选框有两种设计模式:
5 | 1、子节点选中则父节点选中,适合多级多item下方便了解哪些被选中;
6 | 2、子节点全部选中父节点才选中,更符合日常逻辑,适合少数量以及少层级。
7 | 
8 |
--------------------------------------------------------------------------------
/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/values/styles.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
10 |
11 |
12 |
--------------------------------------------------------------------------------
/app/src/test/java/com/example/administrator/treeview/ExampleUnitTest.java:
--------------------------------------------------------------------------------
1 | package com.example.administrator.treeview;
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() throws Exception {
15 | assertEquals(4, 2 + 2);
16 | }
17 | }
--------------------------------------------------------------------------------
/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 | org.gradle.jvmargs=-Xmx1536m
13 |
14 | # When configured, Gradle will run in incubating parallel mode.
15 | # This option should only be used with decoupled projects. More details, visit
16 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
17 | # org.gradle.parallel=true
18 |
--------------------------------------------------------------------------------
/app/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 |
5 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
--------------------------------------------------------------------------------
/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/administrator/treeview/ExampleInstrumentedTest.java:
--------------------------------------------------------------------------------
1 | package com.example.administrator.treeview;
2 |
3 | import android.content.Context;
4 | import android.support.test.InstrumentationRegistry;
5 | import android.support.test.runner.AndroidJUnit4;
6 |
7 | import org.junit.Test;
8 | import org.junit.runner.RunWith;
9 |
10 | import static org.junit.Assert.*;
11 |
12 | /**
13 | * Instrumented test, which will execute on an Android device.
14 | *
15 | * @see Testing documentation
16 | */
17 | @RunWith(AndroidJUnit4.class)
18 | public class ExampleInstrumentedTest {
19 | @Test
20 | public void useAppContext() throws Exception {
21 | // Context of the app under test.
22 | Context appContext = InstrumentationRegistry.getTargetContext();
23 |
24 | assertEquals("com.example.administrator.treeview", appContext.getPackageName());
25 | }
26 | }
27 |
--------------------------------------------------------------------------------
/app/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.android.application'
2 |
3 | android {
4 | compileSdkVersion 26
5 | defaultConfig {
6 | applicationId "com.example.administrator.treeview"
7 | minSdkVersion 15
8 | targetSdkVersion 26
9 | versionCode 1
10 | versionName "1.0"
11 | testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
12 | }
13 | buildTypes {
14 | release {
15 | minifyEnabled false
16 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
17 | }
18 | }
19 | }
20 |
21 | dependencies {
22 | implementation fileTree(dir: 'libs', include: ['*.jar'])
23 | implementation 'com.android.support:appcompat-v7:26.1.0'
24 | implementation 'com.android.support.constraint:constraint-layout:1.0.2'
25 | testImplementation 'junit:junit:4.12'
26 | androidTestImplementation 'com.android.support.test:runner:1.0.2'
27 | androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
28 | }
29 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_main.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
8 |
11 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/member_item.xml:
--------------------------------------------------------------------------------
1 |
2 |
8 |
9 |
16 |
17 |
27 |
28 |
39 |
40 |
41 |
42 |
--------------------------------------------------------------------------------
/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/java/com/example/administrator/treeview/MainActivity.java:
--------------------------------------------------------------------------------
1 | package com.example.administrator.treeview;
2 |
3 | import android.content.Context;
4 | import android.support.v7.app.AppCompatActivity;
5 | import android.os.Bundle;
6 | import android.widget.RelativeLayout;
7 | import com.example.administrator.treeview.treeView.Node;
8 | import com.example.administrator.treeview.treeView.TreeListView;
9 |
10 | import java.util.ArrayList;
11 | import java.util.List;
12 |
13 | public class MainActivity extends AppCompatActivity {
14 |
15 | List list = new ArrayList();
16 | private TreeListView listView;
17 | private RelativeLayout relativeLayout, rl;
18 |
19 | @Override
20 | protected void onCreate(Bundle savedInstanceState) {
21 | super.onCreate(savedInstanceState);
22 | setContentView(R.layout.activity_main);
23 | relativeLayout = (RelativeLayout) findViewById(R.id.main_relative_layout);
24 | Context context=MainActivity.this;
25 | rl = new RelativeLayout(context);
26 | rl.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT));
27 | listView = new TreeListView(context, initNodeTree());
28 | listView.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT));
29 | relativeLayout.addView(listView);
30 | }
31 | public List initNodeTree() {
32 |
33 | List member_list =new ArrayList();
34 | // -1表示为根节点,id的作用为
35 | member_list.add(new Node("" + -1, "1" , "111"));
36 | member_list.add(new Node(""+1 , "2" , "222"));
37 | member_list.add(new Node("" + -1, "3" , "333"));
38 | member_list.add(new Node("" + 1, "4" , "444"));
39 | member_list.add(new Node("" + 4, "5" , "555"));
40 | member_list.add(new Node("" + 4, "6" , "666"));
41 | member_list.add(new Node("" + 4, "7" , "777"));
42 | member_list.add(new Node("" + 7, "8" , "888"));
43 | member_list.add(new Node("" + 8, "9" , "999"));
44 | member_list.add(new Node("" + 8, "10" , "101010"));
45 | list.addAll(member_list);
46 | return list;
47 | }
48 | }
49 |
--------------------------------------------------------------------------------
/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/example/administrator/treeview/treeView/TreeListView.java:
--------------------------------------------------------------------------------
1 | package com.example.administrator.treeview.treeView;
2 |
3 | import android.content.Context;
4 | import android.util.Log;
5 | import android.view.View;
6 | import android.view.ViewGroup;
7 | import android.widget.AdapterView;
8 | import android.widget.LinearLayout;
9 | import android.widget.ListView;
10 | import android.widget.RelativeLayout;
11 | import com.example.administrator.treeview.R;
12 | import java.util.ArrayList;
13 | import java.util.Collection;
14 | import java.util.Iterator;
15 | import java.util.LinkedHashMap;
16 | import java.util.List;
17 | import java.util.Map;
18 | import java.util.Set;
19 |
20 | public class TreeListView extends ListView {
21 | ListView treelist = null;
22 | TreeAdapter ta = null;
23 | public List mNodeList;
24 | private List checkList;
25 |
26 |
27 | public TreeListView(final Context context, List res) {
28 | super(context);
29 | treelist = this;
30 | treelist.setFocusable(false);
31 | treelist.setBackgroundColor(0xffffff);
32 | treelist.setFadingEdgeLength(0);
33 | treelist.setLayoutParams(new ViewGroup.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT));
34 |
35 | treelist.setOnItemClickListener(new OnItemClickListener() {
36 |
37 | @Override
38 | public void onItemClick(AdapterView> parent, View view,
39 | int position, long id) {
40 | ((TreeAdapter) parent.getAdapter()).ExpandOrCollapse(position);
41 | }
42 | });
43 | initNode(context, initNodRoot(res), true, -1, -1, 0);
44 | }
45 |
46 | // 使用 onMeasure 方法,来解决尺寸高度的问题,以及事件冲突的问题;
47 | protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
48 | heightMeasureSpec = MeasureSpec.makeMeasureSpec(
49 | Integer.MAX_VALUE>>2,
50 | MeasureSpec.AT_MOST
51 | );
52 | super.onMeasure(widthMeasureSpec, heightMeasureSpec);
53 | }
54 | // /**
55 | // *
56 | // * @param context
57 | // * 响应监听的上下文
58 | // * @param root
59 | // * 已经挂好树的根节点
60 | // * @param hasCheckBox
61 | // * 是否整个树有复选框
62 | // * @param tree_ex_id
63 | // * 展开iconid -1会使用默认的
64 | // * @param tree_ec_id
65 | // * 收缩iconid -1会使用默认的
66 | // * @param expandLevel
67 | // * 初始展开等级
68 | // *
69 | // */
70 | public List initNodRoot(List res) {
71 | ArrayList list = new ArrayList();
72 | ArrayList roots = new ArrayList();
73 | Map nodemap = new LinkedHashMap();
74 | for (int i = 0; i < res.size(); i++) {
75 | Node nr = res.get(i);
76 | Node n = new Node( nr.getParentId(), nr.getCurId(), nr.getValue());
77 | nodemap.put(n.getCurId(), n);// 生成map树
78 | }
79 | Set set = nodemap.keySet();
80 | Collection collections = nodemap.values();
81 | Iterator iterator = collections.iterator();
82 | while (iterator.hasNext()) {// 添加所有根节点到root中
83 | Node n = iterator.next();
84 | if (!set.contains(n.getParentId()))
85 | roots.add(n);
86 | list.add(n);
87 | }
88 | for (int i = 0; i < list.size(); i++) {
89 | Node n = list.get(i);
90 | for (int j = i + 1; j < list.size(); j++) {
91 | Node m = list.get(j);
92 | if (m.getParentId() .equals( n.getCurId())) {
93 | n.addNode(m);
94 | m.setParent(n);
95 | m.setParents(n);
96 | } else if (m.getCurId() .equals( n.getParentId())) {
97 | m.addNode(n);
98 | n.setParent(m);
99 | m.setParents(m);
100 | }
101 | }
102 | }
103 | return roots;
104 | }
105 |
106 | public void initNode(Context context, List root, boolean hasCheckBox,
107 | int tree_ex_id, int tree_ec_id, int expandLevel) {
108 | ta = new TreeAdapter(context, root);
109 | //获取
110 | mNodeList = ta.all;
111 | // 设置整个树是否显示复选框
112 | ta.setCheckBox(true);
113 | // 设置展开和折叠时图标
114 | int tree_ex_id_ = (tree_ex_id == -1) ? R.drawable.down_icon : tree_ex_id;
115 | int tree_ec_id_ = (tree_ec_id == -1) ? R.drawable.right_icon : tree_ec_id;
116 | ta.setCollapseAndExpandIcon(tree_ex_id_, tree_ec_id_);
117 | // 设置默认展开级别
118 | ta.setExpandLevel(expandLevel);
119 | this.setAdapter(ta);
120 | }
121 | /* 返回当前所有选中节点的List数组 */
122 | public List get() {
123 | Log.d("get", ta.getSelectedNode().size() + "");
124 | return ta.getSelectedNode();
125 | }
126 | public void setSelect(List allSelect){
127 | ta.setSelectedNode(allSelect);
128 | }}
129 |
--------------------------------------------------------------------------------
/app/src/main/java/com/example/administrator/treeview/treeView/Node.java:
--------------------------------------------------------------------------------
1 | package com.example.administrator.treeview.treeView;
2 |
3 | import java.io.Serializable;
4 | import java.util.ArrayList;
5 | import java.util.List;
6 |
7 | public class Node implements Serializable {
8 | private Node parent = null; // 父节点
9 | private List childrens = new ArrayList();//子节点
10 | private String value;//节点显示值
11 | private boolean isChecked = false; //是否被选中
12 | private boolean isExpand = true;//是否处于扩展状态
13 | private boolean hasCheckBox = true;//是否有复选框
14 | private String parentId = null;
15 | private String curId = null;
16 |
17 |
18 | //父节点集合
19 | private List parents = new ArrayList<>();
20 |
21 | /**
22 | * 设置节点值
23 | *
24 | * @param parentId
25 | * TODO
26 | * @param curId
27 | * TODO
28 | */
29 | public Node( String parentId, String curId, String value) {
30 | // TODO Auto-generated constructor stub
31 |
32 | this.value = value;
33 | this.parentId = parentId;
34 | this.curId = curId;
35 |
36 | }
37 |
38 | public List getParents() {
39 | return parents;
40 | }
41 |
42 | public void setParents(Node node) {
43 | if(node != null) {
44 | if (!parents.contains(node)) {
45 | parents.add(node);
46 | }
47 | }
48 | }
49 |
50 | /**
51 | * 得到父节点
52 | */
53 | public Node getParent() {
54 | return parent;
55 | }
56 | /**
57 | * 设置父节点
58 | * @param parent
59 | */
60 | public void setParent(Node parent) {
61 | this.parent = parent;
62 | }
63 | /**
64 | * 得到子节点
65 | * @return
66 | */
67 | public List getChildrens() {
68 | return childrens;
69 | }
70 | /**
71 | * pandu是否根节点
72 | * @return
73 | *
74 | */
75 | public boolean isRoot(){
76 | return parent ==null?true:false;
77 | }
78 |
79 | /**
80 | * 是否被选中
81 | * @return
82 | *
83 | */
84 | public boolean isChecked() {
85 | return isChecked;
86 | }
87 | public void setChecked(boolean isChecked) {
88 | this.isChecked = isChecked;
89 | }
90 | /**
91 | * 是否是展开状态
92 | * @return
93 | *
94 | */
95 | public boolean isExplaned() {
96 | return isExpand;
97 | }
98 | /**
99 | * 设置展开状态
100 | * @param isExplaned
101 | *
102 | */
103 | public void setExplaned(boolean isExplaned) {
104 | this.isExpand = isExplaned;
105 | }
106 | /**
107 | * 是否有复选框
108 | * @return
109 | *
110 | */
111 | public boolean hasCheckBox() {
112 | return hasCheckBox;
113 | }
114 | /**
115 | * 设置是否有复选框
116 | * @param hasCheckBox
117 | *
118 | */
119 | public void setHasCheckBox(boolean hasCheckBox) {
120 | this.hasCheckBox = hasCheckBox;
121 | }
122 |
123 |
124 |
125 |
126 | /**
127 | * 得到节点值
128 | * @return
129 | *
130 | */
131 | public String getValue() {
132 | return value;
133 | }
134 | /**
135 | * 设置节点值
136 | * @param value
137 | *
138 | */
139 | public void setValue(String value) {
140 | this.value = value;
141 | }
142 | /**
143 | * 增加一个子节点
144 | * @param node
145 | *
146 | */
147 | public void addNode(Node node){
148 | if(!childrens.contains(node)){
149 | childrens.add(node);
150 | }
151 | }
152 | /**
153 | * 移除一个子节点
154 | * @param node
155 | *
156 | */
157 | public void removeNode(Node node){
158 | if(childrens.contains(node))
159 | childrens.remove(node);
160 | }
161 | /**
162 | * 移除指定位置的子节点
163 | * @param location
164 | *
165 | */
166 | public void removeNode(int location){
167 | childrens.remove(location);
168 | }
169 | /**
170 | * 清除所有子节点
171 | *
172 | */
173 | public void clears(){
174 | childrens.clear();
175 | }
176 | /**
177 | * 判断给出的节点是否当前节点的父节点
178 | * @param node
179 | * @return
180 | *
181 | */
182 | public boolean isParent(Node node){
183 | if(parent == null)return false;
184 | if(parent.equals(node))return true;
185 | return parent.isParent(node);
186 | }
187 | /**
188 | * 递归获取当前节点级别
189 | * @return
190 | *
191 | */
192 | public int getLevel(){
193 | return parent ==null?0:parent.getLevel()+1;
194 | }
195 | /**
196 | * 父节点是否处于折叠的状态
197 | * @return
198 | *
199 | */
200 | public boolean isParentCollapsed(){
201 | if(parent ==null)return false;
202 | if(!parent.isExplaned())return true;
203 | return parent.isParentCollapsed();
204 | }
205 | /**
206 | * 是否叶节点(没有展开下级的几点)
207 | * @return
208 | *
209 | */
210 | public boolean isLeaf(){
211 | return childrens.size()<1?true:false;
212 | }
213 | /**
214 | * 返回自己的id
215 | * @return
216 | **/
217 | public String getCurId() {
218 | // TODO Auto-generated method stub
219 | return curId;
220 | }
221 | /**
222 | * 返回的父id
223 | * @return
224 | **/
225 | public String getParentId() {
226 | // TODO Auto-generated method stub
227 | return parentId;
228 | }
229 |
230 |
231 | }
232 |
--------------------------------------------------------------------------------
/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/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/java/com/example/administrator/treeview/treeView/TreeAdapter.java:
--------------------------------------------------------------------------------
1 | package com.example.administrator.treeview.treeView;
2 |
3 |
4 | import android.content.Context;
5 | import android.util.Log;
6 | import android.view.LayoutInflater;
7 | import android.view.View;
8 | import android.view.View.OnClickListener;
9 | import android.view.ViewGroup;
10 | import android.widget.BaseAdapter;
11 | import android.widget.CheckBox;
12 | import android.widget.ImageView;
13 | import android.widget.TextView;
14 |
15 |
16 | import com.example.administrator.treeview.R;
17 |
18 | import java.util.ArrayList;
19 | import java.util.List;
20 |
21 | public class TreeAdapter extends BaseAdapter {
22 | private Context con;
23 | private LayoutInflater lif;
24 | public List all = new ArrayList();//展示
25 | private List cache = new ArrayList();//缓存,记录点状态
26 | private TreeAdapter tree = this;
27 | boolean hasCheckBox;
28 | private int expandIcon = -1;//展开图标
29 | private int collapseIcon = -1;//收缩图标
30 | ViewItem vi = null;
31 |
32 | // //存储checkbox选中的集合
33 | // private List<>
34 |
35 | /**
36 | * 构造方法
37 | */
38 | public TreeAdapter(Context context, List rootNodes){
39 | this.con = context;
40 | this.lif = (LayoutInflater)con.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
41 | for(int i=0;i getSelectedNode(){
136 | Log.d("getSelectedNode", "我被执行了!");
137 | List checks =new ArrayList() ;
138 | for(int i = 0;i selectedNode){
147 | for (int i=0;i