How To Make TreeView In Android (Expandable List View in Expandable List View)


Step1: Download AndroidTreeView.zip




Step2: Paste "atv,values and MyHolder.java" in your project.






Step3: Replace Package Name In all "atv" folder files.



Step4: Create your tree starting from root element.


TreeNode root = TreeNode.root();

 TreeNode parent = new TreeNode("MyParentNode");
 TreeNode child0 = new TreeNode("ChildNode0");
 TreeNode child1 = new TreeNode("ChildNode1");
 parent.addChildren(child0, child1);
 root.addChild(parent);


(Add tree view to layout)

AndroidTreeView tView = new AndroidTreeView(getActivity(), root);
 containerView.addView(tView.getView());



Step5: Custom View for nodes
MyHolder.java
package android.com.treeview;

import android.com.treeview.atv.model.TreeNode;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

/**
 * Created by admin on 31-03-2016.
 */
public class MyHolder extends TreeNode.BaseNodeViewHolder<MyHolder.IconTreeItem> {
    ImageView img;
    TextView tvValue;

    public MyHolder(Context context) {
        super(context);
    }

    @Override
    public View createNodeView(TreeNode node, IconTreeItem value) {
        final LayoutInflater inflater = LayoutInflater.from(context);
        final View view = inflater.inflate(R.layout.child/*your layout here*/, null, false);
        img = (ImageView) view.findViewById(R.id.image);
        tvValue = (TextView) view.findViewById(R.id.text);
        img.setImageResource(value.icon);
        tvValue.setText(value.text);
        return view;
    }
 //for toggle image on click////
    public void toggle(boolean active) {
        img.setImageResource(active ? R.drawable.add_wishlist : R.drawable.inquire_now);
    }

    public static class IconTreeItem {
        public int icon;
        public String text;

        public IconTreeItem(int icon, String text) {

            this.icon = icon;
            this.text = text;
        }
    }
}


Step6: Connect View Holder with node.

IconTreeItem nodeItem = new IconTreeItem();
  TreeNode child1 = new TreeNode(nodeItem).setViewHolder(new MyHolder(mContext));



Step6: onClick Working
TreeNode.setClickListener(new TreeNodeClickListener() {
                @Override
                public void onClick(TreeNode node, Object value) {
                    //Your Code Here//

                }
            });

(Other fuctions)
AndroidTreeView.setDefaultViewHolder
AndroidTreeView.setDefaultNodeClickListener

Share on Google Plus

About Mayank Sharma

Mayadi is a knowlage source. You can learn, explore and play many things in a proper way. Mayadi Provides Best, Perfect and Quality Information. subscribe our youtube channel to be updated and and don't forget to take a look on our blog within every week, because we are here to upgrade your knowladge. We love to see you again and again and again...
    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment