Saturday, 18 October 2014

ExpandableListView


>extends ListView

>This differs from the ListView by allowing two levels: groups which can individually be expanded to show its children. The items come from the ExpandableListAdapter associated with this view.

>Following is an demo application: Click Here to download source code

Main Activity

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnChildClickListener;
import android.widget.Toast;

public class MainActivity extends Activity {

ExpandableListAdapter expandableListAdapter;
ExpandableListView expandableListView;
List<String> parentListHeader;
HashMap<String, List<String>> childListData;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_layout);
initializeDisplayElements();
prepareExpandableListData();

expandableListAdapter = new ExpandableListAdapter(this,
parentListHeader, childListData);
expandableListView.setAdapter(expandableListAdapter);

expandableListView.setOnChildClickListener(new OnChildClickListener() {

@Override
public boolean onChildClick(ExpandableListView parent, View v,
int groupPosition, int childPosition, long id) {
Toast.makeText(
getApplicationContext(),
parentListHeader.get(groupPosition)
+ " : "
+ childListData.get(
parentListHeader.get(groupPosition))
.get(childPosition), Toast.LENGTH_SHORT)
.show();
return false;
}
});

}

/**
* This method is used to prepare the expandable list data parent and
* corresponding child items.
*/
private void prepareExpandableListData() {
parentListHeader = new ArrayList<String>();
childListData = new HashMap<String, List<String>>();

// adding parent data
parentListHeader.add("Micromax");
parentListHeader.add("Moto");
parentListHeader.add("Samsung");

// adding child data
List<String> micromax = new ArrayList<String>();
micromax.add("micromax Canvas Nitro");
micromax.add("micromax Canvas Knight");
micromax.add("micromax gold");

List<String> moto = new ArrayList<String>();
moto.add("moto-G");
moto.add("moto-E");
moto.add("moto-G2");

List<String> samsung = new ArrayList<String>();
samsung.add("samsung grand");
samsung.add("samsung Note 2");
samsung.add("samsung Galaxy");

childListData.put(parentListHeader.get(0), micromax);
childListData.put(parentListHeader.get(1), moto);
childListData.put(parentListHeader.get(2), samsung);
}

/**
* This method is used to initialize display elements present on layout
*/
private void initializeDisplayElements() {
expandableListView = (ExpandableListView) findViewById(R.id.expandableListView1);

}
}

 ExpandableListAdapter

import java.util.HashMap;
import java.util.List;
import com.example.expandablelist.R;
import android.content.Context;
import android.graphics.Typeface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;

public class ExpandableListAdapter extends BaseExpandableListAdapter {

private Context _context;
private List<String> _listDataHeader;
private HashMap<String, List<String>> _listDataChild;

public ExpandableListAdapter(Context context, List<String> listDataHeader,
HashMap<String, List<String>> listDataChild) {
this._context = context;
this._listDataHeader = listDataHeader;
this._listDataChild = listDataChild;
}

@Override
public Object getChild(int groupPosition, int childPosition) {
return this._listDataChild.get(this._listDataHeader.get(groupPosition))
.get(childPosition);
}

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

return childPosition;
}

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

final String childText = (String) getChild(groupPosition, childPosition);

if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) this._context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.list_item, null);
}

TextView txtListChild = (TextView) convertView
.findViewById(R.id.lblListItem);

txtListChild.setText(childText);
return convertView;
}

@Override
public int getChildrenCount(int groupPosition) {
return this._listDataChild.get(this._listDataHeader.get(groupPosition))
.size();
}

@Override
public Object getGroup(int groupPosition) {

return this._listDataHeader.get(groupPosition);

}

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

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

@Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
String headerTitle = (String) getGroup(groupPosition);
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) this._context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.list_group, null);
}

TextView lblListHeader = (TextView) convertView
.findViewById(R.id.lblListHeader);
lblListHeader.setTypeface(null, Typeface.BOLD);
lblListHeader.setText(headerTitle);

return convertView;
}

@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;
}

}
























No comments:

Post a Comment