• Stars
    star
    638
  • Rank 70,537 (Top 2 %)
  • Language
    Java
  • License
    Apache License 2.0
  • Created about 8 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

TreeView implement in Android with RecyclerView.

RecyclerTreeView


TreeView implement in Android with RecyclerView..

Effect

Usage

Setup

root build.gradle

allprojects {
    repositories {
        jcenter()
        maven { url "https://jitpack.io" }
    }
}

app build.gradle

dependencies {
    compile 'com.github.TellH:RecyclerTreeView:1.2.0'
}

Quick Start

  • Create a Java bean class and implement the LayoutItemType, to attach item layout id to it.
public class Dir implements LayoutItemType {
    public String dirName;

    public Dir(String dirName) {
        this.dirName = dirName;
    }

    @Override
    public int getLayoutId() {
        return R.layout.item_dir;
    }
}
  • Create a ViewBinder to bind view with the data bean. As you see, provideViewHolder(View itemView) corresponds for onCreateViewHolder in RecyclerView, and bindView corresponds for onBindViewHolder in RecyclerView.
public class FileNodeBinder extends TreeViewBinder<FileNodeBinder.ViewHolder> {
    @Override
    public ViewHolder provideViewHolder(View itemView) {
        return new ViewHolder(itemView);
    }

    @Override
    public void bindView(ViewHolder holder, int position, TreeNode node) {
        File fileNode = (File) node.getContent();
        holder.tvName.setText(fileNode.fileName);
    }

    @Override
    public int getLayoutId() {
        return R.layout.item_file;
    }

    public class ViewHolder extends TreeViewBinder.ViewHolder {
        public TextView tvName;

        public ViewHolder(View rootView) {
            super(rootView);
            this.tvName = (TextView) rootView.findViewById(R.id.tv_name);
        }

    }
}
  • Add TreeNode to TreeViewAdapter.
        List<TreeNode> nodes = new ArrayList<>();
        TreeNode<Dir> app = new TreeNode<>(new Dir("app"));
        nodes.add(app);
        app.addChild(
                new TreeNode<>(new Dir("manifests"))
                        .addChild(new TreeNode<>(new File("AndroidManifest.xml")))
        );
        app.addChild(
                new TreeNode<>(new Dir("java")).addChild(
                        new TreeNode<>(new Dir("tellh")).addChild(
                                new TreeNode<>(new Dir("com")).addChild(
                                        new TreeNode<>(new Dir("recyclertreeview"))
                                                .addChild(new TreeNode<>(new File("Dir")))
                                                .addChild(new TreeNode<>(new File("DirectoryNodeBinder")))
                                                .addChild(new TreeNode<>(new File("File")))
                                                .addChild(new TreeNode<>(new File("FileNodeBinder")))
                                                .addChild(new TreeNode<>(new File("TreeViewBinder")))
                                )
                        )
                )
        );
        TreeNode<Dir> res = new TreeNode<>(new Dir("res"));
        nodes.add(res);
        res.addChild(
                new TreeNode<>(new Dir("layout"))
                        .addChild(new TreeNode<>(new File("activity_main.xml")))
                        .addChild(new TreeNode<>(new File("item_dir.xml")))
                        .addChild(new TreeNode<>(new File("item_file.xml")))
        );
        res.addChild(
                new TreeNode<>(new Dir("mipmap"))
                        .addChild(new TreeNode<>(new File("ic_launcher.png")))
        );
        TreeViewAdapter adapter = new TreeViewAdapter(nodes, Arrays.asList(new FileNodeBinder(), new DirectoryNodeBinder()));
        rv.setAdapter(adapter);
  • set TreeNodeListener to TreeViewAdapter
        adapter.setOnTreeNodeListener(new TreeViewAdapter.OnTreeNodeListener() {
            @Override
            public boolean onClick(TreeNode node, RecyclerView.ViewHolder holder) {
                if (!node.isLeaf()) {
                    //Update and toggle the node.
                    onToggle(!node.isExpand(), holder);
                }
                return false;
            }

            @Override
            public void onToggle(boolean isExpand, RecyclerView.ViewHolder holder) {
                DirectoryNodeBinder.ViewHolder dirViewHolder = (DirectoryNodeBinder.ViewHolder) holder;
                final ImageView ivArrow = dirViewHolder.getIvArrow();
                int rotateDegree = isExpand ? 90 : -90;
                ivArrow.animate().rotationBy(rotateDegree)
                        .start();
            }
        });

Please check out the Demo and source code for more information. If you have any question, feel free to raise an issue. Thanks a lot!

More Repositories

1

GitClub

An elegent Android Client for Github. 不仅仅是Github客户端,而且是一个发现优秀Github开源项目的app
Java
658
star
2

RecyclerStickyHeaderView

Sticky header view or suspending view for RecyclerView.
Java
347
star
3

Android_PullToRefreshLibrary_Collection

Android下拉刷新开源库集锦,提供源码用于学习和研究
Java
240
star
4

AutoGo

Ease your code, easy go!
Java
122
star
5

FragmentLazyLoading

仿微信的Viewpager-Fragment惰性加载,延迟加载,lazy-loading
Java
70
star
6

NoListAdapter

A more elegant and easy way to build an multifunctional adapter for ListView or RecyclerView in Android.
Java
37
star
7

RecyclerViewDemo

Demo for study RecyclerView
Java
36
star
8

RxJavaDemo

学习RxJava
Java
33
star
9

Interview-Note

This is my campus interview note for Android. 我的校招Android面试笔记
25
star
10

AccessInline

An Android gradle plugin to inline methods that start with the prefix 'access$' in bytecode.
Java
20
star
11

Android-Simple-Common-PullToRefreshLayout

This is a light-weight Android PullToRefreshLayout, can be applied to any scrollable view.
Java
20
star
12

FloatingActionButtonPro

Extend from FloatingActionButton for Android
Java
8
star
13

HuffmanExtract

哈夫曼压缩,对应博文:http://blog.csdn.net/TellH/article/details/51285750
C++
8
star
14

AndroidStudioTemplate

Generate Template Files automatically in your Android Studio project.
FreeMarker
4
star
15

AlgorithmStudy

算法Demo
Java
4
star
16

AndroidLibraryLatestVersion

Check out Latest Version of some common libraries in Android developing.
1
star
17

AndroidUnitTestPractice

Android单元测试实践
Java
1
star
18

LearningCompile

探索编译原理
HTML
1
star