Для расширяемого списка просто введите следующий код:
ExpandableListDemo.java
public class ExpandableListDemo extends Activity {
private ExpandableListView expandableListView;
private ArrayList<Places> places;
private PlaceAdapter placeAdapter;
DisplayMetrics metrics;
int width;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_expandable_list_demo);
initUI();
initData();
placeAdapter=new PlaceAdapter(places, this);
expandableListView.setAdapter(placeAdapter);
expandableListView.setIndicatorBounds(width - GetDipsFromPixel(30), width - GetDipsFromPixel(5));
}
private void initData() {
places=new ArrayList<Places>();
Places place1=new Places();
place1.setName("Maharashtra");
String [] city1={"Pune","Mumbai","Nagpur","Aurangabad","Nasik"};
place1.setCities(city1);
Places place2=new Places();
place2.setName("Gujrat");
String [] city2={"Surat","Bhavnagar","Ahmedabad","Vadodara","Rajkot"};
place2.setCities(city2);
Places place3=new Places();
place3.setName("AndhraPradesh");
String [] city3={ "Hyderabad", "Visakhapatnam", "Vijayawada", "Guntur", "Rajahmundr" };
place3.setCities(city3);
Places place4=new Places();
place4.setName("Karnataka");
String [] city4={"Bagalkote","Bangalore","Belgaum","Bellary"};
place4.setCities(city4);
places.add(place1);
places.add(place2);
places.add(place3);
places.add(place4);
metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
width = metrics.widthPixels;
}
private void initUI() {
expandableListView=(ExpandableListView) findViewById(R.id.places_listview);
}
public int GetDipsFromPixel(float pixels)
{
// Get the screen's density scale
final float scale = getResources().getDisplayMetrics().density;
// Convert the dps to pixels, based on density scale
return (int) (pixels * scale + 0.5f);
}
}
Адаптер нестандартного места:
public class PlaceAdapter extends BaseExpandableListAdapter{
private ArrayList<Places> placesList;
private LayoutInflater mInflater;
public PlaceAdapter(ArrayList<Places> placesList,Context context) {
this.placesList=placesList;
mInflater=LayoutInflater.from(context);
}
//return child at specific position.
@Override
public Object getChild(int groupPosition, int childPosition) {
return placesList.get(groupPosition).getCities();
}
//return child id for specific position.
@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
//return child view at specific position.
@Override
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
ChildHolder childHolder;
if(convertView==null) {
childHolder=new ChildHolder();
convertView=mInflater.inflate(R.layout.child_item,null);
childHolder.childTv=(TextView) convertView.findViewById(R.id.txt_chid_item);
convertView.setTag(childHolder);
}else{
childHolder=(ChildHolder) convertView.getTag();
}
childHolder.childTv.setText("-->"+placesList.get(groupPosition).getCities()[childPosition]);
return convertView;
}
//return number of child for specific parent.
@Override
public int getChildrenCount(int groupPosition) {
return placesList.get(groupPosition).getCities().length;
}
//get parent at specific position.
@Override
public Object getGroup(int groupPosition) {
return placesList.get(groupPosition);
}
//return number of parent items.
@Override
public int getGroupCount() {
return placesList.size();
}
//return parent id for specific position
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
//return parent view for specific position
@Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
ParentHolder parentHolder;
if(convertView==null) {
parentHolder=new ParentHolder();
convertView=mInflater.inflate(R.layout.parent_item,null);
parentHolder.parentTv=(TextView) convertView.findViewById(R.id.txt_parent_item);
convertView.setTag(parentHolder);
}else{
parentHolder=(ParentHolder) convertView.getTag();
}
parentHolder.parentTv.setText(placesList.get(groupPosition).getName());
return convertView;
}
@Override
public boolean hasStableIds() {
return false;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return false;
}
private class ParentHolder {
TextView parentTv;
}
private class ChildHolder {
TextView childTv;
}
}
Модель мест:
public class Places {
private String name;
private String [] cities;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String[] getCities() {
return cities;
}
public void setCities(String[] cities) {
this.cities = cities;
}
}
activity_expandable_list_demo.xml
<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" >
<ExpandableListView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/places_listview"
android:groupIndicator="@drawable/arrow_selector"
></ExpandableListView>
</RelativeLayout>
child_item.xml
<?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" >
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/txt_chid_item"/>
</RelativeLayout>
parent_item.xml
<?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" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/txt_parent_item"/>
</RelativeLayout>
AndroidManifest.xml
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="15" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".ExpandableListDemo"
android:label="@string/title_activity_expandable_list_demo" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
(используйте значок выше в вашем рисовании)
Я надеюсь, что этот код будет более полезным для реализации расширяемого представления списка