bmelnychuk / AndroidTreeView

AndroidTreeView. TreeView implementation for android
Apache License 2.0
3k stars 620 forks source link

viewHolder display problem #58

Open Eisaltar opened 8 years ago

Eisaltar commented 8 years ago

Hello, what i want to do is a treeView in wich each item has an imageView and a TextView. The problem is that when i want to put the treeView inside the container (FrameLayout) by using this command: menuContainer.addView(treeView.getView()); I have this error: "The specified child already has a parent. You must call removeView() on the child's parent first." So i try like this : menuContainer.addView(treeMenuAdapter.getView()); treeMenuAdapter is my custom viewHolder, But only the last element is display.

here is all the code : CreatingNode and Tree : `public void GeometrieGraphicTool(){ FrameLayout menuContainer = (FrameLayout) this.findViewById(R.id.toolContainer);

    TreeNode root = TreeNode.root();
    TreeMenuAdapter treeMenuAdapter = new TreeMenuAdapter(this);
    TreeMenuItem nodeItem = new TreeMenuItem();

    //création des noeuds
    nodeItem.text=getString(R.string.geometrie);
    TreeNode geometrieNode = new TreeNode(nodeItem).setViewHolder(treeMenuAdapter);
    nodeItem.icon = R.drawable.tracer;
    nodeItem.text = getString(R.string.tracer);
    TreeNode tracerNode = new TreeNode(nodeItem).setViewHolder(treeMenuAdapter);
    nodeItem.icon=R.drawable.droite;
    nodeItem.text=getString(R.string.droite);
    TreeNode droiteNode = new TreeNode(nodeItem).setViewHolder(treeMenuAdapter);
    nodeItem.icon=R.drawable.segment;
    nodeItem.text= getString(R.string.segment);
    TreeNode segmentNode =new TreeNode(nodeItem).setViewHolder(treeMenuAdapter);
    nodeItem.icon= R.drawable.angle;
    nodeItem.text=getString(R.string.angle);
    TreeNode angleNode = new TreeNode(nodeItem).setViewHolder(treeMenuAdapter);
    nodeItem.icon=R.drawable.cercle;
    nodeItem.text = getString(R.string.cercle);
    TreeNode cercleNode =new TreeNode(nodeItem).setViewHolder(treeMenuAdapter);
    nodeItem.text = getString(R.string.outil);
    TreeNode outilNode =new TreeNode(nodeItem).setViewHolder(treeMenuAdapter);
    nodeItem.icon=R.drawable.equerre;
    nodeItem.text=getString(R.string.equerre);
    TreeNode equerreNode =new TreeNode(nodeItem).setViewHolder(treeMenuAdapter);
    nodeItem.icon=R.drawable.compas;
    nodeItem.text=getString(R.string.compas);
    TreeNode compasNode=new TreeNode(nodeItem).setViewHolder(treeMenuAdapter);
    nodeItem.icon =R.drawable.polygone;
    nodeItem.text = getString(R.string.polygone);
    TreeNode polygoneNode =new TreeNode(nodeItem).setViewHolder(treeMenuAdapter);
    nodeItem.icon=R.drawable.triangle;
    nodeItem.text=getString(R.string.triangle);
    TreeNode triangleNode=new TreeNode(nodeItem).setViewHolder(treeMenuAdapter);
    nodeItem.icon=R.drawable.rectangle;
    nodeItem.text=getString(R.string.quadrilatère);
    TreeNode quadrilatereNode =new TreeNode(nodeItem).setViewHolder(treeMenuAdapter);
    nodeItem.text =getString(R.string.autre);
    TreeNode autreNode =new TreeNode(nodeItem).setViewHolder(treeMenuAdapter);
    nodeItem.text = getString(R.string.symetrie);
    TreeNode symetrieNode=new TreeNode(nodeItem).setViewHolder(treeMenuAdapter);
    nodeItem.icon= R.drawable.translation;
    nodeItem.text= getString(R.string.translation);
    TreeNode translationNode = new TreeNode(nodeItem).setViewHolder(treeMenuAdapter);
    nodeItem.icon=R.drawable.symetrie_axial;
    nodeItem.text=getString(R.string.axial);
    TreeNode axialNode = new TreeNode(nodeItem).setViewHolder(treeMenuAdapter);
    nodeItem.icon = R.drawable.rotation;
    nodeItem.text = getString(R.string.rotation);
    TreeNode rotationNode = new TreeNode(nodeItem).setViewHolder(treeMenuAdapter);
    nodeItem.icon= R.drawable.rotation_point;
    nodeItem.text=getString(R.string.autourPoint);
    TreeNode autourPointNode = new TreeNode(nodeItem).setViewHolder(treeMenuAdapter);
    nodeItem.icon=R.drawable.rotation_sommet;
    nodeItem.text=getString(R.string.autourSommet);
    TreeNode autourSommetNode = new TreeNode(nodeItem).setViewHolder(treeMenuAdapter);
    nodeItem.icon=R.drawable.graphic;
    nodeItem.text=getString(R.string.graphique);
    TreeNode graphicNode = new TreeNode(nodeItem).setViewHolder(treeMenuAdapter);
    nodeItem.icon= R.drawable.remplissage;
    nodeItem.text=getString(R.string.remplissage);
    TreeNode remplissageNode = new TreeNode(nodeItem).setViewHolder(treeMenuAdapter);
    nodeItem.text = getString(R.string.creationGraphique);
    TreeNode creationGraphiqueNode = new TreeNode(nodeItem).setViewHolder(treeMenuAdapter);
    nodeItem.icon=R.drawable.graphique_courbe;
    nodeItem.text = getString(R.string.courbe);
    TreeNode graphicCourbeNode = new TreeNode(nodeItem).setViewHolder(treeMenuAdapter);
    nodeItem.icon = R.drawable.histogramme;
    nodeItem.text = getString(R.string.histogramme);
    TreeNode graphicHistogrammeNode = new TreeNode(nodeItem).setViewHolder(treeMenuAdapter);
    nodeItem.icon = R.drawable.graphique_secteur;
    nodeItem.text= getString(R.string.secteur);
    TreeNode secteurGraphiqueNode = new TreeNode(nodeItem).setViewHolder(treeMenuAdapter);

    //création de l'arborescence
    root.addChildren(geometrieNode, graphicNode);
    geometrieNode.addChildren(tracerNode,symetrieNode,remplissageNode);
    graphicNode.addChildren(remplissageNode, creationGraphiqueNode);
    tracerNode.addChildren(droiteNode, segmentNode, angleNode, cercleNode, outilNode, polygoneNode);
    symetrieNode.addChildren(translationNode, axialNode, rotationNode);
    creationGraphiqueNode.addChildren(graphicCourbeNode, graphicHistogrammeNode, secteurGraphiqueNode);
    outilNode.addChildren(equerreNode, compasNode);
    polygoneNode.addChildren(triangleNode, quadrilatereNode, autreNode);
    rotationNode.addChildren(autourPointNode, autourSommetNode);

    AndroidTreeView treeView = new AndroidTreeView(this,root);
    treeView.setDefaultAnimation(true);
    menuContainer.addView(treeMenuAdapter.getView());
}`

