Android ExpandableListView 可展开的ListView – 打鱼还是晒网 – 博客频道 – CSDN.NET

效果图

public class TestExpandableListView extends Activity {</p><p>	ExpandableListView mView;</p><p>	@Override</p><p>	protected void onCreate(Bundle savedInstanceState) {</p><p>		super.onCreate(savedInstanceState);</p><p>		setContentView(R.layout.expandable_<b style="color:black;background-color:#99ff99">listview</b>);</p><p>		</p><p>		mView = (ExpandableListView) findViewById(R.id.el_list);</p><p>		mView.setAdapter(new MyAdapter());</p><p>		</p><p>	}</p><p>	</p><p>	class MyAdapter extends BaseExpandableListAdapter {</p><p>		</p><p>		 //设置组视图的图片</p><p>//        int[] logos = new int[] { R.drawable.wei, R.drawable.shu,R.drawable.wu};</p><p>        //设置组视图的显示文字</p><p>        private String[] generalsTypes = new String[] { "魏", "蜀", "吴" };</p><p>        //子视图显示文字</p><p>        private String[][] generals = new String[][] {</p><p>                { "夏侯惇", "甄姬", "许褚", "郭嘉", "司马懿", "杨修" },</p><p>                { "马超", "张飞", "刘备", "诸葛亮", "黄月英", "赵云" },</p><p>                { "吕蒙", "陆逊", "孙权", "周瑜", "孙尚香" }</p><p>        };</p><p>        </p><p>        //子视图图片</p><p>//        public int[][] generallogos = new int[][] {</p><p>//                { R.drawable.xiahoudun, R.drawable.zhenji,</p><p>//                        R.drawable.xuchu, R.drawable.guojia,</p><p>//                        R.drawable.simayi, R.drawable.yangxiu },</p><p>//                { R.drawable.machao, R.drawable.zhangfei,</p><p>//                        R.drawable.liubei, R.drawable.zhugeliang,</p><p>//                        R.drawable.huangyueying, R.drawable.zhaoyun },</p><p>//                { R.drawable.lvmeng, R.drawable.luxun, R.drawable.sunquan,</p><p>//                        R.drawable.zhouyu, R.drawable.sunshangxiang } };</p><p>        //自己定义一个获得textview的方法</p><p>        TextView getTextView() {</p><p>            AbsListView.LayoutParams lp = new AbsListView.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, 100);</p><p>            TextView textView = new TextView(TestExpandableListView.this);</p><p>            textView.setLayoutParams(lp);</p><p>            textView.setGravity(Gravity.CENTER_VERTICAL);</p><p>            textView.setPadding(36, 0, 0, 0);</p><p>            textView.setTextSize(20);</p><p>            textView.setTextColor(Color.BLACK);</p><p>            return textView;</p><p>        }</p><p>		@Override</p><p>		public int getGroupCount() {</p><p>			return generalsTypes.length;</p><p>		}</p><p>		@Override</p><p>		public int getChildrenCount(int groupPosition) {</p><p>			return generals[groupPosition].length;</p><p>		}</p><p>		@Override</p><p>		public Object getGroup(int groupPosition) {</p><p>			return generalsTypes[groupPosition];</p><p>		}</p><p>		@Override</p><p>		public Object getChild(int groupPosition, int childPosition) {</p><p>			return generals[groupPosition][childPosition];</p><p>		}</p><p>		@Override</p><p>		public long getGroupId(int groupPosition) {</p><p>			return groupPosition;</p><p>		}</p><p>		@Override</p><p>		public long getChildId(int groupPosition, int childPosition) {</p><p>			return childPosition;</p><p>		}</p><p>		@Override</p><p>		public boolean hasStableIds() {</p><p>			return true;</p><p>		}</p><p>		@Override</p><p>		public View getGroupView(int groupPosition, boolean isExpanded,</p><p>				View convertView, ViewGroup parent) {</p><p>			 LinearLayout ll = new LinearLayout(TestExpandableListView.this);</p><p>             ll.setOrientation(0);</p><p>//             ImageView logo = new ImageView(ExpandableList.this);</p><p>//             logo.setImageResource(logos[groupPosition]);</p><p>//             logo.setPadding(50, 0, 0, 0);</p><p>//             ll.addView(logo);</p><p>             TextView textView = getTextView();</p><p>             textView.setTextColor(Color.BLUE);</p><p>             textView.setText(getGroup(groupPosition).toString());</p><p>             ll.addView(textView);</p><p>             ll.setPadding(100, 10, 10, 10);</p><p>             return ll;</p><p>		}</p><p>		@Override</p><p>		public View getChildView(int groupPosition, int childPosition,</p><p>				boolean isLastChild, View convertView, ViewGroup parent) {</p><p>			 LinearLayout ll = new LinearLayout(TestExpandableListView.this);</p><p>             ll.setOrientation(0);</p><p>//             ImageView generallogo = new ImageView(TestExpandableListView.this);</p><p>//             generallogo.setImageResource(generallogos[groupPosition][childPosition]);</p><p>//             ll.addView(generallogo);</p><p>             TextView textView = getTextView();</p><p>             textView.setText(getChild(groupPosition, childPosition).toString());</p><p>             ll.addView(textView);</p><p>             return ll;</p><p>		}</p><p>		@Override</p><p>		public boolean isChildSelectable(int groupPosition, int childPosition) {</p><p>			return true;</p><p>		}</p><p>		</p><p>	}</p><p>}</p><p>

expandable_listview.xml

&lt;?xml version="1.0" encoding="utf-8"?&gt;</p><p>&lt;LinearLayout xmlns:<b style="color:black;background-color:#ffff66">android</b>="http://schemas.<b style="color:black;background-color:#ffff66">android</b>.com/apk/res/<b style="color:black;background-color:#ffff66">android</b>"</p><p>    <b style="color:black;background-color:#ffff66">android</b>:layout_width="fill_parent"</p><p>    <b style="color:black;background-color:#ffff66">android</b>:layout_height="fill_parent"</p><p>    <b style="color:black;background-color:#ffff66">android</b>:orientation="vertical" &gt;</p><p>    &lt;ExpandableListView </p><p>        <b style="color:black;background-color:#ffff66">android</b>:id="@+id/el_list"</p><p>        <b style="color:black;background-color:#ffff66">android</b>:layout_width="fill_parent"</p><p>        <b style="color:black;background-color:#ffff66">android</b>:layout_height="fill_parent"</p><p>        <b style="color:black;background-color:#ffff66">android</b>:background="#ffffff"</p><p>        <b style="color:black;background-color:#ffff66">android</b>:cacheColorHint="#abcdefff"</p><p>        <b style="color:black;background-color:#ffff66">android</b>:listSelector="#cdefabdc" </p><p>        &gt;</p><p>  &lt;!--       <b style="color:black;background-color:#ffff66">android</b>:cacheColorHint="#00000000",这个设置拖动view时背景色</p><p>		<b style="color:black;background-color:#ffff66">android</b>:listSelector="#00000000" ,设置选中时的底色 --&gt;</p><p>    &lt;/ExpandableListView&gt; </p><p>&lt;/LinearLayout&gt;

一些方法说明:

1.  ExpandableListAdapter 

     group和child都有getView方法,即可使用不同的布局,ViewHolder等

     在getView方法的,groupPosition和childPosition 即在两个集合中的位置

2. ExpandableListView 

    getFirstVisiblePosition();

    getLastVisiblePosition();   //返回屏幕最后可见item的位置: 只计算可见view的位置(不含被group 收缩的子view)

   getPackedPositionGroup(getExpandableListPosition(getFirstVisiblePosition()); //返回 首可见项所在group位置

   setOnChildClickListener()   child点击

   setOnGroupClickListener()  group点击

来源URL:http://cache.baiducontent.com/c?m=9d78d513d9901df918b0cf281a16a6716f53971337c0a06168a7e05f9214051a0620a7e070670443939b733d47e90b4beb832b6f675d7de28cc8ff1b9cedce3f2fff7b633601de19579646f1894022c53dc31aaff448b9eded64c4e8818881154eca50057086f58159&p=9e7cc21b9c904ead0cbd9b7e0e1581&newp=8b2a975e86cc41ac5ba6cd31500097231610db2151d7d21264&user=baidu&fm=sc&query=android+%D5%B9%BF%AAlistview&qid=a9d6767c000331ee&p1=1