Improved the implementation of multi-level tree menus in 17 articles on Android

Source: Internet
Author: User

To implement tree menus in Android, you can use ExpandableList (or you can inherit ListView or LinearLayout by yourself). However, ExpandableList can only implement two levels of tree menus ...... this article still uses ExpandableList, but the three-level tree menu is to be implemented. Program running in this article:

 

When BaseExpandableListAdapter is used to implement a level-2 tree menu, both the parent item (getGroupView () and subitem (getChildView () Use TextView. To implement a three-level tree menu, the subitem (getChildView () must use ExpandableList ....... in addition, you need to define struct to facilitate the call of Level 3 tree data. The level 2 tree menu can be used as follows:

View plaincopy to clipboardprint?
Static public class TreeNode {
Object parent;
List <Object> childs = new ArrayList <Object> ();
}
Static public class TreeNode {
Object parent;
List <Object> childs = new ArrayList <Object> ();
}

The level-3 tree menu can be used as follows. The sub-item is the structure of the level-2 tree menu:

View plaincopy to clipboardprint?
Static public class SuperTreeNode {
Object parent;
// Structure of the second-level tree menu
List <TreeViewAdapter. TreeNode> childs = new ArrayList <TreeViewAdapter. TreeNode> ();
}
Static public class SuperTreeNode {
Object parent;
// Structure of the second-level tree menu
List <TreeViewAdapter. TreeNode> childs = new ArrayList <TreeViewAdapter. TreeNode> ();
}

Note the following two points when implementing a three-level tree menu:

1. Level 2 is also a tree menu. Therefore, you must set enough space for Level 2 project expansion/recycling to display Level 2 tree menus completely;

2. When a three-level tree menu is implemented, it is found that the menu method is useless (such as OnChildClickListener and OnGroupClickListener). Therefore, to obtain the selected data, a callback function must be defined externally, then, the external function is called back when the second-level tree menu is generated.

PS: when solving the key point of No. 2, this article can only obtain the sequence number selected in level 3... While Level 1 still cannot obtain the sequence number.

The source code of main. xml is as follows:

View plaincopy to clipboardprint?
<? Xml version = "1.0" encoding = "UTF-8"?>
<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Android: orientation = "vertical" android: layout_width = "fill_parent"
Android: layout_height = "fill_parent">
<LinearLayout android: id = "@ + id/LinearLayout01"
Android: layout_width = "wrap_content" android: layout_height = "wrap_content">
<Button android: layout_height = "wrap_content" android: text = "two-layer structure"
Android: layout_width = "160dip" android: id = "@ + id/btnNormal"> </Button>
<Button android: layout_height = "wrap_content" android: text = "three-tier structure"
Android: layout_width = "160dip" android: id = "@ + id/btnSuper"> </Button>
</LinearLayout>
<ExpandableListView android: id = "@ + id/ExpandableListView01"
Android: layout_width = "fill_parent" android: layout_height = "fill_parent"> </ExpandableListView>
</LinearLayout>
<? Xml version = "1.0" encoding = "UTF-8"?>
<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Android: orientation = "vertical" android: layout_width = "fill_parent"
Android: layout_height = "fill_parent">
<LinearLayout android: id = "@ + id/LinearLayout01"
Android: layout_width = "wrap_content" android: layout_height = "wrap_content">
<Button android: layout_height = "wrap_content" android: text = "two-layer structure"
Android: layout_width = "160dip" android: id = "@ + id/btnNormal"> </Button>
<Button android: layout_height = "wrap_content" android: text = "three-tier structure"
Android: layout_width = "160dip" android: id = "@ + id/btnSuper"> </Button>
</LinearLayout>
<ExpandableListView android: id = "@ + id/ExpandableListView01"
Android: layout_width = "fill_parent" android: layout_height = "fill_parent"> </ExpandableListView>
</LinearLayout>
 

TestExpandableList. java is the main class that calls other tool classes. The source code is as follows:

View plaincopy to clipboardprint?
Package com. testExpandableList;


Import java. util. List;
Import android. app. Activity;
Import android. OS. Bundle;
Import android. util. Log;
Import android. view. View;
Import android. widget. Button;
Import android. widget. ExpandableListView;
Import android. widget. ExpandableListView. OnChildClickListener;
Import android. widget. Toast;

Public class testExpandableList extends Activity {
/** Called when the activity is first created .*/
ExpandableListView expandableList;
TreeViewAdapter adapter;
SuperTreeViewAdapter superAdapter;
Button btnNormal, btnSuper;
// Sample data set. children [I] contains the children (String []) for groups [I].
Public String [] groups = {"xxxx friends", "xxxx classmates", "xxxxx women "};
Public String [] [] child = {
{"A Jun", "B Jun", "C jun", "D Jun "},
{"Classmate A", "Classmate B", "Classmate C "},
{"Yujie", "Lori "}
};

Public String [] parent = {"xxxx friends", "xxxx students "};
Public String [] [] [] child_grandson = {
{"A Jun "},
{"AA", "AAA "}},
{"B Jun "},
{"BBB", "BBBB", "BBBBB "}},
{"C jun "},
{"CCC", "CCCC "}},
{"D Jun "},
{"DDD", "DDDD", "DDDDD "}},
};

@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );

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.