http://blog.csdn.net/hn307165411/article/details/7173284
有时在写程序时,我们希望一个listview能展开其下的子类目,在android中可以通过使用ExpandAbleListView来实现,只需要在代码里为ExpandAbleListView设置一个ExpandAbleLIstAdapter的数据源即可。
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- >
- <ExpandableListView
- android:id="@+id/list"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:childIndicator="@drawable/icon"
- ></ExpandableListView>
- </LinearLayout>
- public class ExpandableListViewTest extends Activity
- {
- @Override
- public void onCreate(Bundle savedInstanceState)
- {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- //创建一个BaseExpandableListAdapter对象
- ExpandableListAdapter adapter = new BaseExpandableListAdapter()
- {
- int[] logos = new int[]
- {
- R.drawable.sunyz_1,
- R.drawable.sunyz_6,
- R.drawable.sunyz_7
- };
- private String[] CDs = new String[]
- { "风筝", "完美的一天", "是时候"};
- private String[][] songs = new String[][]
- {
- { "绿光", "不是真的爱我", "爱情字典", "练习" },
- { "Honey Honey", "心愿", "明天晴天", "隐形人" },
- { "愚人的国度", "是时候" , "世说心语" }
- };
- //获取指定组位置、指定子列表项处的子列表项数据
- @Override
- public Object getChild(int groupPosition, int childPosition)
- {
- return songs[groupPosition][childPosition];
- }
- @Override
- public long getChildId(int groupPosition, int childPosition)
- {
- return childPosition;
- }
- @Override
- public int getChildrenCount(int groupPosition)
- {
- return songs[groupPosition].length;
- }
- private TextView getTextView()
- {
- //用于实现条目的虚拟列表的基类. 这里的列表没有空间的定义。 例如,该类的子类可以以网格的形式、走马灯的形式显示,或者作为堆栈等等
- AbsListView.LayoutParams lp = new AbsListView.LayoutParams(
- ViewGroup.LayoutParams.FILL_PARENT, 64); //设置宽和高
- TextView textView = new TextView(ExpandableListViewTest.this);
- textView.setLayoutParams(lp);
- textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
- textView.setPadding(36, 0, 0, 0);
- textView.setTextSize(20);
- return textView;
- }
- //该方法决定每个子选项的外观
- @Override
- public View getChildView(int groupPosition, int childPosition,
- boolean isLastChild, View convertView, ViewGroup parent)
- {
- TextView textView = getTextView();
- textView.setText(getChild(groupPosition, childPosition).toString());
- return textView;
- }
- //获取指定组位置处的组数据
- @Override
- public Object getGroup(int groupPosition)
- {
- return CDs[groupPosition];
- }
- @Override
- public int getGroupCount()
- {
- return CDs.length;
- }
- @Override
- public long getGroupId(int groupPosition)
- {
- return groupPosition;
- }
- //该方法决定每个组选项的外观
- @Override
- public View getGroupView(int groupPosition, boolean isExpanded,
- View convertView, ViewGroup parent)
- {
- LinearLayout ll = new LinearLayout(ExpandableListViewTest.this);
- ll.setOrientation(0);
- ImageView logo = new ImageView(ExpandableListViewTest.this);
- logo.setImageResource(logos[groupPosition]);
- ll.addView(logo);
- TextView textView = getTextView();
- textView.setText(getGroup(groupPosition).toString());
- ll.addView(textView);
- return ll;
- }
- @Override
- public boolean isChildSelectable(int groupPosition, int childPosition)
- {
- return true;
- }
- @Override
- public boolean hasStableIds()
- {
- return true;
- }
- };
- ExpandableListView expandListView = (ExpandableListView)
- findViewById(R.id.list);
- expandListView.setAdapter(adapter);
- }
- }