Tree control developed by Android: ExpandableListView (1) and expandablelistview
This example is very simple. It is easy for beginners to develop it at will. Today's goal is only to achieve the effect. If you want to explore it, there are several very good blogs:
Android ExpandableListView special attributes
Http://blog.csdn.net/t12x3456/article/details/7828620
ExpandableListView
Http://www.apkbus.com/thread-124715-1-1.html
Like using listview, We need to inherit an adapter called BaseExpandListAdapter here, and then rewrite several methods of the parent class.
Package com. example. uitest;
Import android. content. Context;
Import android. view. LayoutInflater;
Import android. view. View;
Import android. view. ViewGroup;
Import android. widget. BaseExpandableListAdapter;
Public class TreeAdapter extends BaseExpandableListAdapter {
Context context;
Public TreeAdapter (Context context ){
Super ();
This. context = context;
}
@ Override
Public Object getChild (int arg0, int arg1 ){
Return 0;
}
@ Override
Public long getChildId (int arg0, int arg1 ){
// TODO Auto-generated method stub
Return 0;
}
@ Override
Public View getChildView (int arg0, int arg1, boolean arg2, View v,
ViewGroup arg4 ){
V = LayoutInflater. from (context). inflate (R. layout. item2, null );
Return v;
}
@ Override
Public int getChildrenCount (int arg0 ){
// TODO Auto-generated method stub
Return 3;
}
@ Override
Public Object getGroup (int arg0 ){
// TODO Auto-generated method stub
Return null;
}
@ Override
Public int getGroupCount (){
Return 3;
}
@ Override
Public long getGroupId (int arg0 ){
Return 0;
}
@ Override
Public View getGroupView (int arg0, boolean arg1, View v, ViewGroup vgs ){
V = LayoutInflater. from (context). inflate (R. layout. item_autocomplete, null );
Return v;
}
@ Override
Public boolean hasStableIds (){
// TODO Auto-generated method stub
Return false;
}
@ Override
Public boolean isChildSelectable (int arg0, int arg1 ){
// TODO Auto-generated method stub
Return false;
}
}
Here we only operate four functions:
Public int getChildrenCount (int arg0); // obtain the total number of rows in the sublayout of the list.
Public int getGroupCount (); // gets the number of rows in the first-level list
Public View getChildView (int arg0, int arg1, boolean arg2, View v,
ViewGroup arg4); // obtain the resource layout of the level-2 list
Public View getGroupView (int arg0, boolean arg1, View v, ViewGroup vgs); // gets the number of rows in the first-level list
Finally, load the adapter to ExpandableListView:
ExpandableListView view = (ExpandableListView) findViewById (R. id. elv );
TreeAdapter adapter = new TreeAdapter (getApplicationContext ());
View. setAdapter (adapter );
Main layout xml:
<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Xmlns: tools = "http://schemas.android.com/tools"
Android: layout_width = "match_parent"
Android: layout_height = "match_parent">
<ExpandableListView
Android: id = "@ + id/elv"
Android: layout_width = "fill_parent"
Android: groupIndicator = "@ null"
Android: layout_height = "wrap_content"> </ExpandableListView>
</LinearLayout>
This is the most concentrated version. You should be able to understand it soon;