Android ApiDemos example resolution (112): Views-& gt; Expandable Lists-& am

Source: Internet
Author: User

Previous: http://www.bkjia.com/kf/201208/146527.html

The difference between Expandable List and normal List is that Expandable List can display two levels. The first layer is called "Group" and the second layer is "Child ". Here, "Child" can be folded or expanded, a bit like only two levels of TreeView (file browser ).

The usage is similar to that of List. The following ing can help you understand the Expandable List

ExpandableListActivity-> ListActivity
ExpandableListView-> ListView
ExpandableListAdapter-> ListAdapter
The default Layout of ExpandableListActivity is a list (two levels) displayed in the center full screen ). Android can also use custom Layout to display the Expandable List. To use custom Layout, an ExpandableListView object must be included in XML and the id must be @ android: id/list. You can also define a View for the empty list. The View id is @ id/android: empty.

The following example defines a Layout for ExpandableListActivity and displays "No Data" when the list is empty. You can use setContentView in onCreate to set this Layout.

<? Xml version = "1.0" encoding = "UTF-8"?>
<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Android: orientation = "vertical"
Android: layout_width = "match_parent"
Android: layout_height = "match_parent"
Android: paddingLeft = "8dp"
Android: paddingRight = "8dp">
<ExpandableListView android: id = "@ id/android: list"
Android: layout_width = "match_parent"
Android: layout_height = "match_parent"
Android: background = "#00FF00 ″
Android: layout_weight = "1 ″
Android: drawselectid Top = "false"/>

<TextView android: id = "@ id/android: empty"
Android: layout_width = "match_parent"
Android: layout_height = "match_parent"
Android: background = "# FF0000 ″
Android: text = "No data"/>
</LinearLayout>

ExpandableListActivity calls setListAdapter (ExpandableListAdapter) to set ExpandableListAdapter. ExpandableListAdapter allows you to set views for "group" and "child" respectively.

For example, in this example, MyExpandableListAdapter is derived from BaseExpandableListAdapter, and getChildView and getGroupView are overloaded to define a TextView for group and child respectively. The TextView text is the Group name or the Child name, corresponding to groups and children respectively.

[Java]
// Sample data set. children [I] contains
// The children (String []) for groups [I].
Private String [] groups = {"People Names", "Dog Names ",
"Cat Names", "Fish Names "};
Private String [] [] children = {
{"Arnold", "Barry", "Chuck", "David "},
{"Ace", "Bandit", "Cha-Cha", "Deuce "},
{"Fluffy", "Snuggles "},
{"Goldy", "Bubbles "}
};
 
Public View getChildView (int groupPosition, int childPosition, boolean isLastChild,
View convertView, ViewGroup parent ){
TextView textView = getGenericView ();
TextView. setText (getChild (groupPosition, childPosition). toString ());
Return textView;
}
 
Public View getGroupView (int groupPosition, boolean isExpanded, View convertView,
ViewGroup parent ){
TextView textView = getGenericView ();
TextView. setText (getGroup (groupPosition). toString ());
Return textView;
}

// Sample data set. children [I] contains
// The children (String []) for groups [I].
Private String [] groups = {"People Names", "Dog Names ",
"Cat Names", "Fish Names "};
Private String [] [] children = {
{"Arnold", "Barry", "Chuck", "David "},
{"Ace", "Bandit", "Cha-Cha", "Deuce "},
{"Fluffy", "Snuggles "},
{"Goldy", "Bubbles "}
};

Public View getChildView (int groupPosition, int childPosition, boolean isLastChild,
View convertView, ViewGroup parent ){
TextView textView = getGenericView ();
TextView. setText (getChild (groupPosition, childPosition). toString ());
Return textView;
}

Public View getGroupView (int groupPosition, boolean isExpanded, View convertView,
ViewGroup parent ){
TextView textView = getGenericView ();
TextView. setText (getGroup (groupPosition). toString ());
Return textView;
}

GetExpandableListView () obtains the ExpandableListView corresponding to the current Activity, and then calls registerForContextMenu to add Context Menu for ExpandableListView.

In the onContextItemSelected event, add an event for each List Item in list View. In this example, Toast is used to display a line of text and the currently clicked event.

[Java]
Public boolean onContextItemSelected (MenuItem item ){
ExpandableListContextMenuInfo info
= (ExpandableListContextMenuInfo) item. getMenuInfo ();
 
String title = (TextView) info.tar getView). getText (). toString ();
 
Int type = ExpandableListView. getPackedPositionType (info. packedPosition );
If (type = ExpandableListView. PACKED_POSITION_TYPE_CHILD ){
Int groupPos
= ExpandableListView. getPackedPositionGroup (info. packedPosition );
Int childPos
= ExpandableListView. getPackedPositionChild (info. packedPosition );
Toast. makeText (this, title + ": Child" + childPos
+ "Clicked in group" + groupPos,
Toast. LENGTH_SHORT). show ();
Return true;
} Else if (type = ExpandableListView. PACKED_POSITION_TYPE_GROUP ){
Int groupPos
= ExpandableListView. getPackedPositionGroup (info. packedPosition );
Toast. makeText (this, title + ": Group" + groupPos
+ "Clicked", Toast. LENGTH_SHORT). show ();
Return true;
}
 
Return false;
}

Public boolean onContextItemSelected (MenuItem item ){
ExpandableListContextMenuInfo info
= (ExpandableListContextMenuInfo) item. getMenuInfo ();

String title = (TextView) info.tar getView). getText (). toString ();

Int type = ExpandableListView. getPackedPositionType (info. packedPosition );
If (type = ExpandableListView. PACKED_POSITION_TYPE_CHILD ){
Int groupPos
= ExpandableListView. getPackedPositionGroup (info. packedPosition );
Int childPos
= ExpandableListView. getPackedPositionChild (info. packedPosition );
Toast. makeText (this, title + ": Child" + childPos
+ "Clicked in group" + groupPos,
Toast. LENGTH_SHORT). show ();
Return true;
} Else if (type = ExpandableListView. PACKED_POSITION_TYPE_GROUP ){
Int groupPos
= ExpandableListView. getPackedPositionGroup (info. packedPosition );
Toast. makeText (this, title + ": Group" + groupPos
+ "Clicked", Toast. LENGTH_SHORT). show ();
Return true;
}

Return false;
}

 
Author: mapdigit
 


 

 
 

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.