Android control development-ExpandableListActivity (2): android custom control

Source: Internet
Author: User

Android control development-ExpandableListActivity (2): android custom control
Android control development-ExpandableListActivity (2)This blog post describes how to use ExpandableListActivity to create a friend list similar to QQ. Let's look at the implementation code.This instance contains an Activity and a master layout file. Other level-1 and level-2 directories are implemented by rewriting the getGroupView () and getChildView () methods in ExpandableListActivity.

The MainActivity. java code is as follows:This Activity is mainly an extends self-ListActivity class,
Package com. example. expandablelistactivity;


Import java. util. ArrayList;
Import java. util. HashMap;
Import android. OS. Bundle;
Import android. app. ListActivity;
Import android. database. DataSetObserver;
Import android. graphics. Color;
Import android. view. Gravity;
Import android. view. Menu;
Import android. view. View;
Import android. view. ViewGroup;
Import android. widget. AbsListView;
Import android. widget. ExpandableListAdapter;
Import android. widget. ExpandableListView;
Import android. widget. ImageView;
Import android. widget. LinearLayout;
Import android. widget. SimpleExpandableListAdapter;
Import android. widget. ExpandableListView. OnChildClickListener;
Import android. widget. TextView;

/*
It is mainly implemented by rewriting related methods in ExpandableListAdapter ().*/

Public class MainActivity extends ListActivity {


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

// Anonymous internal class, override the ExpandableListAdapter Class Method
Final ExpandableListAdapter ela = new ExpandableListAdapter (){
// Set the Group view image
Int [] logos = new int [] {R. drawable. ic_launcher, R. drawable. ic_launcher, R. drawable. ic_launcher };
// Set the display text of the Group view
Private String [] generalsTypes = new String [] {"group1", "group2 "};
// Display text in the subview
Private String [] [] generals = new String [] [] {
{"Child1data1", "child1data2", "child1data3 "},
{"Child2data1 "}


};
// Subview Image
Public int [] [] generallogos = new int [] [] {
{R. drawable. list1, R. drawable. list2,
R. drawable. list3 },
{R. drawable. list4 }};

// Define a method for obtaining text information
TextView getTextView (){
// Set the height and width of textView
AbsListView. LayoutParams lp = new AbsListView. LayoutParams (
ViewGroup. LayoutParams. FILL_PARENT, ViewGroup. LayoutParams. WRAP_CONTENT );
TextView textView = new TextView (
MainActivity. this );
TextView. setLayoutParams (lp );
TextView. setGravity (Gravity. CENTER_VERTICAL );
TextView. setPadding (10, 10, 10, 10 );
TextView. setTextSize (15 );
TextView. setTextColor (Color. BLACK );
Return textView;
}



// Override each method in the ExpandableListAdapter
@ Override
Public int getGroupCount (){
// TODO Auto-generated method stub
Return generalsTypes. length;
}


@ Override
Public Object getGroup (int groupPosition ){
// TODO Auto-generated method stub
Return generalsTypes [groupPosition];
}


@ Override
Public long getGroupId (int groupPosition ){
// TODO Auto-generated method stub
Return groupPosition;
}


@ Override
Public int getChildrenCount (int groupPosition ){
// TODO Auto-generated method stub
Return generals [groupPosition]. length;
}


@ Override
Public Object getChild (int groupPosition, int childPosition ){
// TODO Auto-generated method stub
Return generals [groupPosition] [childPosition];
}


@ Override
Public long getChildId (int groupPosition, int childPosition ){
// TODO Auto-generated method stub
Return childPosition;
}


@ Override
Public boolean hasStableIds (){
// TODO Auto-generated method stub
Return true;
}


@ Override
Public View getGroupView (int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent ){
// TODO Auto-generated method stub
// Create a LinearLayout View
LinearLayout ll = new LinearLayout (
MainActivity. this );
Ll. setOrientation (0 );

// Create an ImageView object
ImageView logo = new ImageView (MainActivity. this );
Logo. setImageResource (logos [groupPosition]);
Logo. setPadding (50, 0, 0, 0 );
// Add an ImageView in LinearLayout
Ll. addView (logo );

// Create a TextView object
TextView textView = getTextView ();
TextView. setTextColor (Color. BLACK );
TextView. setText (getGroup (groupPosition). toString ());
// Add TextView to LinearLayout
Ll. addView (textView );


Return ll;
}


@ Override
Public View getChildView (int groupPosition, int childPosition,
Boolean isLastChild, View convertView, ViewGroup parent ){
// TODO Auto-generated method stub
// Create a LinearLayout object
LinearLayout ll = new LinearLayout (
MainActivity. this );
Ll. setOrientation (0 );

// Create an ImageView object
ImageView generallogo = new ImageView (
MainActivity. this );
Generallogo. setImageResource (generallogos [groupPosition] [childPosition]);
Generallogo. setPadding (5, 5, 5, 5 );

// Add the imageView object to the LinearLayout object
Ll. addView (generallogo );

// Create a TextView object
TextView textView = getTextView ();
TextView. setText (getChild (groupPosition, childPosition)
. ToString ());

Ll. addView (textView );
Return ll;
}


@ Override
Public boolean isChildSelectable (int groupPosition,
Int childPosition ){
// TODO Auto-generated method stub
Return true;
}




@ Override
Public boolean areAllItemsEnabled (){
// TODO Auto-generated method stub
Return false;
}






@ Override
Public long getCombinedChildId (long groupId, long childId ){
// TODO Auto-generated method stub
Return 0;
}




@ Override
Public long getCombinedGroupId (long groupId ){
// TODO Auto-generated method stub
Return 0;
}




@ Override
Public boolean isEmpty (){
// TODO Auto-generated method stub
Return false;
}




@ Override
Public void onGroupCollapsed (int groupPosition ){
// TODO Auto-generated method stub

}




@ Override
Public void onGroupExpanded (int groupPosition ){
// TODO Auto-generated method stub

}




@ Override
Public void registerDataSetObserver (DataSetObserver observer ){
// TODO Auto-generated method stub

}


@ Override
Public void unregisterDataSetObserver (DataSetObserver observer ){
// TODO Auto-generated method stub

}};


// Set simpleexpandablelistview to the expandablelistadapter
ExpandableListView expandableListView = (ExpandableListView) findViewById (android. R. id. list );
ExpandableListView. setAdapter (ela );


// Bind the listener of ExpandableListView child
ExpandableListView. setOnChildClickListener (new expandableChildLisenter ());

}

// Implement the second-level menu listener
Class expandableChildLisenter implements OnChildClickListener {


@ Override
Public boolean onChildClick (ExpandableListView parent, View v,
Int groupPosition, int childPosition, long id ){
// TODO Auto-generated method stub
System. out. println ("group =" + groupPosition );
System. out. println ("child =" + childPosition );
System. out. println ("id =" + id );

Return false;
}

}

@ Override
Public boolean onCreateOptionsMenu (Menu menu ){
// Inflate the menu; this adds items to the action bar if it is present.
GetMenuInflater (). inflate (R. menu. main, menu );
Return true;
}


}



Main. xml:
<RelativeLayout 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"
Android: paddingBottom = "@ dimen/activity_vertical_margin"
Android: paddingLeft = "@ dimen/activity_horizontal_margin"
Android: paddingRight = "@ dimen/activity_horizontal_margin"
Android: paddingTop = "@ dimen/activity_vertical_margin"
Tools: context = ". MainActivity">


<TextView
Android: id = "@ + id/myText"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: text = "@ string/hello_world"/>

<! -- Set the layout file of the ExpandableListView main interface -->
<ExpandableListView
Android: id = "@ id/android: list"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: padding = "5dp"
Android: layout_below = "@ id/myText"
> </ExpandableListView>


</RelativeLayout>

The image resources used include:R. drawable. list1R. drawable. list2R. drawable. list3R. drawable. list4R. drawable. ic_launcher
The implementation result is as follows:



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.