Android Expandablelistview Expand List control Use instance _android

Source: Internet
Author: User
Tags event listener stub xmlns

Do you think the Friend list on mobile phone QQ is a great control? No..... That's okay, it's good for you to learn a little more knowledge.

So let's get started.

Expanded-type list control, formerly known as Expandablelistview
is the normal list control advanced version, you can freely the list to shrink, very convenient and good-looking.
First look at my completed screenshot, although the interface is not beautiful, but you can modify the interface.

The control requires a main interface XML a header interface XML and a list content interface xml
First, let's look at the Mian.xml main interface.

Copy Code code as follows:
The interface is very simple, as long as a expandablelistview can
<linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android"
Android:layout_width= "Fill_parent"
android:layout_height= "Fill_parent"
android:orientation= "Vertical" >

<expandablelistview
Android:id= "@id/android:list"
Android:layout_width= "Fill_parent"
android:layout_height= "Fill_parent"
/>
</LinearLayout>

Groups.xml This interface is the parent header interface
All we have to do is put a title TextView control to show it up.

Copy Code code as follows:
<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:id= "@+id/textgroup"
Android:layout_width= "Fill_parent"
android:layout_height= "Fill_parent"
android:paddingleft= "40px"
android:paddingtop= "6PX"
Android:paddingbottom= "6PX"
Android:textsize= "15SP"
android:text= "No Data"
/>

</LinearLayout>

Childs.xml is a child control that directly displays the contents of the list

Copy Code code as follows:
<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:id= "@+id/textchild"
    android:layout_width= "Fill_parent"
        android:layout_height= " Fill_parent "
        android:paddingleft=" 60px
         android:paddingtop= "10px"
        android: Paddingbottom= "10px"
        android:textsize= "20sp"
    android:text= "No Data"
/>

</linearlayout>


Then the main code, naming a bit messy, we are really used for development can not be so named AH.

Copy Code code as follows:
public class Expandactivity extends expandablelistactivity
{
/** called the activity is a. */
@Override
public void OnCreate (Bundle savedinstancestate)
{
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.main);

Create a two-level entry heading
map<string, string> title_1 = new hashmap<string, string> ();
map<string, string> title_2 = new hashmap<string, string> ();

Title_1.put ("group", "development");
Title_2.put ("group", "Management");

Create a first Level entry container
list<map<string, string>> gruops = new arraylist<map<string,string>> ();

Gruops.add (title_1);
Gruops.add (title_2);

Create level Two entry content

Content A
map<string, string> content_1 = new hashmap<string, string> ();
map<string, string> content_2 = new hashmap<string, string> ();

Content_1.put ("Child", "VC + +");
Content_2.put ("Child", "Java");

list<map<string, string>> childs_1 = new arraylist<map<string,string>> ();
Childs_1.add (content_1);
Childs_1.add (content_2);

Content Two
map<string, string> content_3 = new hashmap<string, string> ();
map<string, string> content_4 = new hashmap<string, string> ();

Content_3.put ("Child", "agile development");
Content_4.put ("Child", "iterative development");

list<map<string, string>> childs_2 = new arraylist<map<string,string>> ();
Childs_2.add (Content_3);
Childs_2.add (Content_4);

Store two contents for display in the list
list<list<map<string, string>>> childs = new arraylist<list<map<string,string>> > ();
Childs.add (Childs_1);
Childs.add (childs_2);

Create a Expandablelist adapter container
Parameters: 1. Context 2. First-level collection 3. One-level style file 4. First level entry key value 5. Display control name at level one
6. Second-level set 7. Second-level style 8. Level Two entry key value 9. Two Display control name
Simpleexpandablelistadapter sela = new Simpleexpandablelistadapter (
This, Gruops, r.drawable.groups, new string[]{"group"}, New Int[]{r.id.textgroup},
Childs, R.drawable.childs, New string[]{"Child"}, new Int[]{r.id.textchild}
);

Join list
Setlistadapter (SELA);
}
}
Finally, if you want to respond to each operation, you should overload the following method
The contents of the list are pressed
@Override
public Boolean Onchildclick (expandablelistview parent, View v, int groupposition, int childposition, long id)
{
TODO auto-generated Method Stub
Return Super.onchildclick (parent, V, groupposition, childposition, id);
}

Second-level title press
@Override
public boolean setselectedchild (int groupposition, int childposition, Boolean shouldexpandgroup)
{
TODO auto-generated Method Stub
Return Super.setselectedchild (Groupposition, childposition, Shouldexpandgroup);
}

The first level title presses
@Override
public void Setselectedgroup (int groupposition)
{
TODO auto-generated Method Stub
Super.setselectedgroup (groupposition);
}

