[ 转]Android折叠列表 ExpandableList

本文转自:http://lovezhou.iteye.com/blog/848744

ExpandableList 是折叠列表,通过继承ExpandableListActivity 类就可以非常简单的实现折叠列表。
效果图:


代码实现
Java代码 复制代码 收藏代码
  1. package com.zhou.activity;   
  2.   
  3. import android.app.ExpandableListActivity;   
  4. import android.os.Bundle;   
  5. import android.view.ContextMenu;   
  6. import android.view.Gravity;   
  7. import android.view.MenuItem;   
  8. import android.view.View;   
  9. import android.view.ViewGroup;   
  10. import android.view.ContextMenu.ContextMenuInfo;   
  11. import android.widget.AbsListView;   
  12. import android.widget.BaseExpandableListAdapter;   
  13. import android.widget.ExpandableListAdapter;   
  14. import android.widget.ExpandableListView;   
  15. import android.widget.TextView;   
  16. import android.widget.Toast;   
  17. import android.widget.ExpandableListView.ExpandableListContextMenuInfo;   
  18.   
  19. public class ExpandableList extends ExpandableListActivity {   
  20.     //声明adapter   
  21.     private ExpandableListAdapter mAdapter;   
  22.     @Override  
  23.     protected void onCreate(Bundle savedInstanceState) {   
  24.         super.onCreate(savedInstanceState);   
  25.         //设这标题   
  26.         setTitle("可扩展的列表");   
  27.         //实例化adapter   
  28.         mAdapter = new MyExpandableListAdapter();   
  29.         //为列表设置adapter   
  30.         setListAdapter(mAdapter);   
  31.         //为list注册context菜单   
  32.         registerForContextMenu(this.getExpandableListView());   
  33.     }   
  34.        
  35.        
  36.     @Override  
  37.     public boolean onChildClick(ExpandableListView parent, View v,   
  38.             int groupPosition, int childPosition, long id) {   
  39.         Toast.makeText(this" 组元素索引: " + groupPosition + " 子元素索引: " + childPosition, Toast.LENGTH_SHORT).show();   
  40.         return super.onChildClick(parent, v, groupPosition, childPosition, id);   
  41.     }   
  42.   
  43.   
  44.     @Override  
  45.     public void onGroupExpand(int groupPosition) {   
  46.         Toast.makeText(this" 组元素索引: " + groupPosition, Toast.LENGTH_SHORT).show();   
  47.         super.onGroupExpand(groupPosition);   
  48.     }   
  49.   
  50.     @Override  
  51.     public void onCreateContextMenu(ContextMenu menu, View v,   
  52.             ContextMenuInfo menuInfo) {   
  53.         menu.setHeaderTitle("上下文菜单");   
  54.         menu.add(000"单击我");   
  55.     }   
  56.   
  57.     // 单击上下文菜单后的逻辑   
  58.     @Override  
  59.     public boolean onContextItemSelected(MenuItem item) {   
  60.         ExpandableListContextMenuInfo info = (ExpandableListContextMenuInfo) item.getMenuInfo();   
  61.         String title = ((TextView) info.targetView).getText().toString();   
  62.   
  63.         int type = ExpandableListView.getPackedPositionType(info.packedPosition);   
  64.         if (type == ExpandableListView.PACKED_POSITION_TYPE_CHILD) {   
  65.             int groupPos = ExpandableListView.getPackedPositionGroup(info.packedPosition);   
  66.             int childPos = ExpandableListView.getPackedPositionChild(info.packedPosition);   
  67.             Toast.makeText(this, title + " 组元素索引: " + groupPos + " 子元素索引: " + childPos, Toast.LENGTH_SHORT).show();   
  68.             return true;   
  69.         } else if (type == ExpandableListView.PACKED_POSITION_TYPE_GROUP) {   
  70.             int groupPos = ExpandableListView.getPackedPositionGroup(info.packedPosition);   
  71.             Toast.makeText(this, title + " 组元素索引: " + groupPos, Toast.LENGTH_SHORT).show();   
  72.             return true;   
  73.         }   
  74.         return false;   
  75.     }   
  76.     //自定义Adapter   
  77.     public class MyExpandableListAdapter extends BaseExpandableListAdapter {   
  78.         // 父列表数据   
  79.         private String[] groups =    
  80.         {    
  81.             "吉林省",    
  82.             "辽宁省",    
  83.             "黑龙江省",   
  84.             "山东省"    
  85.         };   
  86.         // 子列表数据   
  87.         private String[][] children =    
  88.         {   
  89.             { "长春市" },   
  90.             { "沈阳市""铁岭市" },   
  91.             { "哈尔滨市""齐齐哈尔市""牡丹江市" },   
  92.             { "济南市""青岛市""淄博市""潍坊市" }   
  93.         };   
  94.         @Override  
  95.         public Object getChild(int groupPosition, int childPosition) {   
  96.             return children[groupPosition][childPosition];   
  97.         }   
  98.         @Override  
  99.         public long getChildId(int groupPosition, int childPosition) {   
  100.             return childPosition;   
  101.         }   
  102.         @Override  
  103.         public int getChildrenCount(int groupPosition) {   
  104.             return children[groupPosition].length;   
  105.         }   
  106.         // 取子列表中的某一项的 View   
  107.         @Override  
  108.         public View getChildView(int groupPosition, int childPosition,   
  109.                 boolean isLastChild, View convertView, ViewGroup parent) {   
  110.             TextView textView = getGenericView();   
  111.             textView.setText(getChild(groupPosition, childPosition).toString());   
  112.             return textView;   
  113.         }   
  114.         @Override  
  115.         public Object getGroup(int groupPosition) {   
  116.             return groups[groupPosition];   
  117.         }   
  118.         @Override  
  119.         public int getGroupCount() {   
  120.             return groups.length;   
  121.         }   
  122.         @Override  
  123.         public long getGroupId(int groupPosition) {   
  124.             return groupPosition;   
  125.         }   
  126.         // 取父列表中的某一项的 View   
  127.         @Override  
  128.         public View getGroupView(int groupPosition, boolean isExpanded,   
  129.                 View convertView, ViewGroup parent) {   
  130.             TextView textView = getGenericView();   
  131.             textView.setText(getGroup(groupPosition).toString());   
  132.             return textView;   
  133.         }   
  134.         @Override  
  135.         public boolean hasStableIds() {   
  136.             return true;   
  137.         }   
  138.         @Override  
  139.         public boolean isChildSelectable(int groupPosition, int childPosition) {   
  140.             return true;   
  141.         }   
  142.         // 获取某一项的 View 的逻辑   
  143.         private TextView getGenericView() {   
  144.             AbsListView.LayoutParams lp = new AbsListView.LayoutParams(   
  145.                     ViewGroup.LayoutParams.FILL_PARENT, 48);   
  146.             TextView textView = new TextView(ExpandableList.this);   
  147.             textView.setLayoutParams(lp);   
  148.             textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);   
  149.             textView.setPadding(32000);   
  150.             return textView;   
  151.         }   
  152.     }   
  153. }  
