Expandable ListView:
1)Lets start a new project in eclipse as we created for earlier projects
2)we need three xml to create a expandable list view,first xml is for the main list view ,second xml is for the list group and the third xml is for child list view.
3)Design your activity_main.xml with Expandable listview as shown below
4)Create a list_group.xml for the listview group and code as follows
5)Create another xml file named list_item.xml it contaoin only listview item.
6)Here we are going to use a custom adapter class to create the expandable listview,create a new class file named Expandablelistadpater.java and extend this from Baseexpandablelistadapter.java
7)This Baseexpandablelistadapter provide some requierd methods to render listview
- Expandable ListView is used to group, that can individually expanded to show its children.
- When the user touches the header it has the ability to expand and collapse the group.
- In this example you can learn how to create a simple expandable listview as shown below.
1)Lets start a new project in eclipse as we created for earlier projects
2)we need three xml to create a expandable list view,first xml is for the main list view ,second xml is for the list group and the third xml is for child list view.
3)Design your activity_main.xml with Expandable listview as shown below
4)Create a list_group.xml for the listview group and code as follows
5)Create another xml file named list_item.xml it contaoin only listview item.
6)Here we are going to use a custom adapter class to create the expandable listview,create a new class file named Expandablelistadpater.java and extend this from Baseexpandablelistadapter.java
7)This Baseexpandablelistadapter provide some requierd methods to render listview
- Getgroupview( )-listgroup header view are returned
- Getchildview ( )-listchild view are returned
package com.example.expandablelist;
import java.util.HashMap;
import java.util.List;
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 int getGroupCount() {
// TODO Auto-generated method stub
return this._listDataHeader.size();
}
@Override
public int getChildrenCount(int groupPosition) {
// TODO Auto-generated method stub
return this._listDataChild.get(this._listDataHeader.get(groupPosition))
.size();
}
@Override
public Object getGroup(int groupPosition) {
// TODO Auto-generated method stub
return this._listDataHeader.get(groupPosition);
}
@Override
public Object getChild(int groupPosition, int childPosition) {
return this._listDataChild.get(this._listDataHeader.get(groupPosition))
.get(childPosition);
}
@Override
public long getGroupId(int groupPosition) {
// TODO Auto-generated method stub
return groupPosition; }
@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 false;
}
@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.textView1);
lblListHeader.setTypeface(null, Typeface.BOLD);
lblListHeader.setText(headerTitle);
return convertView;
}
@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.textView1);
txtListChild.setText(childText);
return convertView;
// TODO Auto-generated method stub
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
return false;
}
}
import java.util.HashMap;
import java.util.List;
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 int getGroupCount() {
// TODO Auto-generated method stub
return this._listDataHeader.size();
}
@Override
public int getChildrenCount(int groupPosition) {
// TODO Auto-generated method stub
return this._listDataChild.get(this._listDataHeader.get(groupPosition))
.size();
}
@Override
public Object getGroup(int groupPosition) {
// TODO Auto-generated method stub
return this._listDataHeader.get(groupPosition);
}
@Override
public Object getChild(int groupPosition, int childPosition) {
return this._listDataChild.get(this._listDataHeader.get(groupPosition))
.get(childPosition);
}
@Override
public long getGroupId(int groupPosition) {
// TODO Auto-generated method stub
return groupPosition; }
@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 false;
}
@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.textView1);
lblListHeader.setTypeface(null, Typeface.BOLD);
lblListHeader.setText(headerTitle);
return convertView;
}
@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.textView1);
txtListChild.setText(childText);
return convertView;
// TODO Auto-generated method stub
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
return false;
}
}
8) Next its time to code the main activity
here i split and post the main activity ibn to two files
listDataChild.put(listDataHeader.get(0), top250);
listDataChild.put(listDataHeader.get(1), nowShowing);
listDataChild.put(listDataHeader.get(1), nowShowing);
listDataChild.put(listDataHeader.get(2),comingSoon);
10)Now its time to run and test the project ,check with your emulator or device .
0 comments:
Post a Comment