and here is my custom holder: `public class TreeMenuAdapter extends TreeNode.BaseNodeViewHolder{ private Context context;

public TreeMenuAdapter(Context context) {
    super(context);
    this.context = context;

}

@Override
public View createNodeView(TreeNode node, TreeMenuItem value) {
    final LayoutInflater inflater = LayoutInflater.from(context);
    final View view = inflater.inflate(R.layout.tree_menu_item, null, false);

    ImageView imageView = (ImageView)view.findViewById(R.id.TreeItemImage);
    TextView textView = (TextView) view.findViewById(R.id.TreeItemText);

    imageView.setImageResource(value.icon);
    textView.setText(value.text);

    return view;
}

}`

thank you for reading me and have a nice day.

ps: sorry if my English is bad I'm a Belgian student

nilesh211 commented 8 years ago

any update on this thread? I am facing the same issue

bmelnychuk commented 8 years ago

I think the problem is in your TreeMenuAdapter. ViewHolder instance should not be re-used for each node, you need to create new object every time, or setDefaultViewHolder(Class<? extends TreeNode.BaseNodeViewHolder> viewHolder) { to set default view holder for each node, and library will create objects for you

stay4cold commented 8 years ago

I'm facing the same issue. Have anyone fix it?

fukemy commented 5 years ago

doesn't work for me @bmelnychuk