Git Product home page Git Product logo

tree2view's Introduction

Tree2View

TreeView implementation in Android.Now available on Jitpack:tada:

中文版

Features

TreeView File Explorer(Advanced Example)
①Multi-level tree view Basic file manager layout
②Remember expansion state Automatically expand the last unclosed directory
③Customize TreeAdapter Different types of documents show different Icon
④Dynamic add and delete nodes refresh status after delete and add files
⑤Select listener Long press node for file operations (Copy, Cut, Rename, Delete)
⑥Animation support Add or delete files with animation

You can also see a more simple example.

Implement

  • TreeView extends from ListView.

  • DFS travel the expandable tree node, and convert it to List which adapt with TreeAdapter.

  • Use SimpleTreeAdapter ot set different indentation on nodes of different depths.

  • Use LinkedList to store node's children, see DefaultTreeNode.

Preview

Download

  1. Add it in your root build.gradle at the end of repositories:
	allprojects {
		repositories {
			...
			maven { url 'https://jitpack.io' }
		}
	}
  1. Add the dependency
	dependencies {
	        implementation 'com.github.LeeReindeer:Tree2View:v0.1.2'
	}

Usage

  1. Add in your xml:

Feel free to use it as ListView.

    <moe.leer.tree2view.TreeView
        android:id="@+id/tree_view"
        android:layout_marginTop="16dp"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:divider="#ffffff"
        android:dividerHeight="1px">

    </moe.leer.tree2view.TreeView>
  1. Add children in Kotlin code(Java is similar whit it)
  var root :DefaultTreeNode? = DefaultTreeNode("Root")
  tree_view.root = root
  val child1 = DefaultTreeNode("Child1")
  val child2 = DefaultTreeNode("Child2")
  // After create a node your should immediately add it.
  root.addChild(child1)
  root.addChild(child2)
  val child3 = DefaultTreeNode("Child3")
  child1.addChild(child3)
  //whether the root's children is expanded by default
  tree_view.isRootVisible = true
  //animation
  tree_view.isDefaultAnimation = true
  1. Add click listener and select listener

Tree2View has a default click listener, but it's ok to add your own.You can refer to here.

  1. If you want to use customized item view, you should implement TreeAapater, like this:
public class FileTreeAdapter extends TreeAdapter<FileItem> {
  //resourceId is your customized view resourceId, please use RelativeLayout, and let view neighbour.
  public FileTreeAdapter(Context context, DefaultTreeNode root, int resourceId) {
    super(context, root, resourceId);
  }
  
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
     //your code here
     //...
     //call padding for a better UI
     setPadding(holder.arrowIcon, depth, -1);
     //toggle your view's status here
     toggle(node, holder);
    }
    
    @Override
     public void toggle(Object... objects) {
     }

Then simply add:

  val adapter = FileTreeAdapter(this@MainActivity, root, R.layout.layout_file_tree_item)
  treeView.treeAdapter = adapter

License

Apache 2.0

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.