Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use this library #2

Open
polo2169 opened this issue Jun 14, 2022 · 7 comments
Open

Use this library #2

polo2169 opened this issue Jun 14, 2022 · 7 comments
Labels
question Further information is requested

Comments

@polo2169
Copy link

Hi,
I'm a beginner in java and I would like to use this library but I don't understand how to use it. The example is half explained, for example I don't understand what are the parent and child layouts.
If you have just a working example it would help me a lot!
Thanks

@AmrDeveloper
Copy link
Owner

Hello @polo2169,

First welcome to java and TreeView😄

I don't understand what are the parent and child layouts.

The tree is represented as roots and subnodes for example

- directory 1
  - file 1
  - directory 2
    - file 2 

Directory 1 node is a parent and has 2 children (file 1, directory 2) and directory 2 is also a parent of one child who is file 2, so to create this in java after creating 4 nodes you need to set the relation between them, so add file 2 node as a child in directory 2, and then add file 1 and directory 2 as children in directory 2

TreeNode directory1 = new TreeNode("Directory 1", R.layout.directory);
TreeNode directory2 = new TreeNode("Directory 2", R.layout.directory);
TreeNode file1 = new TreeNode("File 1", R.layout.file);
TreeNode file2 = new TreeNode("File 2", R.layout.file);

directory2.addChild(file2);
directory1.addChild(file1);
directory1.addChild(directory2);

This is how to use TreeView basically, if you have any problem with any example we can discuss it.

Thanks
Amr Hesham

@AmrDeveloper AmrDeveloper added the question Further information is requested label Jun 14, 2022
@polo2169
Copy link
Author

Hi @AmrDeveloper,
Thanks for your answer, unfortunately it doesn't work any better.
The editor says "add String to first parameter to the constructor".
I think I defined some things wrong but I followed the example.
image

@AmrDeveloper
Copy link
Owner

It weird @polo2169,

Make sure you used this import

import com.amrdeveloper.treeview.TreeNode;

Can you please write the error message in this line

treeViewAdapter.updateTreeNodes(roots);

@polo2169
Copy link
Author

polo2169 commented Jun 16, 2022

you are right I had the wrong import :/
I don't have any more errors! However, nothing is displayed during my test...
I don't understand what I have to put in the layout R.layout.list_item_one
I'm sorry I don't understand ahah

package fr.pdego.treeview2;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.RecyclerView;


import android.os.Bundle;

import com.amrdeveloper.treeview.TreeNode;
import com.amrdeveloper.treeview.TreeViewAdapter;
import com.amrdeveloper.treeview.TreeViewHolderFactory;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
RecyclerView recyclerView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        recyclerView=findViewById(R.id.recycler_view);
        TreeViewHolderFactory factory = (v, layout) -> {
            if (layout == R.layout.directory) return new CustomViewHolderOne(v);
            else if (layout == R.layout.file) return new CustomViewHolderTwo(v);
            else return new CustomViewHolderThree(v);
        };
        TreeViewAdapter treeViewAdapter = new TreeViewAdapter(factory);
        recyclerView.setAdapter(treeViewAdapter);
        TreeNode root1 = new TreeNode("Root1", R.layout.directory);
        root1.addChild(new TreeNode("Child1", R.layout.file));
        root1.addChild(new TreeNode("Child2", R.layout.file));

        List<TreeNode> roots = new ArrayList<>();
        roots.add(root1);

        treeViewAdapter.updateTreeNodes(roots);
    }
}

My view Holder

package fr.pdego.treeview2;

import android.view.View;

import androidx.annotation.NonNull;

import com.amrdeveloper.treeview.TreeNode;
import com.amrdeveloper.treeview.TreeViewHolder;

public class CustomViewHolderOne extends TreeViewHolder {

    public CustomViewHolderOne(@NonNull View itemView) {
        super(itemView);
    }

    @Override
    public void bindTreeNode(TreeNode node) {
        super.bindTreeNode(node);
        // Here you can bind your node and check if it selected or not
    }
}

My layouts

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_centerVertical="true"
    android:paddingLeft="10dp"
    android:paddingRight="10dp"
    android:paddingTop="10dp"
    android:paddingBottom="10dp">

    <ImageView
        android:id="@+id/image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_folder" />

    <TextView
        android:id="@+id/text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:layout_toRightOf="@id/image"
        android:text="List Item"
        android:textAppearance="@style/Base.TextAppearance.AppCompat.Small"
        android:textStyle="bold" />
</RelativeLayout>

Thank you for all your help!

@AmrDeveloper
Copy link
Owner

AmrDeveloper commented Jun 16, 2022

First of all roots should add to the roots list so if you want to show Directories 1 and 2 add them to the list

roots.add(directory1);
roots.add(directory2);

Then in your view holder, you should update the text view to show the value for example, think of it as an adapter

package fr.pdego.treeview2;

import android.view.View;

import androidx.annotation.NonNull;

import com.amrdeveloper.treeview.TreeNode;
import com.amrdeveloper.treeview.TreeViewHolder;

public class CustomViewHolderOne extends TreeViewHolder {

    private TextView text;

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

    @Override
    public void bindTreeNode(TreeNode node) {
        super.bindTreeNode(node);
        text.setText(node.getValue().toString());
    }
}

After your first example work well, I suggest to run the example app and play with it to see the features, and if you have any question please feel free to ask

@polo2169
Copy link
Author

I did it! Thank you so much for your help and for this really nice project!

@AmrDeveloper
Copy link
Owner

@polo2169 You're most welcome :D, enjoy using it

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants