The expandablelistactivity of Android control development

Source: Internet
Author: User

The expandablelistactivity of Android control development This post focuses on the simple use of the expandablelistactivity (extensible list) control in Android control development.
Java code:
Package com.example.expandablelistactivity;


Import java.util.ArrayList;
Import Java.util.HashMap;
Import Android.os.Bundle;
Import android.app.ListActivity;
Import Android.view.Menu;
Import Android.view.View;
Import Android.widget.ExpandableListView;
Import Android.widget.SimpleExpandableListAdapter;
Import Android.widget.ExpandableListView.OnChildClickListener;




/* Creating a expandablelistactivity requires the following steps:
* 1, first need to set the layout style of Expandablelistview
* 2. Create a layout style file for one level entry
* 3. Create a layout style file for level two entries
* 4, set the data of the first level entry
* 5, set the data of level two entry
* 6, use Simpleexpandablelistadapter to provide data for expandablelistactivity
* 7, set the listener
* */


public class Mainactivity extends Listactivity {

@Override
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.main);

Object that creates a first-level entry
arraylisthashmap<string, string> group1 = new hashmap<string, string> ();
hashmap<string, string> group2 = new hashmap<string, string> ();

Setting data for a first-level entry
Group1.put ("group", "Group1");
Group2.put ("group", "Group2");

To add data to an object in a first-level entry
Groups.add (group1);
Groups.add (group2);


Creates a Child1 object for level two entries. Used to provide data for the first level of entry
arraylisthashmap<string, string> child1data1 = new hashmap<string, string> ();
hashmap<string, string> child1data2 = new hashmap<string, string> ();
hashmap<string, string> child1data3 = new hashmap<string, string> ();

Child1data1.put ("Child", "child1data1");
Child1data2.put ("Child", "child1data2");
Child1data3.put ("Child", "child1data3");

Child1.add (CHILD1DATA1);
Child1.add (CHILD1DATA2);
Child1.add (CHILD1DATA3);


Creates a Child2 object for level two entries. Used to provide data for a second level entry
arraylisthashmap<string, string> child2data1 = new hashmap<string, string> ();
Child2data1.put ("Child", "child2data1");
Child2.add (CHILD2DATA1);


An object that creates a level two entry.
arraylist<arraylist
Childs.add (child1);
Childs.add (CHILD2);


Simpleexpandablelistadapter ELA = new Simpleexpandablelistadapter (this, groups,
R.layout.group, new string[]{"group"}, New Int[]{r.id.grouptext},
Childs, R.layout.child, New string[]{"Child"}, new Int[]{r.id.childtext});

Setting the Simpleexpandablelistadapter adapter for Expandablelistview
Expandablelistview Expandablelistview = (expandablelistview) Findviewbyid (Android. R.id.list);
Expandablelistview.setadapter (ELA);


Bind Expandablelistview Child's listener
Expandablelistview.setonchildclicklistener (New Expandablechildlisenter ());

}

Implement level two 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 layout File Main.xml code: This layout file is primarily defined as a Expandablelistview
<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 for 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>



level list layout file Group.xml code: This layout file primarily sets the style of the first-level list <?xml version= "1.0" encoding= "Utf-8"?>
<linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android"
Android:layout_width= "Match_parent"
android:layout_height= "Match_parent"
android:orientation= "Vertical" >
<!--mostly set the style of the first entry--
<textview
Android:id= "@+id/grouptext"
Android:layout_width= "Fill_parent"
android:layout_height= "Wrap_content"
android:paddingleft= "30DP"
android:paddingtop= "5DP"
Android:paddingbottom= "5DP"
Android:textsize= "20sp"/>


</LinearLayout>

The effect is as follows:


level Two list layout file Child.xml code: This layout file is primarily used to set the style of a level two list
<?xml version= "1.0" encoding= "Utf-8"?>
<linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android"
Android:layout_width= "Match_parent"
android:layout_height= "Match_parent"
android:orientation= "Vertical" >


<!--setting style two entries--
<textview
Android:id= "@+id/childtext"
Android:layout_width= "Fill_parent"
android:layout_height= "Wrap_content"
android:paddingleft= "35DP"
Android:textsize= "15SP"/>

</LinearLayout>

The effect is as follows:

When you click on an item in the level two list, the first-level list POS, level two list POS, and the two-level list ID are output in Logcat, as follows:




The expandablelistactivity of Android control development

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.