Android TreeView Effect Implementation Method (with demo source download) _android

Source: Internet
Author: User

An example of this article describes the Android TreeView effect implementation method. Share to everyone for your reference, specific as follows:

It should be said that many operating systems provide a TreeView space, the implementation of the tree structure, the tree structure of the application is very wide, and Google developed Android for the Android phone is to touch the screen, the finger to operate the tree structure is very inconvenient, In this regard, there is no room for the TreeView, but only a two-level tree widget in the expandablelistview:android, which can be used for a number of functional requirements, such as the function of bookmarks. However, with the increasing application of this can not meet the requirements, such as the PDF of the left side of the function of the directory, you need a tree structure to achieve. The following application is based on the directory structure on the left side of the PDF, which is first pasted with the effect chart:

This idea is: according to similar to the "tree" structure of the organization of data, the interface directly using the ListView we often use to achieve. The following is a class of node information:

public class Pdfoutlineelement {
  private string id;//The ID of the current node
  private string Outlinetitle;//node information
  displayed above Private Boolean mhasparent; Whether there is a parent node
  private Boolean Mhaschild//whether there is a child node
  private String parent;//The ID of the parent node
  private int level;// Current node's hierarchy
}

I want to explain this level here, is very critical, he said the current node is the level, do not underestimate him, it is he helped us in the interface to show the level of effect, in fact, this tree is not a tree structure, all the data are maintained in a ArrayList inside, A Node A has child node B, C, in fact a, B, C these three nodes in the ArrayList is stored sequentially, if a level is levels, the level of his child nodes is level+1, we in GetView () when we can not use the system to automatically draw us , but can specify his position holder.icon.setPadding (level + 1), Holder.icon.getPaddingTop (), 0, Holder.icon.getPaddingBottom ()) , so that his child nodes have an indentation effect.

There is also a key point is how to achieve the "expansion" and "retract" effect, in fact, this is only when you click to touch a node, if there is a child node under the node and is "retracted", all of his child nodes from the ArrayList inside remove the tune, and then Notifydatasetchanged (), the same is "expand" when, is after this node to add a child node to the ArrayList inside, and then notifydatasetchanged ().

In fact, we should know how to do this, this tree structure is not a real tree, but is a ListView, through the arrylist to add delete data control information, through setpadding () to achieve indentation

Understand this idea, I explain getview, we can certainly make

Public View GetView (int position, View Convertview, ViewGroup parent) {Viewholder holder;
    if (Convertview = = null) {Convertview = minflater.inflate (r.layout.outline, NULL);
    Holder = new Viewholder ();
    Holder.text = (TextView) Convertview.findviewbyid (R.id.text);
    Holder.icon = (ImageView) Convertview.findviewbyid (R.id.icon);
  Convertview.settag (holder);
  else {holder = (Viewholder) convertview.gettag (); int level = Mfilelist.get (position). Getlevel ();//each time the display is drawn according to the hierarchy of the node holder.icon.setPadding (level + 1), Holder.ic
  On. Getpaddingtop (), 0, Holder.icon.getPaddingBottom ());
  Holder.icon.setVisibility (view.visible);
       Holder.text.setText (Mfilelist.get (position). Getoutlinetitle ()); If there is a child and the current is not expanded icon set to "+" icon if (mfilelist.get (position). Ismhaschild () && (Mfilelist.get (position). Isexpa
  nded () = = False)) {Holder.icon.setImageBitmap (miconcollapse); else if (mfilelist.get (position). Ismhaschild () && (mfilElist.get (position). isexpanded () = = True) {//If there is a child and the icon that is not currently expanded is set to the icon Holder.icon.setImageBitmap of "+" (MICONEXPA
  nd);
    else if (!mfilelist.get (position). Ismhaschild ()) {Holder.icon.setImageBitmap (miconcollapse); Holder.icon.setVisibility (view.invisible)//This is not set to gone, because gone does not display and does not occupy position, and invisible is not displayed but occupies the position of the} return
Convertview;

 }

Full instance code click here to download the site.

For more information on Android-related content readers can view the site topics: "Android Development Introduction and Advanced Course", "Android Multimedia operating skills Summary (audio, video, recording, etc.)", "Android Basic Components Usage Summary", " Android View tips Summary, Android layout layout tips and a summary of Android controls usage

I hope this article will help you with the Android program.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.