Self-implemented Treeview on Android

Source: Internet
Author: User

After studying the results of the Treeview for a while, I 'd like to share this with you and hope to give some good suggestions. This idea is: organize data according to the structure similar to the "Tree", and implement the listview directly on the interface. The following is a class of node information:
Public class pdfoutlineelement {
Private string ID; // the ID of the current node
Private string outlinetitle; // information displayed on the node
Private Boolean mhasparent; // whether the parent node exists
Private Boolean mhaschild; // specifies whether a child node exists.
Private string parent; // the ID of the parent node
Private int level; // the level of the current node
}
Here I want to explain that this level is very important. It indicates the level of the current node. Don't underestimate it, he helped us to display the hierarchical effect on the interface. In fact, this tree structure is not a tree structure, and all data is maintained in an arraylist, A node A has subnodes B and C. In fact, the nodes A, B, and C are stored in the arraylist sequentially. If the level of A is level, the level of the child node is level + 1. When getview () is used, we can specify the position of the child node, instead of automatically drawing the child node to us. icon. setpadding (25 * (LEVEL + 1), Holder. icon. getpaddingtop (), 0, Holder. icon. getpaddingbottom (); in this way, the subnode is indented.
Another key point is how to achieve the effects of "expansion" and "contraction". In fact, this is only when you click a node, if there is a subnode under this node and it is "scaled back", delete all its subnodes from that arraylist, and then notifydatasetchanged (), similarly, when "Expand", add the child node to the arraylist after the node, and then notifydatasetchanged ().
As a matter of fact, we should know how to do this. This tree structure is not a real tree, but a listview. By adding and deleting data control information to arrylist, We Can indent it through setpadding ().
After understanding this idea, I will explain getview. You can definitely make it.

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) convertvi Ew. gettag ();} */INT level = mfilelist. get (position ). getlevel (); // each time the position displayed is drawn based on the hierarchy of the node holder. icon. setpadding (25 * (LEVEL + 1), Holder. icon. getpaddingtop (), 0, Holder. icon. getpaddingbottom (); holder. text. settext (mfilelist. get (position ). getoutlinetitle (); // if a child exists and the expanded icon is set as the "+" icon, if (mfilelist. get (position ). ismhaschild () & (mfilelist. get (position ). isexpanded () = false) {holder. icon. setimage Bitmap (miconcollapse);} else if (mfilelist. get (position ). ismhaschild () & (mfilelist. get (position ). isexpanded () = true) {// if a child exists and whether the expanded icon is set to the "+" icon currently. icon. setimagebitmap (miconexpand);} else if (! Mfilelist. get (position ). ismhaschild () {holder. icon. setimagebitmap (miconcollapse); holder. icon. setvisibility (view. invisible); // do not set it to gone here, because gone is not displayed and does not occupy the position, and invisible is not displayed but occupies the position} return convertview ;}

Please note that/* If (convertview = NULL) {*/comment out this. According to the usual practice, we create it for the first time and no need to create it later, however, this will cause problems because we dynamically add and delete data to and from arraylist. If setlistadapter is called after data is added and deleted, the data will be reloaded each time, in this way, no matter where you click it, you will return to the top. If the tree structure is short, you will not be able to see it. Therefore, notifydatasetchanged () is called every time the data is reloaded (), but there is also a problem, that is, the icons of "+" and "-" are displayed in disorder, but the icons are all unavailable for several times, because they are not re-inflate during each getview operation, so you can reload it every time.

After a friend gave me some advice, the above is not correct here.
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 ();
}

Holder. Icon. setvisibility (view. Visible );

Reference: http://www.apkbus.com/android-14030-1-1.html

Welcome to pay attention to Weibo: http://e.weibo.com/u/2975543812

2012 Android bus developer salon Chengdu station registration For details see: http://www.apkbus.com/android-72722-1-1.html

 

Related Article

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.