7.20. ExpandableListView(树形组件)
需结合适配器BaseExpandableListAdapter完成。
示例:
配置:activity_expandable_list_view.xml
<?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/elistview" android:layout_width="fill_parent" android:layout_height="wrap_content" /> </LinearLayout>
|
Activity:
package com.makyan.demo; import android.app.Activity; import android.os.Bundle; import android.view.ContextMenu; import android.view.ContextMenu.ContextMenuInfo; import android.view.View; import android.widget.ExpandableListAdapter; import android.widget.ExpandableListView; import android.widget.ExpandableListView.OnChildClickListener; import android.widget.ExpandableListView.OnGroupClickListener; import android.widget.ExpandableListView.OnGroupCollapseListener; import android.widget.ExpandableListView.OnGroupExpandListener; import android.widget.Toast; public class ExpandableListViewActivity extends Activity { private ExpandableListView elistview = null; // 定义树型组件 private ExpandableListAdapter adapter = null; // 定义适配器对象 @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); super.setContentView(R.layout.activity_expandable_list_view); // 默认布局管理器 elistview = (ExpandableListView) super.findViewById(R.id.elistview); // 取得组件 adapter = new MyExpandableListAdapter(this); // 实例化适配器 elistview.setAdapter(adapter); // 设置适配器 |