Android [elementary tutorial] Chapter 8 ExpandableListView Control

Source: Internet
Author: User

In this chapter, we will learn the ExpandableListView control. ExpandableListView is a list control that can be expanded. Or, for travel to the West, Tang Miao has different skills in different roles, it is definitely not suitable to display them together. Here we will use the ExpandableListView control to display them.

See main. xml

 
<? 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">
<TextView android: text = "the capabilities of the heroes of travel to the West" android: id = "@ + id/textView1"
Android: layout_width = "wrap_content" android: layout_height = "wrap_content"> </TextView>
<ExpandableListView android: layout_height = "wrap_content"
Android: id = "@ + id/expandableListView" android: layout_width = "match_parent"> </ExpandableListView>
<TextView android: layout_height = "wrap_content"
Android: layout_width = "fill_parent" android: text = "@ string/hello"
Android: id = "@ + id/text"> </TextView>
</LinearLayout>

As before, an ExpandableListView control is defined. The following is the java source code:

 
Import android. app. Activity;
Import android. OS. Bundle;
Import android. view. Gravity;
Import android. view. View;
Import android. view. ViewGroup;
Import android. widget. AbsListView;
Import android. widget. BaseExpandableListAdapter;
Import android. widget. ExpandableListAdapter;
Import android. widget. ExpandableListView;
Import android. widget. ImageView;
Import android. widget. LinearLayout;
Import android. widget. TextView;
 
Public class ButtonDemoActivity extends Activity
{
Private TextView text = null;
Private int [] image = {R. drawable. tangseng, R. drawable. wukong,
R. drawable. bajie, R. drawable. shaseng };
Private String [] item = {"Tang Seng", "Sun Wukong", "", "Sha Monk "};
Private String [] [] ability = {"you will read the curse", "you will say Amitabha "},
{"Will change to", "will beat the Fairy", "will fly the cloud and drive the fog"}, {"will be lazy", "will go to bed" },{ "will pick up the burden "}};
Private ExpandableListView explandListView;
 
/** Called when the activity is first created .*/
@ Override
Public void onCreate (Bundle savedInstanceState)
{
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );
 
// Find the TextView control in main. xml by ID
Text = (TextView) findViewById (R. id. text );
 
// Find the ExpandableListView control in main. xml by ID
ExplandListView = (ExpandableListView) findViewById (R. id. expandableListView );

// Set the ExpandableListView Adapter
ExpandableListAdapter adapter = new BaseExpandableListAdapter ()
{

// Process the Click Event of the subitem
@ Override
Public boolean isChildSelectable (int groupPosition,
Int childPosition)
{
String str = item [groupPosition]
+ Ability [groupPosition] [childPosition];
UpdateText (str );
Return true;
}
 
@ Override
Public boolean hasStableIds ()
{
Return true;
}

// Return the view control of the Parent Project
@ Override
Public View getGroupView (int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent)
{
// Create a linear Layout
LinearLayout ll = new LinearLayout (ButtonDemoActivity. this );
// Set the layout style to Horizontal.
Ll. setOrientation (0 );
// Set the left margin of the layout to 50 pixels
Ll. setPadding (50, 0, 0, 0 );
// Create an ImageView object
ImageView imageView = new ImageView (ButtonDemoActivity. this );
// Set the ID of the object to be displayed in ImageView
ImageView. setImageResource (image [groupPosition]);
// Add the ImageView to the linear Layout
Ll. addView (imageView );
// Use the custom text box
TextView textView = getTextView ();
// Set the display content in the text box
TextView. setText (getGroup (groupPosition). toString ());
// Add TextView to linear Layout
Ll. addView (textView );
Return ll;
}

// Returns the ID of the parent control.
@ Override
Public long getGroupId (int groupPosition)
{
Return groupPosition;
}

// Return the total number of parent controls
@ Override
Public int getGroupCount ()
{
 
Return ability. length;
} Www.2cto.com

// Obtain the parent control object
@ Override
Public Object getGroup (int groupPosition)
{
Return item [groupPosition];
}

// Obtain the number of child Controls
@ Override
Public int getChildrenCount (int groupPosition)
{
Return ability [groupPosition]. length;
}

// Obtain the child control View
@ Override
Public View getChildView (int groupPosition, int childPosition,
Boolean isLastChild, View convertView, ViewGroup parent)
{
// Use the custom TextView Control
TextView textView = getTextView ();
// Set the content of the custom TextView Control
TextView. setText (getChild (groupPosition, childPosition)
. ToString ());
Return textView;
}

// Obtain the child Control ID
@ Override
Public long getChildId (int groupPosition, int childPosition)
{
Return childPosition;
}

// Obtain the child control object
@ Override
Public Object getChild (int groupPosition, int childPosition)
{
Return ability [groupPosition] [childPosition];
}

// Custom text box
Public TextView getTextView ()
{
AbsListView. LayoutParams lp = new AbsListView. LayoutParams (
ViewGroup. LayoutParams. FILL_PARENT, 64 );
TextView textView = new TextView (ButtonDemoActivity. this );
TextView. setLayoutParams (lp );
TextView. setPadding (20, 0, 0, 0 );
// Set the TextView control to left and align horizontally.
TextView. setGravity (Gravity. CENTER_VERTICAL | Gravity. LEFT );
 
Return textView;
 
}
};
 
ExplandListView. setAdapter (adapter );
 
 
}
 
Private void updateText (String string)
{
// Set the text information to display in the TextView control.
Text. setText (string );
}
 
}
 

 

 

In fact, the ExpandableListView control is more convenient to use, as long as you have learned how to use BaseExpandableListAdapter (). Of course, the layout here is generated in the Code Section, for convenience of expansion, you can also use Layout. xml layout file to generate. after class, do you think about it? I mainly use LayoutInflater. All right. This lesson is over. Thank you.

 

From: kangkangz4 Column

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.