| I. Overview of the problem |
Tree structure is a kind of organization data structure commonly used in development, and many platforms also provide corresponding control. In the Android platform, it is not convenient to manipulate the tree structure with a finger, and there is no tree-shaped structure control. However, in practical applications, it is unavoidable to encounter the situation of displaying hierarchical relational data, such as the display of organization structure, the display of documents catalogue, etc.
Based on this requirement, I refer to the breadcrumb navigation in the site and the ListView control in Android that implements the following effects:
In this way, you can use the Breadcrumb navigation section above to navigate to any node, and take advantage of the powerful data presentation capabilities of the ListView, while lazy loading of node data through the corresponding listener. The following steps are used:
| Ii. Steps of implementation |
1. This component supports any entity object, and the entity is mapped to a node in the component by means of annotations. Defines the folder entity object:
public class Filefolder { @TreeNodeID private String FolderID; @TreeNodeName private String folderName; @TreeNodeParentID private String parentfolder; Public Filefolder (String FolderID, String folderName, String parentfolder) { super (); This.folderid = FolderID; This.foldername = FolderName; This.parentfolder = ParentFolder; } Public Filefolder () { super (); }}
2, create a layout file, note to have a ListView
<relativelayout xmlns:android= "http://schemas.android.com/apk/res/android" android:layout_width= "Fill_ Parent " android:layout_height=" wrap_content "> <com.jredu.view.breadcrumbtreeview android:id= "@+id/mybreadcrumb" android:layout_width= "match_parent" android:layout_height= "wrap_content"/> <listview android:id= "@+id/mylist" android:layout_width= "match_parent" android:layout_height= " Wrap_content " android:layout_below=" @id/mybreadcrumb "/> </RelativeLayout>
3, for the ListView implementation adapter components, the primary adapter to inherit Breadcrumbtreeviewadapter, in this adapter will complete the conversion of the entity to the TreeNode node.
public class Foldertreeadapter extends Breadcrumbtreeviewadapter<filefolder > {public foldertreeadapter (Context mcontext, list<filefolder> treedata) {super (Mcontext, treedata); } @Override Public View Getconvertview (TreeNode node, view Convertview, ViewGroup parent) {Viewholder hol Der = null; if (Convertview = = null) {Convertview = This.mLayoutInflater.inflate (R.layout.tree_list_item, NULL); Holder = new Viewholder (); Holder.deptname = (TextView) convertview. Findviewbyid (R.id.treenode); Convertview.settag (holder); } else {holder = (Viewholder) convertview.gettag (); } holder.deptName.setText (Node.getnodename ()); return convertview; } public class Viewholder {public TextView deptname; }}
4. Bind the Ontreenodeclicklistener listener to the Breadcrumbtreeview component in activity, and this listener will be executed when you click item in the ListView or click Breadcrumb navigation. There is only one method in the listener that is Onnodeclick, and you can load the child node's data in this method.
public void Onnodeclick (TreeNode node, int position, Boolean isbreadcrumb)
First parameter: Clicked node
Second parameter: The index of the clicked node
Third three: nodes in the ListView or in the breadcrumbs when the node is clicked.
With the above steps, you can use the Breadcrumbtreeview component, this component is now only a preliminary implementation, there are some features are not completed and not optimized, the next step will continue to refine the changes.
To continue to understand the students, you can download the source code to see more sharing content! If you have any questions, you can comment on the message.
Customizing the use of Component-breadcrumbtreeview