Vous êtes sur la page 1sur 30

Android nos brinda con mltiples vista que nos facilitan la vida a la hora de disear e

implementar las interfaces de nuestras apps, entre ellas se encuentra la que pasamos a
ensearos hoy y que nos proporciona una manera elegante de mostrar listados de datos
agrupados de una forma ordenada.
Normalmente usaremos un ListView para mostrar los datos pero si la cantidad de estos es
excesiva y tenemos la posibilidad de agruparlos mediante algn criterio una opcin muy a
tener en cuenta seria usar ExpandableListView que es la vista que a continuacin os
vamos a ensear a usar:

Nota: En este tutorial suponemos unos conocimientos bsicos de desarrollo en Android y


manejo de layouts mediante xml.

PASO 1 Inicializamos el ExpandableListView y ExpandableListAdapter en el


onCreate()

Una vez hemos incluido nuestro ExpandableListView en nuestro xml de definicin de


Layout y le hemos asociado el un id, mediante cdigo instanciaremos el Adapter y los
asociaremos al mismo:
?

1
2
3
4

ExpandableListAdapter mAdapter;
ExpandableListView epView = (ExpandableListView) indViewById(R.id.ExpandableListV
mAdapter = new MyExpandableListAdapter();
epView.setAdapter(mAdapter);

PASO 2 Creamos nuestro Adapter

Al igual que ListView y cualquier AdapterView deberemos usar un adapter que gestione la
coleccin de datos que mostraremos, aqu os dejamos un ejemplo de uno:
?

1
2
3
4

/**

* A simple adapter which maintains an ArrayList of photo resource Ids. Each


* photo is displayed as an image. This adapter supports clearing the list
* of photos and adding a new photo.
*

5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54

*/
public class MyExpandableListAdapter extends BaseExpandableListAdapter {
// Sample data set. children[i] contains the children (String[]) for
// groups[i].
private String[] groups = { "Parent1", "Parent2",
"Parent3" };
private String[][] children = { { "Child1" },{ "Child2" }, { "Child3" },{ "Chi
public Object getChild(int groupPosition, int childPosition) {
return children[groupPosition][childPosition];
}
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
public int getChildrenCount(int groupPosition) {
int i = 0;
try {
i = children[groupPosition].length;
} catch (Exception e) {
}

return i;

public TextView getGenericView() {


// Layout parameters for the ExpandableListView
AbsListView.LayoutParams lp = new AbsListView.LayoutParams(
ViewGroup.LayoutParams.FILL_PARENT, 64);
TextView textView = new TextView(MainActivity.this);
textView.setLayoutParams(lp);
// Center the text vertically
textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
textView.setTextColor(R.color.marcyred);
// Set the text starting position
textView.setPadding(36, 0, 0, 0);
return textView;
}
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 Object getGroup(int groupPosition) {
return groups[groupPosition];
}
public int getGroupCount() {
return groups.length;
}
public long getGroupId(int groupPosition) {
return groupPosition;

55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82

}
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
TextView textView = getGenericView();
textView.setText(getGroup(groupPosition).toString());
return textView;
}
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
public boolean hasStableIds() {
return true;
}
}

En este ejemplo generamos las vistas tanto del Parent como del Child mediante cdigo,
pero no habra ningn problema en generarlas a partir de archivos xml. Aqu un ejemplo:
?

1
2
3
4
5
6
7
8
9
10
11
12
13

public View getGroupView(int groupPosition, boolean isExpanded, View convertView,


View rowparent = View.inflate(ctx, R.layout.rowparent, null);
TextView nameTV = (TextView) rowparent.findViewById(R.id.name);
nameTV.setText(countryName);

// Nota: El LayoutParams ira en funcion del contenedor global elegido en


// Al inflar codigo alguna de las configuraciones del xml se pierden y s
siguiente.
AbsListView.LayoutParams lp = new AbsListView.LayoutParams(ViewGroup.Layo
nameTV.setLayoutParams(lp);
return nameTV;
}

PASO 3 Listeners

Por ltimo implementaremos los mtodos que aadirn la funcionalidad a los elementos
del listado:
?

1
2
3
4
5
6
7
8
9
1 epView.setOnGroupClickListener(new OnGroupClickListener() {
@Override
0
public boolean onGroupClick(ExpandableListView arg0, View arg1,
11
int groupPosition, long arg3) {
1
if (groupPosition == 5) {
2
1
}
3
1
// Aqui podriamos cambiar si quisieramos el comportamiento de apertura y ci
4 expandGroup(int groupPos)
1
return false;
5
}
1
});
6
1
epView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
7
@Override
public boolean onChildClick(ExpandableListView parent,
1
View v, int groupPosition, int childPosition,
8
long id) {
1
if (groupPosition == 0 && childPosition == 0) {
9
2
}
0
return false;
2
}
1
});
2
2
2
3
2
4
2
5
2
6

PASO 4 Personalizacin

Mediante xml podemos configurar ciertos aspectos grficos como los divisores del listado o
los indicadores. Podis ver el listado completo en la documentacin oficial. A continuacin
os contamos como crear nuestro propio groupIndicator.

Si no quisiramos incluir ningn groupIndicator bastara con indicarlo en el propio xml de la


siguiente manera:
?

android:groupIndicator="@null"

En caso contario nos crearamos nuestro propio drawable que referenciaramos en el


mismo atributo. Aqu un ejemplo de la estructura que debera de seguir el drawable:
?

1
2
3
4
5

<selectorxmlns:android="http://schemas.android.com/apk/res/android">
<itemandroid:state_empty="true"android:drawable="@android:color/transparent"/
<itemandroid:state_expanded="true"android:drawable="@drawable/my_icon_max"/>
<itemandroid:drawable="@drawable/my_icon_min"/>
</selector>

Y con esto hemos terminado, con un poco de maa e imaginacin se pueden conseguir
resultados muy vistosos como el que os mostramos de nuestra prximas app:

ExpandableListView With Example In


Android Studio
In Android, ExpandableListView is a View that shows items in a
vertically scrolling two level list. Different from the listview by
allowing two level groups which can individually be expanded to
show its children. Each group can be expanded or collapsed
individually to show or hide its children items.

We can attach listeners events to the ExpandableListView to


listen for OnClick or any other events on the Group or the
individual children. Adapters are used to supply or control the
data that will be displayed in a ExpandableListView.
Important Note: You cannot use the value wrap_content for
the height attribute of a ExpandableListView in XML if the
parents size is not strictly specified. In other words we mean if
the parent were ScrollView then you could not specify
wrap_content since it can be of any length. However, you can
use wrap content if the ExpandableListView parent has a specific
size, such as 200 pixels.
ExpandableListView code in XML:
<ExpandableListView
android:id="@+id/simpleExpandableListView"
android:layout_width="fill_parent"

android:layout_height="fill_parent"/>

Table Of Contents [hide]


1 Attributes of ExpandableListView In Android
2 Adapter Used In ExpandableListView In Android:
3 ExpandableListView
using
BaseExpandableListAdapter
Example In Android Studio

Attributes of ExpandableListView In Android


Now lets we discuss about some important attributes that helps
us to configure a ExpandableListView in XML file(layout).
1. id: id is an attribute used to uniquely identify a Expandable
List View.
<ExpandableListView
android:id="@+id/simpleExpandableListView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/> <!-identify a expandable list view -->

id of an attribute used to uniquely

2. divider: This is a drawable or color to draw between different


group list items.
Below we draw red color divider between different group items.
<ExpandableListView

android:id="@+id/simpleExpandableListView"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:divider="#f00"
android:dividerHeight="1dp" /> "/> <!-- red color divider with 1dp height between
the groups items of expandable list view -->

3. dividerHeight: This specify the height of the divider


between group list items. This could be in dp ( density pixel ),
sp(scale independent pixel) or px ( pixel ).
In above example of divider we also set the divider height 1dp
between the list items. The height should be in dp, sp or px.
4. listSelector: This property is used to set the selector of the
expandable list View. It is generally orange or Sky blue color
mostly but you can also define your own custom color or an
image as a list selector as per your design.
Below selector color is green, when you select any list item then
that items background color is green.
<ExpandableListView
android:id="@+id/simpleExpandableListView"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:divider="#f00"
android:dividerHeight="1dp"

android:listSelector="#0f0" /> <!-- green color for the list selector item -->

5. childDivider: This is a drawable or color to draw between


different child list items of a expandable list view.
Below we draw green color divider between different child items
of a group.
<ExpandableListView
android:id="@+id/simpleExpandableListView"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:divider="#f00"
android:dividerHeight="1dp"
android:childDivider="#0f0" /> <!-- green color divider between the child items
of expandable list view -->

The below image is from the ExpandableListView example which


is explained at the end of this post. In this we have set green
color as Child divider and red color as divider. The reason we
have used this example image because we need fill data
using Adapter to show you childDivider attribute in action.

6. padding: padding attribute is used to set the padding from


left, right, top or bottom.
paddingRight: set the padding from the right side of the
expandable list view.
paddingLeft: set the padding from the left side of the
Progress bar.
paddingTop: set the padding from the top side of the
expandable list view.
paddingBottom: set the padding from the bottom side of
the expandable list view.
Padding: set the padding from the all sides of the
expandable list view.
Below we set the 50dp padding from all the sides of the
expandable list view.
<ExpandableListView
android:id="@+id/simpleExpandableListView"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:divider="#f00"
android:dividerHeight="2dp"
android:childDivider="#0f0"
android:padding="50dp" /> <!-- 50 dp padding from all the sides of a expandable
list view -->

Adapter Used In ExpandableListView In Android:


An adapter is a bridge between UI component and data source
that helps us to fill data in UI component. It holds the data and
send the data to Adapter view then view can takes the data from
the adapter view and shows the data on different views like as
ExpandableListView or other Views. The implementation of
thisinterface will provide access to the data of the children
(categorized by groups), and also instantiate views for the
children and groups.
In Android for supplying data in a ExpandableListView following
adapters are used.
1. ExpandableListAdapter
2. BaseExpandableListAdapter
3. SimpleExpandableListAdapter
Now we explain these three adapters in detail:
1. ExpandableListAdapter:
ExpandableListAdapter
is
ExpandableListView
with

an
the

Adapter
that
links
a
underlying
data.
The

implementation of this interface will provide the data for the


children and also initiate the views for the children and groups.
For
customization
of
list
we
need
to
implement
ExpandableListAdapter in our custom adapter.
Below is the example code of ExpandableListAdapter in which
we
create
CustomAdapter class and
then
implements
ExpandableListAdapter in that class.
public class CustomAdapter implements ExpandableListAdapter {
@Override
public void registerDataSetObserver(DataSetObserver observer) {

@Override
public void unregisterDataSetObserver(DataSetObserver observer) {

@Override
public int getGroupCount() {
return 0;
}

@Override
public int getChildrenCount(int groupPosition) {
return 0;
}

@Override
public Object getGroup(int groupPosition) {
return null;
}

@Override
public Object getChild(int groupPosition, int childPosition) {

return null;
}

@Override
public long getGroupId(int groupPosition) {
return 0;
}

@Override
public long getChildId(int groupPosition, int childPosition) {
return 0;
}

@Override
public boolean hasStableIds() {
return false;
}

@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView,
ViewGroup parent) {
return null;
}

@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild,
View convertView, ViewGroup parent) {
return null;
}

@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return false;
}

@Override

public boolean areAllItemsEnabled() {


return false;
}

@Override
public boolean isEmpty() {
return false;
}

@Override
public void onGroupExpanded(int groupPosition) {

@Override
public void onGroupCollapsed(int groupPosition) {

@Override
public long getCombinedChildId(long groupId, long childId) {
return 0;
}

@Override
public long getCombinedGroupId(long groupId) {
return 0;
}
}

Read ExpandableListAdapter Tutorial With Example In Android


Studio for more details.
2. BaseExpandableListAdapter:
BaseExpandableListAdapter is a base class for the expandable
list adapter used to provide data and Views from some data to
ExpandableListView. For Creating a custom ExpandableListView

we need to create a custom class and


BaseExpandableListAdapter class in that class.

then

extends

Below is an example code of BaseExpandableListAdapter in


which we create custom adapter class and then extends
BaseExpandableListAdapter in that class.
public class CustomAdapter extends BaseExpandableListAdapter {

@Override
public int getGroupCount() {
return 0;
}

@Override
public int getChildrenCount(int groupPosition) {
return 0;
}

@Override
public Object getGroup(int groupPosition) {
return null;
}

@Override
public Object getChild(int groupPosition, int childPosition) {
return null;
}

@Override
public long getGroupId(int groupPosition) {
return 0;
}

@Override
public long getChildId(int groupPosition, int childPosition) {
return 0;

@Override
public boolean hasStableIds() {
return false;
}

@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView,
ViewGroup parent) {
return null;
}

@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild,
View convertView, ViewGroup parent) {
return null;
}

@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return false;
}
}

Read BaseExpandableListAdapter With Example


Studio for explanation of all these function.

In

Android

3. SimpleExpandableListAdapter:
SimpleExpandableListAdapter is an adapter that is used to map
the static data to group and child views defined in our XML
( layout ) file. We can separately specify the data backing to the
group as a List of Maps. Each entry in a ArrayList corresponds to
one group in the Expandable List. The maps contains the data
for each row. We can also specify an XML file that defines the
views used to display a group, and a mapping from keys in the
Map to specific views. This process is similar for a child, except it
is one level deeper so the data backing is specified as a

List<list>, where the first List is corresponds to the group of the


child and the second List corresponds to the position of the child
within that group, and finally the Map holds the data for the
particular child.
public SimpleExpandableListAdapter (Context context, List<? extends Map<String,
?>>
groupData,
int
groupLayout, String[]groupFrom,
int[]
groupTo, List<? extends List<? extends Map<String, ?>>>
childData,
int
childLayout, String[] childFrom, int[] childTo)

Read SimpleExpandableListAdapter tutorial for explanation of all


these parameter.

ExpandableListView using BaseExpandableListAdapter


Example In Android Studio
Below
is
the
example
of
ExpandableListView
in
android where we display an expandable list with subject name
and their topics. In this example we display subject names
as Group items and their topic names as child items for a
particular group. In this we implement setOnChildClickListener()
and setOnGroupClickListener() events and whenever a user
clicks on a child or a group item the name of the item is
displayed by using a Toast.
Below you can download code, final output and step by step
explanation of Example in Android Studio:
Download Code (Password: abhiandroid) ?

Step
1: Create
a
new
ExpandableListViewExample.

project and

name

it

Step 2: Open res -> layout ->activity_main.xml (or) main.xml


and add following code:
In this step we open an XML file and add the code for displaying
a ExpandableListView by using its different attributes.
<?xml version="1.0" encoding="UTF-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<ExpandableListView
android:id="@+id/simpleExpandableListView"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:divider="#f00"
android:childDivider="#0f0"
android:dividerHeight="1dp" />

</RelativeLayout>

Step 3: Create a new xml file for group items Open res ->
layout -> group_items.xml and add following code:
In this step we add the code for displaying a TextView subject
names.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="55dip"
android:orientation="vertical" >

<TextView
android:id="@+id/heading"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="35sp"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textStyle="bold" />

</LinearLayout>

Step 4: Create a new xml file for group items Open res ->
layout -> child_items.xml and add following code:
In this step we add the code for displaying two TextView i.e. one
for sequence of topics and another for topic name
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
android:layout_height="match_parent"

android:layout_width="match_parent"

android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android">

<TextView
android:id="@+id/sequence"
android:layout_width="wrap_content"
android:layout_height="wrap_content"

android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:paddingLeft="35sp"
android:textAppearance="?android:attr/textAppearanceMedium" />

<TextView
android:id="@+id/childItem"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toRightOf="@id/sequence"
android:textAppearance="?android:attr/textAppearanceMedium" />

</RelativeLayout>

Step 5: Open src -> package -> MainActivity.Java


In this step we open MainActivity and add the code to initiate
the ExpandableListView and add the data to lists for displaying
in an ExpandableListView using model classes and then set the
adapter which fills the data in the ExpandableListView. In this we
implement
setOnChildClickListener()
and
setOnGroupClickListener() events. Whenever a user clicks on a
child or a group item the name of the item is display by using
a Toast.
package example.abhiandroid.expandablelistviewexample;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ExpandableListView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.LinkedHashMap;

public class MainActivity extends AppCompatActivity{

private
LinkedHashMap<String,
LinkedHashMap<String, GroupInfo>();

GroupInfo>

subjects

new

private ArrayList<GroupInfo> deptList = new ArrayList<GroupInfo>();

private CustomAdapter listAdapter;


private ExpandableListView simpleExpandableListView;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

// add data for displaying in expandable list view


loadData();

//get reference of the ExpandableListView


simpleExpandableListView
findViewById(R.id.simpleExpandableListView);

(ExpandableListView)

// create the adapter by passing your ArrayList data


listAdapter = new CustomAdapter(MainActivity.this, deptList);
// attach the adapter to the expandable list view
simpleExpandableListView.setAdapter(listAdapter);

//expand all the Groups


expandAll();

// setOnChildClickListener listener for child row click


simpleExpandableListView.setOnChildClickListener(new
ExpandableListView.OnChildClickListener() {
@Override
public boolean onChildClick(ExpandableListView parent, View v, int
groupPosition, int childPosition, long id) {
//get the group header
GroupInfo headerInfo = deptList.get(groupPosition);
//get the child info
ChildInfo detailInfo = headerInfo.getProductList().get(childPosition);

//display it or do something with it


headerInfo.getName()

Toast.makeText(getBaseContext(), " Clicked on :: " +

+ "/" + detailInfo.getName(), Toast.LENGTH_LONG).show();


return false;
}
});
// setOnGroupClickListener listener for group heading click
simpleExpandableListView.setOnGroupClickListener(new
ExpandableListView.OnGroupClickListener() {
@Override
public boolean onGroupClick(ExpandableListView parent, View v, int
groupPosition, long id) {
//get the group header
GroupInfo headerInfo = deptList.get(groupPosition);
//display it or do something with it
Toast.makeText(getBaseContext(), " Header is :: " +
headerInfo.getName(),
Toast.LENGTH_LONG).show();

return false;
}
});

//method to expand all groups


private void expandAll() {
int count = listAdapter.getGroupCount();
for (int i = 0; i < count; i++){
simpleExpandableListView.expandGroup(i);
}
}

//method to collapse all groups


private void collapseAll() {

int count = listAdapter.getGroupCount();


for (int i = 0; i < count; i++){
simpleExpandableListView.collapseGroup(i);
}
}

//load some initial data into out list


private void loadData(){

addProduct("Android","ListView");
addProduct("Android","ExpandableListView");
addProduct("Android","GridView");

addProduct("Java","PolyMorphism");
addProduct("Java","Collections");

//here we maintain our products in various departments


private int addProduct(String department, String product){

int groupPosition = 0;

//check the hash map if the group already exists


GroupInfo headerInfo = subjects.get(department);
//add the group if doesn't exists
if(headerInfo == null){
headerInfo = new GroupInfo();
headerInfo.setName(department);
subjects.put(department, headerInfo);
deptList.add(headerInfo);
}

//get the children for the group

ArrayList<ChildInfo> productList = headerInfo.getProductList();


//size of the children list
int listSize = productList.size();
//add to the counter
listSize++;

//create a new child and add that to the group


ChildInfo detailInfo = new ChildInfo();
detailInfo.setSequence(String.valueOf(listSize));
detailInfo.setName(product);
productList.add(detailInfo);
headerInfo.setProductList(productList);

//find the group position inside the list


groupPosition = deptList.indexOf(headerInfo);
return groupPosition;
}

Step 6: Create a New Class Open ->


GroupInfo.Java and add the following code.

package

>

In this step, we create a class for setting and getting the group
item name and child items info according to a particular group.
GroupInfo is a model class used to set the name of the group
item and child items information from your main activity and
then get the information within Adapter class. Finally set the
value to ExpandableListView.
package example.abhiandroid.expandablelistviewexample;

import java.util.ArrayList;

public class GroupInfo {

private String name;

private ArrayList<ChildInfo> list = new ArrayList<ChildInfo>();

public String getName() {


return name;
}

public void setName(String name) {


this.name = name;
}

public ArrayList<ChildInfo> getProductList() {


return list;
}

public void setProductList(ArrayList<ChildInfo> productList) {


this.list = productList;
}

Step 7: Create a New Class Open -> package > ChildInfo.Java


and add the following code.
In this step, we create a class for setting and getting the name
and sequence for the child items. ChildInfo is a model class used
to set the name of the child item and the sequence of the child
item from your main activity and then get the name and
sequence within adapter class. Finally set the value to
expandable list view.
package example.abhiandroid.expandablelistviewexample;

public class ChildInfo {

private String sequence = "";


private String name = "";

public String getSequence() {


return sequence;
}

public void setSequence(String sequence) {


this.sequence = sequence;
}

public String getName() {


return name;
}

public void setName(String name) {


this.name = name;
}

Step 8: Create a New Class Open -> package


CustomAdapter.Java and add the following code.

>

In this step, we create a CustomAdapter class and then extends


BaseExpandableListAdapter in that class. Finally set the data in
the ExpandableListView from GroupInfo and ChildInfo model
class.
package example.abhiandroid.expandablelistviewexample;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;

import java.util.ArrayList;

/**
* Created by Gourav on 08-03-2016.
*/
public class CustomAdapter extends BaseExpandableListAdapter {

private Context context;


private ArrayList<GroupInfo> deptList;

public CustomAdapter(Context context, ArrayList<GroupInfo> deptList) {


this.context = context;
this.deptList = deptList;
}

@Override
public Object getChild(int groupPosition, int childPosition) {
ArrayList<ChildInfo>
deptList.get(groupPosition).getProductList();

productList

return productList.get(childPosition);
}

@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}

@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild,
View view, ViewGroup parent) {

ChildInfo detailInfo = (ChildInfo) getChild(groupPosition, childPosition);


if (view == null) {
LayoutInflater infalInflater =
context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = infalInflater.inflate(R.layout.child_items, null);
}

(LayoutInflater)

TextView sequence = (TextView) view.findViewById(R.id.sequence);


sequence.setText(detailInfo.getSequence().trim() + ". ");
TextView childItem = (TextView) view.findViewById(R.id.childItem);
childItem.setText(detailInfo.getName().trim());

return view;
}

@Override
public int getChildrenCount(int groupPosition) {

ArrayList<ChildInfo>
deptList.get(groupPosition).getProductList();

productList

return productList.size();

@Override
public Object getGroup(int groupPosition) {
return deptList.get(groupPosition);
}

@Override
public int getGroupCount() {
return deptList.size();
}

@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}

@Override
public View getGroupView(int groupPosition, boolean isLastChild, View view,
ViewGroup parent) {

GroupInfo headerInfo = (GroupInfo) getGroup(groupPosition);


if (view == null) {
LayoutInflater
inf
=
context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

(LayoutInflater)

view = inf.inflate(R.layout.group_items, null);


}

TextView heading = (TextView) view.findViewById(R.id.heading);


heading.setText(headerInfo.getName().trim());

return view;
}

@Override
public boolean hasStableIds() {
return true;
}

@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}

Vous aimerez peut-être aussi