ikws4 / TreeView

A general TreeView implementation for android base on RecyclerView.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

TreeView

A general TreeView implementation for android base on RecyclerView.

Setup

  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.ikws4:TreeView:0.1.1"
}

Examples

FileTree

Check out FileTreeAdapter if you want to learn more.

demo.mp4

TaskTree

Create a custom adapter by extends TreeView.Adapter.

class TaskTreeAdapter extends TreeView.Adapter<TaskTreeAdapter.ViewHolder, String> {

  @Override
  public ViewHolder onCreateViewHolder(View view) {
    return new ViewHolder(view);
  }

  @Override
  public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
    super.onBindViewHolder(holder, position);
    holder.name.setText(items.get(position).getValue());
  }

  @Override
  public int getLayoutRes() {
    return R.layout.item_tasktree;
  }

  static class ViewHolder extends TreeView.ViewHolder {
    TextView name;

    public ViewHolder(@NonNull View itemView) {
      super(itemView);
      name = itemView.findViewById(R.id.name);
    }
  }
}

item_tasktree.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center_vertical"
    android:orientation="horizontal">

    <TextView
        android:id="@+id/name"
        android:layout_marginLeft="4dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</LinearLayout>

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <io.github.ikws4.treeview.TreeView
        android:id="@+id/tree_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</LinearLayout>

Bind TreeView with your custom adapter by calling treeView.setAdapter()

TreeView treeView = findViewById(R.id.tree_view);
TaskTreeAdapter adapter = new TaskTreeAdapter();
treeView.setAdapter(adapter);

TreeItem<String> root = new TreeItem<>("Tasks", true);
TreeItem<String> daily = new TreeItem<>("Daily", true);
TreeItem<String> weekly = new TreeItem<>("Weekly", true);

daily.getChildren().add(new TreeItem<>("Wake up at 6:00"));
daily.getChildren().add(new TreeItem<>("Drink at least 4 cups of water"));

weekly.getChildren().add(new TreeItem<>("Week review"));
weekly.getChildren().add(new TreeItem<>("Reward your self"));

root.setExpanded(true);
root.getChildren().add(daily);
root.getChildren().add(weekly);

adapter.setRoot(root, true);

Thanks

This library was inspired by RecyclerTreeView

License

MIT License

Copyright (c) 2022 ikws4

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

About

A general TreeView implementation for android base on RecyclerView.

License:MIT License


Languages

Language:Java 100.0%