1/* 2 * Copyright (C) 2007 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17package android.widget.expandablelistview; 18 19import com.android.frameworks.coretests.R; 20 21import android.app.Activity; 22import android.os.Bundle; 23import android.view.Gravity; 24import android.view.View; 25import android.view.ViewGroup; 26import android.widget.AbsListView; 27import android.widget.BaseExpandableListAdapter; 28import android.widget.ExpandableListView; 29import android.widget.TextView; 30 31public class InflatedExpandableListView extends Activity { 32 33 @Override 34 protected void onCreate(Bundle savedInstanceState) { 35 super.onCreate(savedInstanceState); 36 37 setContentView(R.layout.inflated_expandablelistview); 38 39 ExpandableListView elv = (ExpandableListView) findViewById(R.id.elv); 40 elv.setAdapter(new MyExpandableListAdapter()); 41 } 42 43 public class MyExpandableListAdapter extends BaseExpandableListAdapter { 44 // Sample data set. children[i] contains the children (String[]) for groups[i]. 45 private String[] groups = { "People Names", "Dog Names", "Cat Names", "Fish Names" }; 46 private String[][] children = { 47 { "Arnold", "Barry", "Chuck", "David" }, 48 { "Ace", "Bandit", "Cha-Cha", "Deuce" }, 49 { "Fluffy", "Snuggles" }, 50 { "Goldy", "Bubbles" } 51 }; 52 53 public Object getChild(int groupPosition, int childPosition) { 54 return children[groupPosition][childPosition]; 55 } 56 57 public long getChildId(int groupPosition, int childPosition) { 58 return childPosition; 59 } 60 61 public int getChildrenCount(int groupPosition) { 62 return children[groupPosition].length; 63 } 64 65 public TextView getGenericView() { 66 // Layout parameters for the ExpandableListView 67 AbsListView.LayoutParams lp = new AbsListView.LayoutParams( 68 ViewGroup.LayoutParams.MATCH_PARENT, 64); 69 70 TextView textView = new TextView(InflatedExpandableListView.this); 71 textView.setLayoutParams(lp); 72 // Center the text vertically 73 textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT); 74 // Set the text starting position 75 textView.setPadding(36, 0, 0, 0); 76 return textView; 77 } 78 79 public View getChildView(int groupPosition, int childPosition, boolean isLastChild, 80 View convertView, ViewGroup parent) { 81 TextView textView = getGenericView(); 82 textView.setText(getChild(groupPosition, childPosition).toString()); 83 return textView; 84 } 85 86 public Object getGroup(int groupPosition) { 87 return groups[groupPosition]; 88 } 89 90 public int getGroupCount() { 91 return groups.length; 92 } 93 94 public long getGroupId(int groupPosition) { 95 return groupPosition; 96 } 97 98 public View getGroupView(int groupPosition, boolean isExpanded, View convertView, 99 ViewGroup parent) { 100 TextView textView = getGenericView(); 101 textView.setText(getGroup(groupPosition).toString()); 102 return textView; 103 } 104 105 public boolean isChildSelectable(int groupPosition, int childPosition) { 106 return true; 107 } 108 109 public boolean hasStableIds() { 110 return true; 111 } 112 113 } 114 115} 116