package com.zhou.activity;

import android.app.ExpandableListActivity;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.Gravity;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.ContextMenu.ContextMenuInfo;
import android.widget.AbsListView;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ExpandableListView.ExpandableListContextMenuInfo;

public class ExpandableList extends ExpandableListActivity {
	//声明adapter
	private ExpandableListAdapter mAdapter;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		//设这标题
		setTitle("可扩展的列表");
		//实例化adapter
		mAdapter = new MyExpandableListAdapter();
		//为列表设置adapter
		setListAdapter(mAdapter);
		//为list注册context菜单
		registerForContextMenu(this.getExpandableListView());
	}
	
	
	@Override
	public boolean onChildClick(ExpandableListView parent, View v,
			int groupPosition, int childPosition, long id) {
		Toast.makeText(this, " 组元素索引: " + groupPosition + " 子元素索引: " + childPosition, Toast.LENGTH_SHORT).show();
		return super.onChildClick(parent, v, groupPosition, childPosition, id);
	}


	@Override
	public void onGroupExpand(int groupPosition) {
		Toast.makeText(this, " 组元素索引: " + groupPosition, Toast.LENGTH_SHORT).show();
		super.onGroupExpand(groupPosition);
	}

	@Override
	public void onCreateContextMenu(ContextMenu menu, View v,
			ContextMenuInfo menuInfo) {
		menu.setHeaderTitle("上下文菜单");
		menu.add(0, 0, 0, "单击我");
	}

	// 单击上下文菜单后的逻辑
	@Override
	public boolean onContextItemSelected(MenuItem item) {
		ExpandableListContextMenuInfo info = (ExpandableListContextMenuInfo) item.getMenuInfo();
		String title = ((TextView) info.targetView).getText().toString();

		int type = ExpandableListView.getPackedPositionType(info.packedPosition);
		if (type == ExpandableListView.PACKED_POSITION_TYPE_CHILD) {
			int groupPos = ExpandableListView.getPackedPositionGroup(info.packedPosition);
			int childPos = ExpandableListView.getPackedPositionChild(info.packedPosition);
			Toast.makeText(this, title + " 组元素索引: " + groupPos + " 子元素索引: " + childPos, Toast.LENGTH_SHORT).show();
			return true;
		} else if (type == ExpandableListView.PACKED_POSITION_TYPE_GROUP) {
			int groupPos = ExpandableListView.getPackedPositionGroup(info.packedPosition);
			Toast.makeText(this, title + " 组元素索引: " + groupPos, Toast.LENGTH_SHORT).show();
			return true;
		}
		return false;
	}
	//自定义Adapter
	public class MyExpandableListAdapter extends BaseExpandableListAdapter {
		// 父列表数据
		private String[] groups = 
		{ 
			"吉林省", 
			"辽宁省", 
			"黑龙江省",
			"山东省" 
		};
		// 子列表数据
		private String[][] children = 
		{
			{ "长春市" },
			{ "沈阳市", "铁岭市" },
			{ "哈尔滨市", "齐齐哈尔市", "牡丹江市" },
			{ "济南市", "青岛市", "淄博市", "潍坊市" }
		};
		@Override
		public Object getChild(int groupPosition, int childPosition) {
			return children[groupPosition][childPosition];
		}
		@Override
		public long getChildId(int groupPosition, int childPosition) {
			return childPosition;
		}
		@Override
		public int getChildrenCount(int groupPosition) {
			return children[groupPosition].length;
		}
		// 取子列表中的某一项的 View
		@Override
		public View getChildView(int groupPosition, int childPosition,
				boolean isLastChild, View convertView, ViewGroup parent) {
			TextView textView = getGenericView();
			textView.setText(getChild(groupPosition, childPosition).toString());
			return textView;
		}
		@Override
		public Object getGroup(int groupPosition) {
			return groups[groupPosition];
		}
		@Override
		public int getGroupCount() {
			return groups.length;
		}
		@Override
		public long getGroupId(int groupPosition) {
			return groupPosition;
		}
		// 取父列表中的某一项的 View
		@Override
		public View getGroupView(int groupPosition, boolean isExpanded,
				View convertView, ViewGroup parent) {
			TextView textView = getGenericView();
			textView.setText(getGroup(groupPosition).toString());
			return textView;
		}
		@Override
		public boolean hasStableIds() {
			return true;
		}
		@Override
		public boolean isChildSelectable(int groupPosition, int childPosition) {
			return true;
		}
		// 获取某一项的 View 的逻辑
		private TextView getGenericView() {
			AbsListView.LayoutParams lp = new AbsListView.LayoutParams(
					ViewGroup.LayoutParams.FILL_PARENT, 48);
			TextView textView = new TextView(ExpandableList.this);
			textView.setLayoutParams(lp);
			textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
			textView.setPadding(32, 0, 0, 0);
			return textView;
		}
	}
}
  • 大小: 17.2 KB

posted on 2013-04-07 10:31  freeliver54  阅读(1261)  评论(0编辑  收藏  举报

导航