Lightweight and fast file tree for Android
This FileTree is no longer maintained in favour of https://github.com/dingyi222666/TreeView
FileTree
is a custom Android view that extends RecyclerView
to display a hierarchical file structure. This library provides a flexible and visually appealing way to present file trees in your Android applications.
- Add Jitpack repository
dependencyResolutionManagement {
repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
repositories {
mavenCentral()
maven { url 'https://jitpack.io' }
}
}
-
Add Dependency
Include the FileTree library in your project. Add the following to your
build.gradle
file:currently not available you have to add the library manually
implementation 'com.github.RohitKushvaha01:FileTree:Tag'
Replace the tag TAG with a realese tag
-
Initialization
You can initialize the
FileTree
view programmatically or define it directly in your XML layout.XML Layout Example:
<!-- XML layout file example --> <com.rk.filetree.widget.FileTree android:layout_width="match_parent" android:layout_height="match_parent" />
Programmatic Initialization:
// Initialize FileTree programmatically val fileTree = FileTree(this)
It is recommended to wrap the
FileTree
view within aHorizontalScrollView
or the customDiagonalScrollView
provided by this library for smoother navigation through large file structures.
Use the loadFiles
method to populate the FileTree
with file data. Ensure you create a wrapper for the FileObject
interface.
Example:
// Using private app data files as a demo
val targetFile = filesDir.parentFile!!
// Creating a FileObject wrapper
val fileObject = file(targetFile)
// Load the file tree
fileTree.loadFiles(fileObject)
- The
loadFiles
method accepts an optional boolean parametershowRootNodeX
. If set tofalse
, the root node will not be displayed. Iftrue
ornull
, the root node will be shown. To change the root file, call theloadFiles
method again with a differentFileObject
.
By default, FileTree
uses a default icon provider. To use custom icons, implement and set your own FileIconProvider
.
Example:
fileTree.setIconProvider(object : FileIconProvider {
override fun getIcon(node: Node<FileObject>): Drawable? {
val fileObject = node.value
return if (fileObject.isDirectory()) {
directoryDrawable // Custom drawable for directories
} else {
fileDrawable // Custom drawable for files
}
}
override fun getChevronRight(): Drawable? {
return chevronDrawable // Custom drawable for the chevron icon
}
override fun getExpandMore(): Drawable? {
return expandChevronDrawable // Custom drawable for the expand more icon
}
})
You can set click and long-click listeners to handle user interactions with the file items.
Example:
// Setting a file click listener
fileTree.setOnFileClickListener(object : FileClickListener {
override fun onClick(node: Node<FileObject>) {
val fileObject = node.value
Toast.makeText(this@MainActivity, "Clicked: ${fileObject.getName()}", Toast.LENGTH_SHORT).show()
}
})
// Setting a file long-click listener
fileTree.setOnFileLongClickListener(object : FileLongClickListener {
override fun onLongClick(node: Node<FileObject>) {
val fileObject = node.value
Toast.makeText(this@MainActivity, "Long clicked: ${fileObject.getName()}", Toast.LENGTH_SHORT).show()
}
})
To refresh the file tree, use the reloadFileTree
method.
Example:
// Refresh the file tree
fileTree.reloadFileTree()
If you'd like to contribute to the development of this library, please follow the standard GitHub workflow for contributing:
- Fork the repository.
- Create a feature branch.
- Make your changes and test them.
- Submit a pull request with a detailed description of your changes.
This project is licensed under the Apache License 2.0.
For any questions or support, please reach out to [email protected].