├── app ├── .gitignore ├── .settings │ └── org.eclipse.buildship.core.prefs ├── src │ ├── main │ │ ├── res │ │ │ ├── values │ │ │ │ ├── strings.xml │ │ │ │ ├── colors.xml │ │ │ │ └── themes.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 │ │ │ ├── menu │ │ │ │ └── home_menu.xml │ │ │ ├── layout │ │ │ │ ├── activity_tree.xml │ │ │ │ ├── item_tasktree.xml │ │ │ │ └── item_filetree.xml │ │ │ ├── drawable │ │ │ │ ├── ic_folder.xml │ │ │ │ ├── ic_file.xml │ │ │ │ └── ic_launcher_background.xml │ │ │ ├── values-night │ │ │ │ └── themes.xml │ │ │ └── drawable-v24 │ │ │ │ └── ic_launcher_foreground.xml │ │ ├── java │ │ │ └── io │ │ │ │ └── github │ │ │ │ └── ikws4 │ │ │ │ └── treeview │ │ │ │ ├── task │ │ │ │ ├── TaskTreeAdapter.java │ │ │ │ └── TaskTreeActivity.java │ │ │ │ ├── documentfile │ │ │ │ ├── DocumentFileTreeActivity.java │ │ │ │ └── DocumentFileTreeAdapter.java │ │ │ │ └── file │ │ │ │ ├── FileTreeActivity.java │ │ │ │ └── FileTreeAdapter.java │ │ └── AndroidManifest.xml │ ├── test │ │ └── java │ │ │ └── io │ │ │ └── github │ │ │ └── ikws4 │ │ │ └── treeview │ │ │ └── ExampleUnitTest.java │ └── androidTest │ │ └── java │ │ └── io │ │ └── github │ │ └── ikws4 │ │ └── treeview │ │ └── ExampleInstrumentedTest.java ├── .classpath ├── .project ├── proguard-rules.pro └── build.gradle ├── treeview ├── consumer-rules.pro ├── .gitignore ├── src │ ├── main │ │ ├── res │ │ │ ├── values │ │ │ │ └── dimens.xml │ │ │ ├── drawable │ │ │ │ └── ic_arrow_right.xml │ │ │ └── layout │ │ │ │ └── item_treeview.xml │ │ ├── AndroidManifest.xml │ │ └── java │ │ │ └── io │ │ │ └── github │ │ │ └── ikws4 │ │ │ └── treeview │ │ │ ├── TreeItem.java │ │ │ ├── TreeItemChildrenList.java │ │ │ └── TreeView.java │ ├── test │ │ └── java │ │ │ └── io │ │ │ └── github │ │ │ └── ikws4 │ │ │ └── treeview │ │ │ └── ExampleUnitTest.java │ └── androidTest │ │ └── java │ │ └── io │ │ └── github │ │ └── ikws4 │ │ └── treeview │ │ └── ExampleInstrumentedTest.java ├── proguard-rules.pro └── build.gradle ├── .idea ├── .gitignore ├── compiler.xml ├── vcs.xml ├── misc.xml ├── gradle.xml └── jarRepositories.xml ├── assets ├── demo.mp4 └── task_tree.png ├── settings.gradle ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── .gitignore ├── .settings └── org.eclipse.buildship.core.prefs ├── .project ├── LICENSE ├── gradle.properties ├── gradlew.bat ├── README.md └── gradlew /app/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /treeview/consumer-rules.pro: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /treeview/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /.idea/.gitignore: -------------------------------------------------------------------------------- 1 | # Default ignored files 2 | /shelf/ 3 | /workspace.xml 4 | -------------------------------------------------------------------------------- /assets/demo.mp4: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ikws4/TreeView/HEAD/assets/demo.mp4 -------------------------------------------------------------------------------- /assets/task_tree.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ikws4/TreeView/HEAD/assets/task_tree.png -------------------------------------------------------------------------------- /settings.gradle: -------------------------------------------------------------------------------- 1 | include ':treeview' 2 | include ':app' 3 | rootProject.name = "TreeView" 4 | -------------------------------------------------------------------------------- /app/.settings/org.eclipse.buildship.core.prefs: -------------------------------------------------------------------------------- 1 | connection.project.dir=.. 2 | eclipse.preferences.version=1 3 | -------------------------------------------------------------------------------- /app/src/main/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | TreeView 3 | 4 | -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ikws4/TreeView/HEAD/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /app/src/main/res/mipmap-hdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ikws4/TreeView/HEAD/app/src/main/res/mipmap-hdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-mdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ikws4/TreeView/HEAD/app/src/main/res/mipmap-mdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ikws4/TreeView/HEAD/app/src/main/res/mipmap-xhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ikws4/TreeView/HEAD/app/src/main/res/mipmap-xxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ikws4/TreeView/HEAD/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-hdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ikws4/TreeView/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/ikws4/TreeView/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/ikws4/TreeView/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/ikws4/TreeView/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/ikws4/TreeView/HEAD/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /treeview/src/main/res/values/dimens.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 28dp 4 | 5 | -------------------------------------------------------------------------------- /.idea/compiler.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /.idea/vcs.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /treeview/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | #Sun Mar 13 13:31:16 CST 2022 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-6.5-bin.zip 7 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *.iml 2 | .gradle 3 | /local.properties 4 | /.idea/caches 5 | /.idea/libraries 6 | /.idea/modules.xml 7 | /.idea/workspace.xml 8 | /.idea/navEditor.xml 9 | /.idea/assetWizardSettings.xml 10 | .DS_Store 11 | /build 12 | /captures 13 | .externalNativeBuild 14 | .cxx 15 | local.properties 16 | -------------------------------------------------------------------------------- /app/src/main/res/mipmap-anydpi-v26/ic_launcher.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /app/src/main/res/mipmap-anydpi-v26/ic_launcher_round.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /app/src/main/res/menu/home_menu.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 6 | 7 | 10 | 11 | -------------------------------------------------------------------------------- /app/.classpath: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | -------------------------------------------------------------------------------- /.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 9 | -------------------------------------------------------------------------------- /app/src/main/res/values/colors.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | #FFBB86FC 4 | #FF6200EE 5 | #FF3700B3 6 | #FF03DAC5 7 | #FF018786 8 | #FF000000 9 | #FFFFFFFF 10 | 11 | -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_tree.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /treeview/src/main/res/drawable/ic_arrow_right.xml: -------------------------------------------------------------------------------- 1 | 7 | 10 | 11 | -------------------------------------------------------------------------------- /.settings/org.eclipse.buildship.core.prefs: -------------------------------------------------------------------------------- 1 | arguments= 2 | auto.sync=false 3 | build.scans.enabled=false 4 | connection.gradle.distribution=GRADLE_DISTRIBUTION(WRAPPER) 5 | connection.project.dir= 6 | eclipse.preferences.version=1 7 | gradle.user.home= 8 | java.home=/Library/Java/JavaVirtualMachines/openjdk-12.0.1.jdk/Contents/Home 9 | jvm.arguments= 10 | offline.mode=false 11 | override.workspace.settings=true 12 | show.console.view=true 13 | show.executions.view=true 14 | -------------------------------------------------------------------------------- /app/src/test/java/io/github/ikws4/treeview/ExampleUnitTest.java: -------------------------------------------------------------------------------- 1 | package io.github.ikws4.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() { 15 | assertEquals(4, 2 + 2); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | TreeView 4 | Project TreeView created by Buildship. 5 | 6 | 7 | 8 | 9 | org.eclipse.buildship.core.gradleprojectbuilder 10 | 11 | 12 | 13 | 14 | 15 | org.eclipse.buildship.core.gradleprojectnature 16 | 17 | 18 | -------------------------------------------------------------------------------- /treeview/src/test/java/io/github/ikws4/treeview/ExampleUnitTest.java: -------------------------------------------------------------------------------- 1 | package io.github.ikws4.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() { 15 | assertEquals(4, 2 + 2); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/ic_folder.xml: -------------------------------------------------------------------------------- 1 | 7 | 10 | 11 | -------------------------------------------------------------------------------- /app/src/main/res/layout/item_tasktree.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | 8 | 13 | 14 | 15 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/ic_file.xml: -------------------------------------------------------------------------------- 1 | 7 | 10 | 11 | -------------------------------------------------------------------------------- /app/.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | app 4 | Project app created by Buildship. 5 | 6 | 7 | 8 | 9 | org.eclipse.jdt.core.javabuilder 10 | 11 | 12 | 13 | 14 | org.eclipse.buildship.core.gradleprojectbuilder 15 | 16 | 17 | 18 | 19 | 20 | org.eclipse.jdt.core.javanature 21 | org.eclipse.buildship.core.gradleprojectnature 22 | 23 | 24 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /treeview/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/main/res/values/themes.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 16 | 17 | -------------------------------------------------------------------------------- /app/src/main/res/values-night/themes.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 16 | 17 | -------------------------------------------------------------------------------- /treeview/src/main/res/layout/item_treeview.xml: -------------------------------------------------------------------------------- 1 | 2 | 9 | 10 | 16 | 17 | 22 | 23 | 24 | -------------------------------------------------------------------------------- /app/src/androidTest/java/io/github/ikws4/treeview/ExampleInstrumentedTest.java: -------------------------------------------------------------------------------- 1 | package io.github.ikws4.treeview; 2 | 3 | import android.content.Context; 4 | 5 | import androidx.test.platform.app.InstrumentationRegistry; 6 | import androidx.test.ext.junit.runners.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.getInstrumentation().getTargetContext(); 24 | assertEquals("io.github.ikws4.treeview", appContext.getPackageName()); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /treeview/src/androidTest/java/io/github/ikws4/treeview/ExampleInstrumentedTest.java: -------------------------------------------------------------------------------- 1 | package io.github.ikws4.treeview; 2 | 3 | import android.content.Context; 4 | 5 | import androidx.test.platform.app.InstrumentationRegistry; 6 | import androidx.test.ext.junit.runners.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.getInstrumentation().getTargetContext(); 24 | assertEquals("io.github.ikws4.treeview.test", appContext.getPackageName()); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /app/src/main/res/layout/item_filetree.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | 8 | 15 | 16 | 25 | 26 | 27 | -------------------------------------------------------------------------------- /.idea/gradle.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 22 | 23 | -------------------------------------------------------------------------------- /app/src/main/java/io/github/ikws4/treeview/task/TaskTreeAdapter.java: -------------------------------------------------------------------------------- 1 | package io.github.ikws4.treeview.task; 2 | 3 | import android.view.View; 4 | import android.widget.TextView; 5 | 6 | import androidx.annotation.NonNull; 7 | 8 | import io.github.ikws4.treeview.R; 9 | import io.github.ikws4.treeview.TreeView; 10 | 11 | class TaskTreeAdapter extends TreeView.Adapter { 12 | 13 | @Override 14 | public ViewHolder onCreateViewHolder(View view) { 15 | return new ViewHolder(view); 16 | } 17 | 18 | @Override 19 | public void onBindViewHolder(@NonNull ViewHolder holder, int position) { 20 | super.onBindViewHolder(holder, position); 21 | holder.name.setText(items.get(position).getValue()); 22 | } 23 | 24 | @Override 25 | public int getLayoutRes() { 26 | return R.layout.item_tasktree; 27 | } 28 | 29 | static class ViewHolder extends TreeView.ViewHolder { 30 | TextView name; 31 | 32 | public ViewHolder(@NonNull View itemView) { 33 | super(itemView); 34 | name = itemView.findViewById(R.id.name); 35 | } 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /treeview/build.gradle: -------------------------------------------------------------------------------- 1 | plugins { 2 | id 'com.android.library' 3 | } 4 | 5 | android { 6 | compileSdkVersion 30 7 | buildToolsVersion "30.0.3" 8 | 9 | defaultConfig { 10 | minSdkVersion 21 11 | targetSdkVersion 30 12 | versionCode 1 13 | versionName "1.0" 14 | 15 | testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" 16 | consumerProguardFiles "consumer-rules.pro" 17 | } 18 | 19 | buildTypes { 20 | release { 21 | minifyEnabled false 22 | proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' 23 | } 24 | } 25 | compileOptions { 26 | sourceCompatibility JavaVersion.VERSION_1_8 27 | targetCompatibility JavaVersion.VERSION_1_8 28 | } 29 | } 30 | 31 | dependencies { 32 | implementation "androidx.recyclerview:recyclerview:1.2.1" 33 | 34 | testImplementation 'junit:junit:4.+' 35 | androidTestImplementation 'androidx.test.ext:junit:1.1.3' 36 | androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0' 37 | } 38 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2022 ikws4 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /.idea/jarRepositories.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 9 | 10 | 14 | 15 | 19 | 20 | 24 | 25 | -------------------------------------------------------------------------------- /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=-Xmx2048m -Dfile.encoding=UTF-8 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 | -------------------------------------------------------------------------------- /app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 7 | 8 | 16 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | -------------------------------------------------------------------------------- /app/build.gradle: -------------------------------------------------------------------------------- 1 | plugins { 2 | id 'com.android.application' 3 | } 4 | 5 | android { 6 | compileSdkVersion 32 7 | buildToolsVersion "30.0.3" 8 | 9 | defaultConfig { 10 | applicationId "io.github.ikws4.treeview" 11 | minSdkVersion 21 12 | targetSdkVersion 32 13 | versionCode 1 14 | versionName "1.0" 15 | 16 | testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" 17 | } 18 | 19 | buildTypes { 20 | release { 21 | minifyEnabled false 22 | proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' 23 | } 24 | } 25 | compileOptions { 26 | sourceCompatibility JavaVersion.VERSION_1_8 27 | targetCompatibility JavaVersion.VERSION_1_8 28 | } 29 | } 30 | 31 | dependencies { 32 | 33 | implementation 'androidx.appcompat:appcompat:1.3.1' 34 | implementation 'com.google.android.material:material:1.4.0' 35 | 36 | implementation project(path: ":treeview") 37 | implementation "com.lazygeniouz:documentfile_compat:0.8" 38 | 39 | testImplementation 'junit:junit:4.+' 40 | androidTestImplementation 'androidx.test.ext:junit:1.1.3' 41 | androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0' 42 | } 43 | -------------------------------------------------------------------------------- /app/src/main/java/io/github/ikws4/treeview/task/TaskTreeActivity.java: -------------------------------------------------------------------------------- 1 | package io.github.ikws4.treeview.task; 2 | 3 | import android.os.Bundle; 4 | 5 | import androidx.appcompat.app.AppCompatActivity; 6 | 7 | import io.github.ikws4.treeview.R; 8 | import io.github.ikws4.treeview.TreeItem; 9 | import io.github.ikws4.treeview.TreeView; 10 | 11 | public class TaskTreeActivity extends AppCompatActivity { 12 | 13 | @Override 14 | protected void onCreate(Bundle savedInstanceState) { 15 | super.onCreate(savedInstanceState); 16 | setContentView(R.layout.activity_tree); 17 | 18 | TreeView treeView = findViewById(R.id.tree_view); 19 | TaskTreeAdapter adapter = new TaskTreeAdapter(); 20 | treeView.setAdapter(adapter); 21 | 22 | TreeItem root = new TreeItem<>("Tasks", true); 23 | TreeItem daily = new TreeItem<>("Daily", true); 24 | TreeItem weekly = new TreeItem<>("Weekly", true); 25 | 26 | daily.getChildren().add(new TreeItem<>("Wake up at 6:00")); 27 | daily.getChildren().add(new TreeItem<>("Drink at least 4 cups of water")); 28 | 29 | weekly.getChildren().add(new TreeItem<>("Week review")); 30 | weekly.getChildren().add(new TreeItem<>("Reward your self")); 31 | 32 | root.setExpanded(true); 33 | root.getChildren().add(daily); 34 | root.getChildren().add(weekly); 35 | 36 | adapter.setRoot(root, true); 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /app/src/main/java/io/github/ikws4/treeview/documentfile/DocumentFileTreeActivity.java: -------------------------------------------------------------------------------- 1 | package io.github.ikws4.treeview.documentfile; 2 | 3 | import android.content.Intent; 4 | import android.net.Uri; 5 | import android.os.Bundle; 6 | 7 | import androidx.activity.result.ActivityResult; 8 | import androidx.activity.result.ActivityResultCallback; 9 | import androidx.activity.result.contract.ActivityResultContracts; 10 | import androidx.appcompat.app.AppCompatActivity; 11 | import androidx.documentfile.provider.DocumentFile; 12 | 13 | import com.lazygeniouz.filecompat.file.DocumentFileCompat; 14 | 15 | import io.github.ikws4.treeview.R; 16 | import io.github.ikws4.treeview.TreeView; 17 | 18 | public class DocumentFileTreeActivity extends AppCompatActivity { 19 | 20 | @Override 21 | protected void onCreate(Bundle savedInstanceState) { 22 | super.onCreate(savedInstanceState); 23 | setContentView(R.layout.activity_tree); 24 | 25 | TreeView treeView = findViewById(R.id.tree_view); 26 | 27 | registerForActivityResult(new ActivityResultContracts.StartActivityForResult(), new ActivityResultCallback() { 28 | @Override 29 | public void onActivityResult(ActivityResult result) { 30 | if (result.getResultCode() == RESULT_OK) { 31 | if (result.getData() != null) { 32 | Uri uri = result.getData().getData(); 33 | DocumentFileCompat documentFile = DocumentFileCompat.Companion.fromTreeUri(DocumentFileTreeActivity.this, uri); 34 | treeView.setAdapter(new DocumentFileTreeAdapter(documentFile)); 35 | } 36 | } 37 | } 38 | }).launch(new Intent(Intent.ACTION_OPEN_DOCUMENT_TREE)); 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /treeview/src/main/java/io/github/ikws4/treeview/TreeItem.java: -------------------------------------------------------------------------------- 1 | package io.github.ikws4.treeview; 2 | 3 | import androidx.annotation.NonNull; 4 | import androidx.annotation.Nullable; 5 | 6 | import java.util.List; 7 | 8 | public class TreeItem { 9 | private final T value; 10 | private final List> children; 11 | private boolean expanded; 12 | private final boolean expandable; 13 | TreeItem parent; 14 | 15 | public TreeItem(@NonNull T value) { 16 | this(value, false); 17 | } 18 | 19 | public TreeItem(@NonNull T value, boolean expandable) { 20 | this.value = value; 21 | this.expandable = expandable; 22 | this.children = new TreeItemChildrenList(this); 23 | } 24 | 25 | public boolean isExpanded() { 26 | return expanded; 27 | } 28 | 29 | /** 30 | * @throws IllegalStateException if item is not expandable 31 | */ 32 | public void setExpanded(boolean expanded) { 33 | ensureSelfIsExpandable(); 34 | this.expanded = expanded; 35 | } 36 | 37 | public boolean isExpandable() { 38 | return expandable; 39 | } 40 | 41 | public T getValue() { 42 | return value; 43 | } 44 | 45 | @Nullable 46 | public TreeItem getParent() { 47 | return parent; 48 | } 49 | 50 | @NonNull 51 | public List> getChildren() { 52 | return children; 53 | } 54 | 55 | int getDepth() { 56 | if (parent == null) return 0; 57 | return parent.getDepth() + 1; 58 | } 59 | 60 | @Override 61 | public String toString() { 62 | return "TreeItem(" + 63 | "value=" + value + 64 | ", expanded=" + expanded + 65 | ')'; 66 | } 67 | 68 | private void ensureSelfIsExpandable() { 69 | if (!expandable) { 70 | throw new IllegalStateException(this.toString() + " is not expandable."); 71 | } 72 | } 73 | } 74 | -------------------------------------------------------------------------------- /app/src/main/res/drawable-v24/ic_launcher_foreground.xml: -------------------------------------------------------------------------------- 1 | 7 | 8 | 9 | 15 | 18 | 21 | 22 | 23 | 24 | 30 | 31 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /app/src/main/java/io/github/ikws4/treeview/file/FileTreeActivity.java: -------------------------------------------------------------------------------- 1 | package io.github.ikws4.treeview.file; 2 | 3 | import android.Manifest; 4 | import android.content.Intent; 5 | import android.content.pm.PackageManager; 6 | import android.os.Build; 7 | import android.os.Bundle; 8 | import android.os.Environment; 9 | import android.provider.Settings; 10 | import android.view.Menu; 11 | import android.view.MenuItem; 12 | import android.widget.Toast; 13 | 14 | import androidx.annotation.NonNull; 15 | import androidx.appcompat.app.AppCompatActivity; 16 | import androidx.core.app.ActivityCompat; 17 | 18 | import io.github.ikws4.treeview.R; 19 | import io.github.ikws4.treeview.TreeView; 20 | import io.github.ikws4.treeview.documentfile.DocumentFileTreeActivity; 21 | import io.github.ikws4.treeview.task.TaskTreeActivity; 22 | 23 | public class FileTreeActivity extends AppCompatActivity { 24 | private static final int EXTERNAL_STORAGE_REQUEST_CODE = 1; 25 | 26 | @Override 27 | protected void onCreate(Bundle savedInstanceState) { 28 | super.onCreate(savedInstanceState); 29 | setContentView(R.layout.activity_tree); 30 | 31 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) { 32 | if (!Environment.isExternalStorageManager()) { 33 | startActivity(new Intent(Settings.ACTION_MANAGE_ALL_FILES_ACCESS_PERMISSION)); 34 | } 35 | } 36 | 37 | ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, EXTERNAL_STORAGE_REQUEST_CODE); 38 | } 39 | 40 | @Override 41 | public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { 42 | super.onRequestPermissionsResult(requestCode, permissions, grantResults); 43 | 44 | if (requestCode == EXTERNAL_STORAGE_REQUEST_CODE) { 45 | if (grantResults[0] == PackageManager.PERMISSION_GRANTED) { 46 | TreeView treeView = findViewById(R.id.tree_view); 47 | treeView.setAdapter(new FileTreeAdapter()); 48 | } else { 49 | Toast.makeText(this, "You should enable external storage premission to get this app work.", Toast.LENGTH_SHORT).show(); 50 | } 51 | } 52 | } 53 | 54 | @Override 55 | public boolean onCreateOptionsMenu(Menu menu) { 56 | getMenuInflater().inflate(R.menu.home_menu, menu); 57 | return true; 58 | } 59 | 60 | @Override 61 | public boolean onOptionsItemSelected(@NonNull MenuItem item) { 62 | int id = item.getItemId(); 63 | if (id == R.id.task_tree) { 64 | startActivity(new Intent(this, TaskTreeActivity.class)); 65 | } else if (id == R.id.document_file_tree) { 66 | startActivity(new Intent(this, DocumentFileTreeActivity.class)); 67 | } 68 | return super.onOptionsItemSelected(item); 69 | } 70 | } 71 | -------------------------------------------------------------------------------- /app/src/main/java/io/github/ikws4/treeview/file/FileTreeAdapter.java: -------------------------------------------------------------------------------- 1 | package io.github.ikws4.treeview.file; 2 | 3 | 4 | import android.os.Environment; 5 | import android.view.View; 6 | import android.widget.ImageView; 7 | import android.widget.TextView; 8 | 9 | import androidx.annotation.NonNull; 10 | 11 | import java.io.File; 12 | import java.util.Collections; 13 | 14 | import io.github.ikws4.treeview.R; 15 | import io.github.ikws4.treeview.TreeItem; 16 | import io.github.ikws4.treeview.TreeView; 17 | 18 | class FileTreeAdapter extends TreeView.Adapter implements TreeView.Adapter.OnTreeItemClickListener { 19 | 20 | public FileTreeAdapter() { 21 | setTreeItemClickListener(this); 22 | File rootFile = Environment.getExternalStorageDirectory(); 23 | TreeItem root = new TreeItem<>(rootFile.getPath(), true); 24 | expand(root); 25 | setRoot(root); 26 | } 27 | 28 | @Override 29 | public int getLayoutRes() { 30 | return R.layout.item_filetree; 31 | } 32 | 33 | @Override 34 | public ViewHolder onCreateViewHolder(View view) { 35 | return new ViewHolder(view); 36 | } 37 | 38 | @Override 39 | public void onBindViewHolder(@NonNull ViewHolder holder, int position) { 40 | super.onBindViewHolder(holder, position); 41 | 42 | TreeItem item = items.get(position); 43 | if (item.isExpandable()) { 44 | holder.icon.setImageResource(R.drawable.ic_folder); 45 | } else { 46 | holder.icon.setImageResource(R.drawable.ic_file); 47 | } 48 | 49 | holder.name.setText(item.getValue()); 50 | } 51 | 52 | @Override 53 | public void onClick(TreeItem item) { 54 | if (item.isExpandable() && !item.isExpanded()) { 55 | expand(item); 56 | } 57 | } 58 | 59 | static class ViewHolder extends TreeView.ViewHolder { 60 | ImageView icon; 61 | TextView name; 62 | 63 | public ViewHolder(@NonNull View itemView) { 64 | super(itemView); 65 | icon = itemView.findViewById(R.id.icon); 66 | name = itemView.findViewById(R.id.name); 67 | } 68 | } 69 | 70 | private void expand(TreeItem root) { 71 | if (!root.getChildren().isEmpty()) return; 72 | 73 | for (File child : getChildren(new File(getFilePath(root)))) { 74 | TreeItem item = new TreeItem<>(child.getName(), child.isDirectory()); 75 | root.getChildren().add(item); 76 | } 77 | Collections.sort(root.getChildren(), (a, b) -> { 78 | int res = Boolean.compare(b.isExpandable(), a.isExpandable()); 79 | if (res == 0) return a.getValue().compareToIgnoreCase(b.getValue()); 80 | return res; 81 | }); 82 | } 83 | 84 | private String getFilePath(TreeItem root) { 85 | if (root == null) return ""; 86 | 87 | return getFilePath(root.getParent()) + "/" + root.getValue(); 88 | } 89 | 90 | private File[] getChildren(File file) { 91 | File[] children = file.listFiles(); 92 | return children == null ? new File[0] : children; 93 | } 94 | } 95 | -------------------------------------------------------------------------------- /app/src/main/java/io/github/ikws4/treeview/documentfile/DocumentFileTreeAdapter.java: -------------------------------------------------------------------------------- 1 | package io.github.ikws4.treeview.documentfile; 2 | 3 | import android.view.View; 4 | import android.widget.ImageView; 5 | import android.widget.TextView; 6 | 7 | import androidx.annotation.NonNull; 8 | import androidx.documentfile.provider.DocumentFile; 9 | 10 | import com.lazygeniouz.filecompat.file.DocumentFileCompat; 11 | 12 | import java.util.Collections; 13 | import java.util.List; 14 | 15 | import io.github.ikws4.treeview.R; 16 | import io.github.ikws4.treeview.TreeItem; 17 | import io.github.ikws4.treeview.TreeView; 18 | 19 | class DocumentFileTreeAdapter extends TreeView.Adapter implements TreeView.Adapter.OnTreeItemClickListener { 20 | 21 | public DocumentFileTreeAdapter(DocumentFileCompat rootFile) { 22 | setTreeItemClickListener(this); 23 | TreeItem root = new TreeItem<>(rootFile, true); 24 | expand(root); 25 | setRoot(root); 26 | } 27 | 28 | @Override 29 | public int getLayoutRes() { 30 | return R.layout.item_filetree; 31 | } 32 | 33 | @Override 34 | public ViewHolder onCreateViewHolder(View view) { 35 | return new ViewHolder(view); 36 | } 37 | 38 | @Override 39 | public void onBindViewHolder(@NonNull ViewHolder holder, int position) { 40 | super.onBindViewHolder(holder, position); 41 | 42 | TreeItem item = items.get(position); 43 | if (item.isExpandable()) { 44 | holder.icon.setImageResource(R.drawable.ic_folder); 45 | } else { 46 | holder.icon.setImageResource(R.drawable.ic_file); 47 | } 48 | 49 | holder.name.setText(item.getValue().getName()); 50 | } 51 | 52 | @Override 53 | public void onClick(TreeItem item) { 54 | if (item.isExpandable() && !item.isExpanded()) { 55 | expand(item); 56 | } 57 | } 58 | 59 | static class ViewHolder extends TreeView.ViewHolder { 60 | ImageView icon; 61 | TextView name; 62 | 63 | public ViewHolder(@NonNull View itemView) { 64 | super(itemView); 65 | icon = itemView.findViewById(R.id.icon); 66 | name = itemView.findViewById(R.id.name); 67 | } 68 | } 69 | 70 | private void expand(TreeItem root) { 71 | if (!root.getChildren().isEmpty()) return; 72 | 73 | for (DocumentFileCompat child : getChildren(root)) { 74 | TreeItem item = new TreeItem<>(child, !child.isFile()); 75 | root.getChildren().add(item); 76 | } 77 | 78 | Collections.sort(root.getChildren(), (a, b) -> { 79 | int res = Boolean.compare(b.isExpandable(), a.isExpandable()); 80 | if (res == 0) return a.getValue().getName().compareToIgnoreCase(b.getValue().getName()); 81 | return res; 82 | }); 83 | } 84 | 85 | private List getChildren(TreeItem file) { 86 | return file.getValue().listFiles(); 87 | } 88 | } 89 | 90 | -------------------------------------------------------------------------------- /treeview/src/main/java/io/github/ikws4/treeview/TreeItemChildrenList.java: -------------------------------------------------------------------------------- 1 | package io.github.ikws4.treeview; 2 | 3 | import androidx.annotation.NonNull; 4 | import androidx.annotation.Nullable; 5 | 6 | import java.util.ArrayList; 7 | import java.util.Collection; 8 | import java.util.Iterator; 9 | import java.util.List; 10 | import java.util.ListIterator; 11 | 12 | class TreeItemChildrenList implements List>{ 13 | private final List> list = new ArrayList<>(); 14 | private final TreeItem parent; 15 | 16 | public TreeItemChildrenList(TreeItem parent) { 17 | this.parent = parent; 18 | } 19 | 20 | @Override 21 | public int size() { 22 | return list.size(); 23 | } 24 | 25 | @Override 26 | public boolean isEmpty() { 27 | return list.isEmpty(); 28 | } 29 | 30 | @Override 31 | public boolean contains(@Nullable Object o) { 32 | return list.contains(o); 33 | } 34 | 35 | @NonNull 36 | @Override 37 | public Iterator> iterator() { 38 | return list.iterator(); 39 | } 40 | 41 | @NonNull 42 | @Override 43 | public Object[] toArray() { 44 | return list.toArray(); 45 | } 46 | 47 | @NonNull 48 | @Override 49 | public E[] toArray(@NonNull E[] a) { 50 | throw new UnsupportedOperationException(); 51 | } 52 | 53 | /** 54 | * @throws IllegalStateException if parent is not a expandable item 55 | */ 56 | @Override 57 | public boolean add(TreeItem ele) { 58 | ensureParentIsExpandable(); 59 | 60 | ele.parent = parent; 61 | return list.add(ele); 62 | } 63 | 64 | @Override 65 | public boolean remove(@Nullable Object o) { 66 | return list.remove(o); 67 | } 68 | 69 | @Override 70 | public boolean containsAll(@NonNull Collection c) { 71 | return list.containsAll(c); 72 | } 73 | 74 | /** 75 | * @throws IllegalStateException if item is not expandable 76 | */ 77 | @Override 78 | public boolean addAll(@NonNull Collection> c) { 79 | ensureParentIsExpandable(); 80 | 81 | for (TreeItem ele : c) { 82 | ele.parent = parent; 83 | } 84 | 85 | return list.addAll(c); 86 | } 87 | 88 | /** 89 | * @throws IllegalStateException if item is not expandable 90 | */ 91 | @Override 92 | public boolean addAll(int index, @NonNull Collection> c) { 93 | ensureParentIsExpandable(); 94 | 95 | for (TreeItem ele : c) { 96 | ele.parent = parent; 97 | } 98 | 99 | return list.addAll(index, c); 100 | } 101 | 102 | @Override 103 | public boolean removeAll(@NonNull Collection c) { 104 | return list.removeAll(c); 105 | } 106 | 107 | @Override 108 | public boolean retainAll(@NonNull Collection c) { 109 | return list.retainAll(c); 110 | } 111 | 112 | @Override 113 | public void clear() { 114 | list.clear(); 115 | } 116 | 117 | @Override 118 | public TreeItem get(int index) { 119 | return list.get(index); 120 | } 121 | 122 | /** 123 | * @throws IllegalStateException if item is not expandable 124 | */ 125 | @Override 126 | public TreeItem set(int index, TreeItem ele) { 127 | ensureParentIsExpandable(); 128 | 129 | ele.parent = parent; 130 | return list.set(index, ele); 131 | } 132 | 133 | /** 134 | * @throws IllegalStateException if item is not expandable 135 | */ 136 | @Override 137 | public void add(int index, TreeItem ele) { 138 | ensureParentIsExpandable(); 139 | 140 | ele.parent = parent; 141 | list.add(index, ele); 142 | } 143 | 144 | @Override 145 | public TreeItem remove(int index) { 146 | return list.remove(index); 147 | } 148 | 149 | @Override 150 | public int indexOf(@Nullable Object o) { 151 | return list.indexOf(o); 152 | } 153 | 154 | @Override 155 | public int lastIndexOf(@Nullable Object o) { 156 | return list.lastIndexOf(o); 157 | } 158 | 159 | @NonNull 160 | @Override 161 | public ListIterator> listIterator() { 162 | return list.listIterator(); 163 | } 164 | 165 | @NonNull 166 | @Override 167 | public ListIterator> listIterator(int index) { 168 | return list.listIterator(index); 169 | } 170 | 171 | @NonNull 172 | @Override 173 | public List> subList(int fromIndex, int toIndex) { 174 | return list.subList(fromIndex, toIndex); 175 | } 176 | 177 | private void ensureParentIsExpandable() { 178 | if (!parent.isExpandable()) { 179 | throw new IllegalStateException(parent.toString() + " is not expandable."); 180 | } 181 | } 182 | } 183 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # TreeView 2 | 3 | A general TreeView implementation for android base on RecyclerView. 4 | 5 | # Setup 6 | 7 | [![](https://jitpack.io/v/ikws4/TreeView.svg)](https://jitpack.io/#ikws4/TreeView) 8 | 9 | 1. Add it in your root build.gradle at the end of repositories: 10 | 11 | ```gradle 12 | allprojects { 13 | repositories { 14 | ... 15 | maven { url "https://jitpack.io" } 16 | } 17 | } 18 | ``` 19 | 20 | 2. Add the dependency 21 | 22 | ```gradle 23 | dependencies { 24 | implementation "com.github.ikws4:TreeView:0.1.1" 25 | } 26 | ``` 27 | 28 | # Examples 29 | 30 | ## FileTree 31 | 32 | Check out [FileTreeAdapter](https://github.com/ikws4/TreeView/blob/main/app/src/main/java/io/github/ikws4/treeview/file/FileTreeAdapter.java) if you want to learn more. 33 | 34 | https://user-images.githubusercontent.com/47056144/158051176-6f45652c-5262-407a-a0ca-6c8c30c0f892.mp4 35 | 36 | ## TaskTree 37 | 38 | 39 | 40 | Create a custom adapter by extends `TreeView.Adapter`. 41 | 42 | ```java 43 | class TaskTreeAdapter extends TreeView.Adapter { 44 | 45 | @Override 46 | public ViewHolder onCreateViewHolder(View view) { 47 | return new ViewHolder(view); 48 | } 49 | 50 | @Override 51 | public void onBindViewHolder(@NonNull ViewHolder holder, int position) { 52 | super.onBindViewHolder(holder, position); 53 | holder.name.setText(items.get(position).getValue()); 54 | } 55 | 56 | @Override 57 | public int getLayoutRes() { 58 | return R.layout.item_tasktree; 59 | } 60 | 61 | static class ViewHolder extends TreeView.ViewHolder { 62 | TextView name; 63 | 64 | public ViewHolder(@NonNull View itemView) { 65 | super(itemView); 66 | name = itemView.findViewById(R.id.name); 67 | } 68 | } 69 | } 70 | ``` 71 | 72 | `item_tasktree.xml` 73 | 74 | ```xml 75 | 76 | 81 | 82 | 87 | 88 | 89 | ``` 90 | 91 | `activity_main.xml` 92 | 93 | ```xml 94 | 95 | 98 | 99 | 103 | 104 | 105 | ``` 106 | 107 | Bind TreeView with your custom adapter by calling `treeView.setAdapter()` 108 | 109 | ```java 110 | TreeView treeView = findViewById(R.id.tree_view); 111 | TaskTreeAdapter adapter = new TaskTreeAdapter(); 112 | treeView.setAdapter(adapter); 113 | 114 | TreeItem root = new TreeItem<>("Tasks", true); 115 | TreeItem daily = new TreeItem<>("Daily", true); 116 | TreeItem weekly = new TreeItem<>("Weekly", true); 117 | 118 | daily.getChildren().add(new TreeItem<>("Wake up at 6:00")); 119 | daily.getChildren().add(new TreeItem<>("Drink at least 4 cups of water")); 120 | 121 | weekly.getChildren().add(new TreeItem<>("Week review")); 122 | weekly.getChildren().add(new TreeItem<>("Reward your self")); 123 | 124 | root.setExpanded(true); 125 | root.getChildren().add(daily); 126 | root.getChildren().add(weekly); 127 | 128 | adapter.setRoot(root, true); 129 | ``` 130 | 131 | # Thanks 132 | 133 | This library was inspired by [RecyclerTreeView](https://github.com/TellH/RecyclerTreeView) 134 | 135 | # License 136 | 137 | ``` 138 | MIT License 139 | 140 | Copyright (c) 2022 ikws4 141 | 142 | Permission is hereby granted, free of charge, to any person obtaining a copy 143 | of this software and associated documentation files (the "Software"), to deal 144 | in the Software without restriction, including without limitation the rights 145 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 146 | copies of the Software, and to permit persons to whom the Software is 147 | furnished to do so, subject to the following conditions: 148 | 149 | The above copyright notice and this permission notice shall be included in all 150 | copies or substantial portions of the Software. 151 | 152 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 153 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 154 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 155 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 156 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 157 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 158 | SOFTWARE. 159 | ``` 160 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /treeview/src/main/java/io/github/ikws4/treeview/TreeView.java: -------------------------------------------------------------------------------- 1 | package io.github.ikws4.treeview; 2 | 3 | import android.content.Context; 4 | import android.util.AttributeSet; 5 | import android.view.LayoutInflater; 6 | import android.view.View; 7 | import android.view.ViewGroup; 8 | import android.widget.ImageView; 9 | 10 | import androidx.annotation.NonNull; 11 | import androidx.annotation.Nullable; 12 | import androidx.recyclerview.widget.LinearLayoutManager; 13 | import androidx.recyclerview.widget.RecyclerView; 14 | 15 | import java.util.ArrayList; 16 | import java.util.List; 17 | 18 | public class TreeView extends RecyclerView { 19 | 20 | public TreeView(@NonNull Context context) { 21 | super(context); 22 | init(); 23 | } 24 | 25 | public TreeView(@NonNull Context context, @Nullable AttributeSet attrs) { 26 | super(context, attrs); 27 | init(); 28 | } 29 | 30 | public TreeView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) { 31 | super(context, attrs, defStyleAttr); 32 | init(); 33 | } 34 | 35 | private void init() { 36 | setLayoutManager(new LinearLayoutManager(getContext())); 37 | } 38 | 39 | public void setAdapter(@Nullable Adapter adapter) { 40 | super.setAdapter(adapter); 41 | 42 | if (adapter != null) { 43 | adapter.setIdentationWidth(getResources().getDimensionPixelSize(R.dimen.treeview_default_indentation_width)); 44 | } 45 | } 46 | 47 | public abstract static class Adapter extends RecyclerView.Adapter { 48 | private static final int ANIM_TIME = 200; 49 | private int indentationWidth; 50 | public Adapter.OnTreeItemClickListener listener; 51 | protected List> items = new ArrayList<>(); 52 | private boolean showRoot = false; 53 | 54 | public abstract VH onCreateViewHolder(View view); 55 | 56 | public abstract int getLayoutRes(); 57 | 58 | @NonNull 59 | @Override 60 | public VH onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { 61 | LayoutInflater inflater = LayoutInflater.from(parent.getContext()); 62 | View view = inflater.inflate(R.layout.item_treeview, parent, false); 63 | inflater.inflate(getLayoutRes(), view.findViewById(R.id.content), true); 64 | return onCreateViewHolder(view); 65 | } 66 | 67 | @Override 68 | public void onBindViewHolder(@NonNull VH holder, int position) { 69 | TreeItem item = items.get(position); 70 | View itemView = holder.itemView; 71 | ImageView leadingIcon = holder.leadingIcon; 72 | 73 | itemView.setPadding(computeIndentPadding(item), 0, 0, 0); 74 | 75 | if (item.isExpandable()) { 76 | 77 | if (item.isExpanded()) { 78 | leadingIcon.setRotation(90); 79 | } else { 80 | leadingIcon.setRotation(0); 81 | } 82 | 83 | leadingIcon.setVisibility(View.VISIBLE); 84 | } else { 85 | leadingIcon.setVisibility(View.INVISIBLE); 86 | } 87 | 88 | itemView.setOnClickListener((v) -> { 89 | if (listener != null) { 90 | listener.onClick(item); 91 | } 92 | 93 | // click with debounce 94 | long lastClickTime = (long) itemView.getTag(); 95 | if (System.currentTimeMillis() - lastClickTime < ANIM_TIME) { 96 | return; 97 | } 98 | itemView.setTag(System.currentTimeMillis()); 99 | 100 | if (item.isExpandable()) { 101 | if (item.isExpanded()) { 102 | int removedCount = removeAllExpandedItemRecursive(item, holder.getLayoutPosition() + 1); 103 | notifyItemRangeRemoved(holder.getLayoutPosition() + 1, removedCount); 104 | } else { 105 | item.setExpanded(true); 106 | int insertedCount = insertAllExpandedItemRecursive(item, holder.getLayoutPosition() + 1); 107 | notifyItemRangeInserted(holder.getLayoutPosition() + 1, insertedCount); 108 | item.setExpanded(false); 109 | } 110 | 111 | leadingIcon.animate() 112 | .setDuration(ANIM_TIME) 113 | .rotationBy(item.isExpanded() ? -90 : 90) 114 | .start(); 115 | 116 | item.setExpanded(!item.isExpanded()); 117 | } 118 | }); 119 | } 120 | 121 | private int computeIndentPadding(TreeItem item) { 122 | int depth = item.getDepth(); 123 | if (!showRoot) depth--; 124 | return indentationWidth * depth; 125 | } 126 | 127 | private int removeAllExpandedItemRecursive(TreeItem root, int index) { 128 | if (root == null || !root.isExpanded()) return 0; 129 | 130 | int cnt = 0; 131 | for (TreeItem child : root.getChildren()) { 132 | items.remove(index); 133 | cnt += removeAllExpandedItemRecursive(child, index) + 1; 134 | } 135 | 136 | return cnt; 137 | } 138 | 139 | private int insertAllExpandedItemRecursive(TreeItem root, int index) { 140 | if (root == null || !root.isExpanded()) return 0; 141 | 142 | int cnt = 0; 143 | for (TreeItem child : root.getChildren()) { 144 | items.add(index + cnt, child); 145 | cnt += insertAllExpandedItemRecursive(child, index + cnt + 1) + 1; 146 | } 147 | 148 | return cnt; 149 | } 150 | 151 | @Override 152 | public int getItemCount() { 153 | return items.size(); 154 | } 155 | 156 | public void setRoot(TreeItem root) { 157 | setRoot(root, false); 158 | } 159 | 160 | public void setRoot(TreeItem root, boolean showRoot) { 161 | items.clear(); 162 | 163 | if (showRoot) { 164 | items.add(root); 165 | 166 | if (root.isExpanded()) { 167 | items.addAll(root.getChildren()); 168 | } 169 | } else { 170 | items.addAll(root.getChildren()); 171 | } 172 | 173 | notifyDataSetChanged(); 174 | 175 | this.showRoot = showRoot; 176 | } 177 | 178 | public void setTreeItemClickListener(Adapter.OnTreeItemClickListener listener) { 179 | this.listener = listener; 180 | } 181 | 182 | void setIdentationWidth(int width) { 183 | this.indentationWidth = width; 184 | } 185 | 186 | public interface OnTreeItemClickListener { 187 | void onClick(TreeItem item); 188 | } 189 | } 190 | 191 | public static class ViewHolder extends RecyclerView.ViewHolder { 192 | ImageView leadingIcon; 193 | 194 | public ViewHolder(@NonNull View itemView) { 195 | super(itemView); 196 | leadingIcon = itemView.findViewById(R.id.leading_icon); 197 | itemView.setTag(System.currentTimeMillis()); 198 | } 199 | } 200 | 201 | } 202 | --------------------------------------------------------------------------------