And finally, run your simulator and you can see it.
Control the Expandablelistview above.
The control is relatively simple we just use the ordinary activity class to be able, no longer inherit Expandablelistview.
You only need to add in the member variable
Private Expandablelistview expandlist;
And then when you add content, change to
Expandlist.setadapter (SELA);
It's OK. Of course, the response event listener can also be added yourself.

Attachment: Another example

The use of Expandablelistview is similar to ListView and Gridview,gallery, and is displayed through a adapter.
Main.xml:

Copy Code code as follows:
<?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" >
<expandablelistview android:id= "@+id/elv" android:indicatorright= "160DP"
Android:layout_width= "Fill_parent" android:layout_height= "Fill_parent" >
</ExpandableListView>
<!--indicatorright Set the spacing between the right edge of the small icon and the left edge of the Expandablelistview-->
</LinearLayout>

Elvadapter.java
Copy Code code as follows:
Package com.test;

Import java.util.ArrayList;

Import Android.content.Context;
Import Android.view.View;
Import Android.view.ViewGroup;
Import Android.widget.BaseExpandableListAdapter;
Import Android.widget.ExpandableListView;
Import Android.widget.TextView;
Import Android.widget.LinearLayout.LayoutParams;

public class Elvadapter extends Baseexpandablelistadapter
{

Arraylist<elvobject> Objs;
Context context;
Elvadapter (Context context,arraylist<elvobject> OBJS)
{
THIS.OBJS=OBJS;
This.context=context;
}
@Override
Public Object getchild (int groupposition, int childposition)
{
TODO auto-generated Method Stub
Return Objs.get (groupposition). Childs.get (childposition);
}

@Override
public long Getchildid (int groupposition, int childposition)
{
TODO auto-generated Method Stub
return childposition;
}

@Override
Public View getchildview (int groupposition, int childposition, Boolean islastchild, View Convertview, ViewGroup parent)
{
View of child elements
TextView tv=new TextView (context);
Tv.settext (Objs.get (groupposition). Childs.get (childposition));
Tv.setlayoutparams (New Expandablelistview.layoutparams (ExpandableListView.LayoutParams.FILL_PARENT, ExpandableListView.LayoutParams.WRAP_CONTENT));
return TV;
}

@Override
public int Getchildrencount (int groupposition)
{
TODO auto-generated Method Stub
Return Objs.get (groupposition). Childs.size ();
}

@Override
Public Object getgroup (int groupposition)
{
TODO auto-generated Method Stub
Return Objs.get (groupposition);
}

@Override
public int GetGroupCount ()
{
TODO auto-generated Method Stub
return Objs.size ();
}

@Override
public long getgroupid (int groupposition)
{
TODO auto-generated Method Stub
return groupposition;
}

@Override
Public View getgroupview (int groupposition, Boolean isexpanded, View Convertview, ViewGroup parent)
{
View in Groups
TextView tv=new TextView (context);
Tv.settext (Objs.get (groupposition). GroupName);
Expandablelistview.layoutparams params=new Expandablelistview.layoutparams (ExpandableListView.LayoutParams.FILL_ PARENT,60);
Tv.setlayoutparams (params);
return TV;
}

@Override
public boolean hasstableids ()
{
TODO auto-generated Method Stub
return false;
}

@Override
public boolean ischildselectable (int groupposition, int childposition)
{
TODO auto-generated Method Stub
return true;
}

}

Class elvobject{
String groupname= "";
Arraylist<string> childs=new arraylist<string> ();
Elvobject (String groupname,arraylist<string> Childs)
{
This.groupname=groupname;
This.childs=childs;
}
}


Note that there is also a elvobject class below
Main program Androidtestactivity.java
Copy Code code as follows:
Package com.test;

Import java.util.ArrayList;

Import android.app.Activity;
Import Android.os.Bundle;
Import Android.widget.ExpandableListView;

public class Androidtestactivity extends activity
{
/** called the activity is a. */
Expandablelistview ELV;
Elvadapter adapter;
Arraylist<elvobject> objs=new arraylist<elvobject> ();
@Override
public void OnCreate (Bundle savedinstancestate)
{
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.main);
elv= (Expandablelistview) Findviewbyid (R.ID.ELV);
Adapter=new Elvadapter (THIS,OBJS);
Elv.setadapter (adapter);
Arraylist<string> list=new arraylist<string> ();
List.add ("AAA");
List.add ("BBB");
List.add ("CCC");
Objs.add (New Elvobject ("English", list);
Arraylist<string> list2=new arraylist<string> ();
List2.add ("111");
List2.add ("222");
List2.add ("333");
List2.add ("444");
Objs.add (New Elvobject ("number", List2));

}
}

Related Article

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.