一个简单的小例子:
可以展开的ListView,和Listview差不多,只是设置的Adapter不同。常用的Adapter有BaseExpandableListAdapter、SimpleExpandableListAdapter、SimpleCursorTreeAdapter
布局如下:(布局中我设置了android:groupIndicator,不知道为什么不起作用。另外,android:dividerHeight这个属性是组对象和子节点共用的。如果要定义比较复杂的组视图及子节点视图,还是单独设置一个layout文件,再在Adapter中设置相应的返回View视图)
1 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 xmlns:tools="http://schemas.android.com/tools" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:paddingBottom="@dimen/activity_vertical_margin" 6 android:paddingLeft="@dimen/activity_horizontal_margin" 7 android:paddingRight="@dimen/activity_horizontal_margin" 8 android:paddingTop="@dimen/activity_vertical_margin" 9 tools:context="com.example.testandroid.ui.MainActivity" > 10 11 <ExpandableListView 12 android:id="@+id/main_elv1" 13 android:layout_width="match_parent" 14 android:layout_height="wrap_content" 15 android:layout_alignParentLeft="true" 16 android:layout_alignParentTop="true" 17 android:childDivider="@color/white" 18 android:divider="@color/gray" 19 android:dividerHeight="1px" 20 android:groupIndicator="@drawable/arrow_triangle_down" 21 android:indicatorLeft="4dp" 22 android:indicatorRight="4dp" > 23 </ExpandableListView> 24 25 </RelativeLayout>
主Activity如下:
1 package com.example.testandroid.ui; 2 3 import android.os.Bundle; 4 import android.support.v7.app.ActionBarActivity; 5 import android.view.Menu; 6 import android.view.MenuItem; 7 import android.widget.ExpandableListView; 8 9 import com.example.testandroid.R; 10 import com.example.testandroid.adapter.MainActivityExLVAdapter; 11 12 public class MainActivity extends ActionBarActivity { 13 14 private ExpandableListView mExpandableListView = null; 15 private MainActivityExLVAdapter mMainActivityExLVAdapter = null; 16 17 @Override 18 protected void onCreate(Bundle savedInstanceState) { 19 super.onCreate(savedInstanceState); 20 setContentView(R.layout.activity_main); 21 init(); 22 } 23 24 private void init() { 25 mExpandableListView = (ExpandableListView) findViewById(R.id.main_elv1); 26 mMainActivityExLVAdapter = new MainActivityExLVAdapter(this); 27 mExpandableListView.setAdapter(mMainActivityExLVAdapter); 28 } 29 30 @Override 31 public boolean onCreateOptionsMenu(Menu menu) { 32 getMenuInflater().inflate(R.menu.main, menu); 33 return true; 34 } 35 36 @Override 37 public boolean onOptionsItemSelected(MenuItem item) { 38 int id = item.getItemId(); 39 if (id == R.id.action_settings) { 40 return true; 41 } 42 return super.onOptionsItemSelected(item); 43 } 44 }
Adapter我写到另外一个类文件中:
1 package com.example.testandroid.adapter; 2 3 import android.content.Context; 4 import android.view.View; 5 import android.view.ViewGroup; 6 import android.widget.BaseExpandableListAdapter; 7 import android.widget.TextView; 8 9 public class MainActivityExLVAdapter extends BaseExpandableListAdapter { 10 11 private Context mContext = null; 12 13 private String[] mGroup = { "FirstOne", "SecondTwo", "ThirdThress" }; 14 private String[][] mChildren = { 15 { "2014-year-01-month-01", "2014-year-01-month-02", 16 "2014-year-01-month-03", "2014-year-01-month-04", 17 "2014-year-01-month-05" }, 18 { "2014-year-02-month-01", "2014-year-02-month-02", 19 "2014-year-02-month-03", "2014-year-02-month-04" }, 20 { "2014-year-03-month-01", "2014-year-03-month-02", } }; 21 22 public MainActivityExLVAdapter(Context mContext) { 23 this.mContext = mContext; 24 } 25 26 /** 27 * 获取组的数量 28 */ 29 @Override 30 public int getGroupCount() { 31 return mGroup.length; 32 } 33 34 /** 35 * 获取组下的节点数量 36 */ 37 @Override 38 public int getChildrenCount(int groupPosition) { 39 return mChildren[groupPosition].length; 40 } 41 42 /** 43 * 获取组对象(这个方法可以返回任意对象,或者null都行,给自己调用的) 44 */ 45 @Override 46 public Object getGroup(int groupPosition) { 47 return mGroup[groupPosition]; 48 } 49 50 /** 51 * 获取子节点对象(这个方法可以返回任意对象,或者null都行,给自己调用的) 52 */ 53 @Override 54 public Object getChild(int groupPosition, int childPosition) { 55 return mChildren[groupPosition][childPosition]; 56 } 57 58 /** 59 * 给自己调用的 60 */ 61 @Override 62 public long getGroupId(int groupPosition) { 63 return groupPosition; 64 } 65 66 /** 67 * 给自己调用的 68 */ 69 @Override 70 public long getChildId(int groupPosition, int childPosition) { 71 return childPosition; 72 } 73 74 /** 75 * TODO 不知道有什么用 76 */ 77 @Override 78 public boolean hasStableIds() { 79 return true; 80 } 81 82 /** 83 * 返回组对象的视图 84 */ 85 @Override 86 public View getGroupView(int groupPosition, boolean isExpanded, 87 View convertView, ViewGroup parent) { 88 TextView mTextView = new TextView(mContext); 89 mTextView.setText(getGroup(groupPosition).toString()); 90 return mTextView; 91 } 92 93 /** 94 * 返回子节点的视图 95 */ 96 @Override 97 public View getChildView(int groupPosition, int childPosition, 98 boolean isLastChild, View convertView, ViewGroup parent) { 99 TextView mTextView = new TextView(mContext); 100 mTextView.setText(getChild(groupPosition, childPosition).toString()); 101 return mTextView; 102 } 103 104 @Override 105 public boolean isChildSelectable(int groupPosition, int childPosition) { 106 return false; 107 } 108 109 }