ZoomControls,PopupWindow,ExpandableListView

一、缩放控制: ZoomControls

Mian.xml

Xml代码 复制代码  收藏代码
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent"  
  5.     android:orientation="vertical" >  
  6.   
  7.     <TextView  
  8.         android:id="@+id/text"  
  9.         android:layout_width="wrap_content"  
  10.         android:layout_height="wrap_content"  
  11.         android:text="你好"  
  12.         android:textSize="10px" />  
  13.   
  14.     <ZoomControls  
  15.         android:id="@+id/zommcontrols"  
  16.         android:layout_width="wrap_content"  
  17.         android:layout_height="wrap_content"  
  18.         android:gravity="bottom" />  
  19.   
  20. </LinearLayout>  
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:orientation="vertical" >    <TextView        android:id="@+id/text"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="你好"        android:textSize="10px" />    <ZoomControls        android:id="@+id/zommcontrols"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:gravity="bottom" /></LinearLayout>
 

 

ZoomControlsActivity.java

Java代码 复制代码  收藏代码
  1. package com.iflytek.demo;   
  2.   
  3. import android.app.Activity;   
  4. import android.os.Bundle;   
  5. import android.view.View;   
  6. import android.view.View.OnClickListener;   
  7. import android.widget.TextView;   
  8. import android.widget.ZoomControls;   
  9.   
  10. public class ZoomControlsActivity extends Activity {   
  11.     private ZoomControls zoomcontrols = null;   
  12.     private TextView text = null;   
  13.     private int size = 10; // 是保存文字大小   
  14.   
  15.     /** Called when the activity is first created. */  
  16.     @Override  
  17.     public void onCreate(Bundle savedInstanceState) {   
  18.         super.onCreate(savedInstanceState);   
  19.         setContentView(R.layout.main);   
  20.         this.zoomcontrols = (ZoomControls) super  
  21.                 .findViewById(R.id.zommcontrols);   
  22.         this.text = (TextView) super.findViewById(R.id.text);   
  23.   
  24.         this.zoomcontrols   
  25.                 .setOnZoomInClickListener(new OnZoomInClickListenerImpl());   
  26.         this.zoomcontrols   
  27.                 .setOnZoomOutClickListener(new OnZoomOutClickListenerImpl());   
  28.     }   
  29.   
  30.     private class OnZoomInClickListenerImpl implements OnClickListener {   
  31.   
  32.         @Override  
  33.         public void onClick(View v) {   
  34.             ZoomControlsActivity.this.size = size + 2;   
  35.             ZoomControlsActivity.this.text.setTextSize(size);   
  36.         }   
  37.   
  38.     }   
  39.   
  40.     private class OnZoomOutClickListenerImpl implements OnClickListener {   
  41.   
  42.         @Override  
  43.         public void onClick(View v) {   
  44.             ZoomControlsActivity.this.size = size - 2;   
  45.             ZoomControlsActivity.this.text.setTextSize(size);   
  46.         }   
  47.   
  48.     }   
  49. }  
package com.iflytek.demo;import android.app.Activity;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.widget.TextView;import android.widget.ZoomControls;public class ZoomControlsActivity extends Activity {	private ZoomControls zoomcontrols = null;	private TextView text = null;	private int size = 10; // 是保存文字大小	/** Called when the activity is first created. */	@Override	public void onCreate(Bundle savedInstanceState) {		super.onCreate(savedInstanceState);		setContentView(R.layout.main);		this.zoomcontrols = (ZoomControls) super				.findViewById(R.id.zommcontrols);		this.text = (TextView) super.findViewById(R.id.text);		this.zoomcontrols				.setOnZoomInClickListener(new OnZoomInClickListenerImpl());		this.zoomcontrols				.setOnZoomOutClickListener(new OnZoomOutClickListenerImpl());	}	private class OnZoomInClickListenerImpl implements OnClickListener {		@Override		public void onClick(View v) {			ZoomControlsActivity.this.size = size + 2;			ZoomControlsActivity.this.text.setTextSize(size);		}	}	private class OnZoomOutClickListenerImpl implements OnClickListener {		@Override		public void onClick(View v) {			ZoomControlsActivity.this.size = size - 2;			ZoomControlsActivity.this.text.setTextSize(size);		}	}}
 

 

二、弹出窗口: PopupWindow

PopupWindowActivity.java

Java代码 复制代码  收藏代码
  1. package com.iflytek.demo;   
  2.   
  3. import android.app.Activity;   
  4. import android.os.Bundle;   
  5. import android.view.Gravity;   
  6. import android.view.LayoutInflater;   
  7. import android.view.View;   
  8. import android.view.View.OnClickListener;   
  9. import android.widget.Button;   
  10. import android.widget.PopupWindow;   
  11. import android.widget.RadioButton;   
  12. import android.widget.RadioGroup;   
  13. import android.widget.RadioGroup.OnCheckedChangeListener;   
  14. import android.widget.TextView;   
  15.   
  16. public class PopupWindowActivity extends Activity {   
  17.     private Button popbut = null;   
  18.     private TextView statusinfo = null;   
  19.     private View popView = null;   
  20.     private PopupWindow popWin = null;   
  21.     private RadioGroup changestatus = null;   
  22.     private Button cancel = null;   
  23.   
  24.     /** Called when the activity is first created. */  
  25.     @Override  
  26.     public void onCreate(Bundle savedInstanceState) {   
  27.         super.onCreate(savedInstanceState);   
  28.         setContentView(R.layout.main);   
  29.         this.popbut = (Button) super.findViewById(R.id.popbut);   
  30.         this.statusinfo = (TextView) super.findViewById(R.id.statusinfo);   
  31.   
  32.         this.popbut.setOnClickListener(new OnClickListenerImpl());   
  33.     }   
  34.   
  35.     private class OnClickListenerImpl implements OnClickListener {   
  36.   
  37.         @Override  
  38.         public void onClick(View v) {   
  39.             LayoutInflater inflater = LayoutInflater   
  40.                     .from(PopupWindowActivity.this);   
  41.             PopupWindowActivity.this.popView = inflater.inflate(   
  42.                     R.layout.popwindow, null); // 找到了布局文件中的View   
  43.             PopupWindowActivity.this.popWin = new PopupWindow(   
  44.                     PopupWindowActivity.this.popView, 300220, true);   
  45.             PopupWindowActivity.this.changestatus = (RadioGroup) PopupWindowActivity.this.popView   
  46.                     .findViewById(R.id.changestatus); // 取得弹出界面中的组件   
  47.             PopupWindowActivity.this.cancel = (Button) PopupWindowActivity.this.popView   
  48.                     .findViewById(R.id.cancel);   
  49.             PopupWindowActivity.this.changestatus   
  50.                     .setOnCheckedChangeListener(new OnCheckedChangeListenerImpl());   
  51.             PopupWindowActivity.this.cancel   
  52.                     .setOnClickListener(new OnClickListener() {   
  53.   
  54.                         @Override  
  55.                         public void onClick(View v) {   
  56.                             PopupWindowActivity.this.popWin.dismiss(); // 不显示   
  57.                         }   
  58.                     });   
  59.             PopupWindowActivity.this.popWin.showAtLocation(   
  60.                     PopupWindowActivity.this.popbut, Gravity.CENTER, 00);   
  61.         }   
  62.     }   
  63.   
  64.     private class OnCheckedChangeListenerImpl implements  
  65.             OnCheckedChangeListener {   
  66.   
  67.         @Override  
  68.         public void onCheckedChanged(RadioGroup group, int checkedId) {   
  69.             RadioButton but = (RadioButton) PopupWindowActivity.this.popView   
  70.                     .findViewById(group.getCheckedRadioButtonId()); // 取得指定的单选钮被选中   
  71.             PopupWindowActivity.this.statusinfo.setText("当前用户状态:"  
  72.                     + but.getText().toString());   
  73.             PopupWindowActivity.this.popWin.dismiss();   
  74.         }   
  75.   
  76.     }   
  77. }  
package com.iflytek.demo;import android.app.Activity;import android.os.Bundle;import android.view.Gravity;import android.view.LayoutInflater;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.PopupWindow;import android.widget.RadioButton;import android.widget.RadioGroup;import android.widget.RadioGroup.OnCheckedChangeListener;import android.widget.TextView;public class PopupWindowActivity extends Activity {	private Button popbut = null;	private TextView statusinfo = null;	private View popView = null;	private PopupWindow popWin = null;	private RadioGroup changestatus = null;	private Button cancel = null;	/** Called when the activity is first created. */	@Override	public void onCreate(Bundle savedInstanceState) {		super.onCreate(savedInstanceState);		setContentView(R.layout.main);		this.popbut = (Button) super.findViewById(R.id.popbut);		this.statusinfo = (TextView) super.findViewById(R.id.statusinfo);		this.popbut.setOnClickListener(new OnClickListenerImpl());	}	private class OnClickListenerImpl implements OnClickListener {		@Override		public void onClick(View v) {			LayoutInflater inflater = LayoutInflater					.from(PopupWindowActivity.this);			PopupWindowActivity.this.popView = inflater.inflate(					R.layout.popwindow, null); // 找到了布局文件中的View			PopupWindowActivity.this.popWin = new PopupWindow(					PopupWindowActivity.this.popView, 300, 220, true);			PopupWindowActivity.this.changestatus = (RadioGroup) PopupWindowActivity.this.popView					.findViewById(R.id.changestatus); // 取得弹出界面中的组件			PopupWindowActivity.this.cancel = (Button) PopupWindowActivity.this.popView					.findViewById(R.id.cancel);			PopupWindowActivity.this.changestatus					.setOnCheckedChangeListener(new OnCheckedChangeListenerImpl());			PopupWindowActivity.this.cancel					.setOnClickListener(new OnClickListener() {						@Override						public void onClick(View v) {							PopupWindowActivity.this.popWin.dismiss(); // 不显示						}					});			PopupWindowActivity.this.popWin.showAtLocation(					PopupWindowActivity.this.popbut, Gravity.CENTER, 0, 0);		}	}	private class OnCheckedChangeListenerImpl implements			OnCheckedChangeListener {		@Override		public void onCheckedChanged(RadioGroup group, int checkedId) {			RadioButton but = (RadioButton) PopupWindowActivity.this.popView					.findViewById(group.getCheckedRadioButtonId()); // 取得指定的单选钮被选中			PopupWindowActivity.this.statusinfo.setText("当前用户状态:"					+ but.getText().toString());			PopupWindowActivity.this.popWin.dismiss();		}	}}
 

 

Main.xml

Xml代码 复制代码  收藏代码
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout    
  3.     xmlns:android="http://schemas.android.com/apk/res/android"  
  4.     android:orientation="vertical"    
  5.     android:layout_width="fill_parent"  
  6.     android:layout_height="fill_parent">  
  7.     <TextView    
  8.         android:id="@+id/statusinfo"  
  9.         android:layout_width="fill_parent"  
  10.         android:layout_height="wrap_content"    
  11.         android:text="当前用户状态:在线" />  
  12.     <Button    
  13.         android:id="@+id/popbut"  
  14.         android:layout_width="fill_parent"  
  15.         android:layout_height="wrap_content"    
  16.         android:text="状态" />  
  17. </LinearLayout>  
<?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">	<TextView 		android:id="@+id/statusinfo"		android:layout_width="fill_parent"		android:layout_height="wrap_content" 		android:text="当前用户状态:在线" />	<Button 		android:id="@+id/popbut"		android:layout_width="fill_parent"		android:layout_height="wrap_content" 		android:text="状态" /></LinearLayout>
 

 

popwindow.xml

Xml代码 复制代码  收藏代码
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout    
  3.     xmlns:android="http://schemas.android.com/apk/res/android"  
  4.     android:orientation="vertical"    
  5.     android:layout_width="fill_parent"  
  6.     android:layout_height="fill_parent">  
  7.     <TextView    
  8.         android:id="@+id/popinfo"  
  9.         android:layout_width="wrap_content"  
  10.         android:layout_height="wrap_content"    
  11.         android:text="请选择您的当前状态:"  
  12.         android:textSize="20px" />  
  13.     <RadioGroup  
  14.         android:id="@+id/changestatus"  
  15.         android:layout_width="wrap_content"  
  16.         android:layout_height="wrap_content"    
  17.         android:orientation="vertical"  
  18.         android:checkedButton="@+id/online">  
  19.         <RadioButton  
  20.             android:id="@+id/online"  
  21.             android:text="在线" />  
  22.         <RadioButton  
  23.             android:id="@+id/offline"  
  24.             android:text="离线" />  
  25.         <RadioButton  
  26.             android:id="@+id/stealth"  
  27.             android:text="隐身" />  
  28.     </RadioGroup>  
  29.     <Button    
  30.         android:id="@+id/cancel"  
  31.         android:layout_width="wrap_content"  
  32.         android:layout_height="wrap_content"    
  33.         android:text="取消"/>  
  34. </LinearLayout>  
<?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">	<TextView 		android:id="@+id/popinfo"		android:layout_width="wrap_content"		android:layout_height="wrap_content" 		android:text="请选择您的当前状态:"		android:textSize="20px" />	<RadioGroup		android:id="@+id/changestatus"		android:layout_width="wrap_content"		android:layout_height="wrap_content" 		android:orientation="vertical"		android:checkedButton="@+id/online">		<RadioButton			android:id="@+id/online"			android:text="在线" />		<RadioButton			android:id="@+id/offline"			android:text="离线" />		<RadioButton			android:id="@+id/stealth"			android:text="隐身" />	</RadioGroup>	<Button 		android:id="@+id/cancel"		android:layout_width="wrap_content"		android:layout_height="wrap_content" 		android:text="取消"/></LinearLayout>

 

三、树型组件: ExpandableListView

1 、组项监听

ExpandableListView02_Activity.java

Java代码 复制代码  收藏代码
  1. package com.iflytek.demo;   
  2.   
  3. import android.app.Activity;   
  4. import android.os.Bundle;   
  5. import android.view.View;   
  6. import android.widget.ExpandableListAdapter;   
  7. import android.widget.ExpandableListView;   
  8. import android.widget.ExpandableListView.OnChildClickListener;   
  9. import android.widget.ExpandableListView.OnGroupClickListener;   
  10. import android.widget.ExpandableListView.OnGroupCollapseListener;   
  11. import android.widget.ExpandableListView.OnGroupExpandListener;   
  12. import android.widget.Toast;   
  13.   
  14. public class ExpandableListView02_Activity extends Activity {   
  15.     private ExpandableListView elistview = null;   
  16.     private ExpandableListAdapter adapter = null;   
  17.   
  18.     /** Called when the activity is first created. */  
  19.     @Override  
  20.     public void onCreate(Bundle savedInstanceState) {   
  21.         super.onCreate(savedInstanceState);   
  22.         setContentView(R.layout.main);   
  23.         this.elistview = (ExpandableListView) super  
  24.                 .findViewById(R.id.elistview);   
  25.         this.adapter = new MyExpandableListAdapter(this);   
  26.         this.elistview.setAdapter(this.adapter);   
  27.   
  28.         this.elistview.setOnChildClickListener(new OnChildClickListenerImpl());   
  29.         this.elistview.setOnGroupClickListener(new OnGroupClickListenerImpl());   
  30.         this.elistview   
  31.                 .setOnGroupExpandListener(new OnGroupExpandListenerImpl());   
  32.         this.elistview   
  33.                 .setOnGroupCollapseListener(new OnGroupCollapseListenerImpl());   
  34.     }   
  35.   
  36.     private class OnChildClickListenerImpl implements OnChildClickListener {   
  37.   
  38.         @Override  
  39.         public boolean onChildClick(ExpandableListView parent, View v,   
  40.                 int groupPosition, int childPosition, long id) {   
  41.             Toast.makeText(   
  42.                     ExpandableListView02_Activity.this,   
  43.                     "子选项被选中,groupPosition = " + groupPosition   
  44.                             + ",childPosition = " + childPosition,   
  45.                     Toast.LENGTH_SHORT).show();   
  46.             return false;   
  47.         }   
  48.     }   
  49.   
  50.     private class OnGroupClickListenerImpl implements OnGroupClickListener {   
  51.   
  52.         @Override  
  53.         public boolean onGroupClick(ExpandableListView parent, View v,   
  54.                 int groupPosition, long id) {   
  55.             Toast.makeText(ExpandableListView02_Activity.this,   
  56.                     "分组被选中,groupPosition = " + groupPosition,   
  57.                     Toast.LENGTH_SHORT).show();   
  58.             return false;   
  59.         }   
  60.   
  61.     }   
  62.   
  63.     private class OnGroupExpandListenerImpl implements OnGroupExpandListener {   
  64.   
  65.         @Override  
  66.         public void onGroupExpand(int groupPosition) {   
  67.             Toast.makeText(ExpandableListView02_Activity.this,   
  68.                     "打开分组,groupPosition = " + groupPosition, Toast.LENGTH_SHORT)   
  69.                     .show();   
  70.   
  71.         }   
  72.   
  73.     }   
  74.   
  75.     private class OnGroupCollapseListenerImpl implements  
  76.             OnGroupCollapseListener {   
  77.   
  78.         @Override  
  79.         public void onGroupCollapse(int groupPosition) {   
  80.             Toast.makeText(ExpandableListView02_Activity.this,   
  81.                     "关闭分组,groupPosition = " + groupPosition, Toast.LENGTH_SHORT)   
  82.                     .show();   
  83.   
  84.         }   
  85.   
  86.     }   
  87. }  
package com.iflytek.demo;import android.app.Activity;import android.os.Bundle;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 ExpandableListView02_Activity extends Activity {	private ExpandableListView elistview = null;	private ExpandableListAdapter adapter = null;	/** Called when the activity is first created. */	@Override	public void onCreate(Bundle savedInstanceState) {		super.onCreate(savedInstanceState);		setContentView(R.layout.main);		this.elistview = (ExpandableListView) super				.findViewById(R.id.elistview);		this.adapter = new MyExpandableListAdapter(this);		this.elistview.setAdapter(this.adapter);		this.elistview.setOnChildClickListener(new OnChildClickListenerImpl());		this.elistview.setOnGroupClickListener(new OnGroupClickListenerImpl());		this.elistview				.setOnGroupExpandListener(new OnGroupExpandListenerImpl());		this.elistview				.setOnGroupCollapseListener(new OnGroupCollapseListenerImpl());	}	private class OnChildClickListenerImpl implements OnChildClickListener {		@Override		public boolean onChildClick(ExpandableListView parent, View v,				int groupPosition, int childPosition, long id) {			Toast.makeText(					ExpandableListView02_Activity.this,					"子选项被选中,groupPosition = " + groupPosition							+ ",childPosition = " + childPosition,					Toast.LENGTH_SHORT).show();			return false;		}	}	private class OnGroupClickListenerImpl implements OnGroupClickListener {		@Override		public boolean onGroupClick(ExpandableListView parent, View v,				int groupPosition, long id) {			Toast.makeText(ExpandableListView02_Activity.this,					"分组被选中,groupPosition = " + groupPosition,					Toast.LENGTH_SHORT).show();			return false;		}	}	private class OnGroupExpandListenerImpl implements OnGroupExpandListener {		@Override		public void onGroupExpand(int groupPosition) {			Toast.makeText(ExpandableListView02_Activity.this,					"打开分组,groupPosition = " + groupPosition, Toast.LENGTH_SHORT)					.show();		}	}	private class OnGroupCollapseListenerImpl implements			OnGroupCollapseListener {		@Override		public void onGroupCollapse(int groupPosition) {			Toast.makeText(ExpandableListView02_Activity.this,					"关闭分组,groupPosition = " + groupPosition, Toast.LENGTH_SHORT)					.show();		}	}}
 

 

MyExpandableListAdapter.java

Java代码 复制代码  收藏代码
  1. package com.iflytek.demo;   
  2.   
  3. import android.content.Context;   
  4. import android.view.Gravity;   
  5. import android.view.View;   
  6. import android.view.ViewGroup;   
  7. import android.widget.AbsListView;   
  8. import android.widget.BaseExpandableListAdapter;   
  9. import android.widget.TextView;   
  10.   
  11. public class MyExpandableListAdapter extends BaseExpandableListAdapter {   
  12.     // 一个组下肯定会有多个子选项   
  13.     private String[] groups = new String[] { "我的好友", "家人", "同事", "黑名单" }; // 组名称   
  14.     private String[][] children = new String[][] { { "赵一", "吴二", "张三", "李四" },   
  15.             { "父亲", "母亲" }, { "王五", "赵六", "王八" }, { "票贩子", "造假商" } }; // 这里面定义的子选项   
  16.     private Context context = null;   
  17.   
  18.     public MyExpandableListAdapter(Context context) {   
  19.         this.context = context;   
  20.     }   
  21.   
  22.     @Override  
  23.     public Object getChild(int groupPosition, int childPosition) { // 取得指定的子选项   
  24.         return this.children[groupPosition][childPosition];   
  25.     }   
  26.   
  27.     @Override  
  28.     public long getChildId(int groupPosition, int childPosition) {   
  29.         return childPosition;   
  30.     }   
  31.   
  32.     private TextView buildTextView() {   
  33.         AbsListView.LayoutParams params = new AbsListView.LayoutParams(   
  34.                 ViewGroup.LayoutParams.FILL_PARENT, 55);   
  35.         TextView textView = new TextView(this.context);   
  36.         textView.setLayoutParams(params);   
  37.         textView.setTextSize(15.0f);   
  38.         textView.setGravity(Gravity.LEFT);   
  39.         textView.setPadding(70833);   
  40.         return textView;   
  41.     }   
  42.   
  43.     @Override  
  44.     public View getChildView(int groupPosition, int childPosition,   
  45.             boolean isLastChild, View convertView, ViewGroup parent) {   
  46.         TextView textView = this.buildTextView();   
  47.         textView.setText(this.getChild(groupPosition, childPosition).toString());   
  48.         return textView;   
  49.     }   
  50.   
  51.     @Override  
  52.     public int getChildrenCount(int groupPosition) {   
  53.         return this.children[groupPosition].length;   
  54.     }   
  55.   
  56.     @Override  
  57.     public Object getGroup(int groupPosition) {   
  58.         return this.groups[groupPosition];   
  59.     }   
  60.   
  61.     @Override  
  62.     public int getGroupCount() {   
  63.         return this.groups.length;   
  64.     }   
  65.   
  66.     @Override  
  67.     public long getGroupId(int groupPosition) {   
  68.         return groupPosition;   
  69.     }   
  70.   
  71.     @Override  
  72.     public View getGroupView(int groupPosition, boolean isExpanded,   
  73.             View convertView, ViewGroup parent) {   
  74.         TextView textView = this.buildTextView();   
  75.         textView.setText(this.getGroup(groupPosition).toString());   
  76.         return textView;   
  77.     }   
  78.   
  79.     @Override  
  80.     public boolean hasStableIds() {   
  81.         return true;   
  82.     }   
  83.   
  84.     @Override  
  85.     public boolean isChildSelectable(int groupPosition, int childPosition) {   
  86.         return true;   
  87.     }   
  88.   
  89. }  
package com.iflytek.demo;import android.content.Context;import android.view.Gravity;import android.view.View;import android.view.ViewGroup;import android.widget.AbsListView;import android.widget.BaseExpandableListAdapter;import android.widget.TextView;public class MyExpandableListAdapter extends BaseExpandableListAdapter {	// 一个组下肯定会有多个子选项	private String[] groups = new String[] { "我的好友", "家人", "同事", "黑名单" }; // 组名称	private String[][] children = new String[][] { { "赵一", "吴二", "张三", "李四" },			{ "父亲", "母亲" }, { "王五", "赵六", "王八" }, { "票贩子", "造假商" } }; // 这里面定义的子选项	private Context context = null;	public MyExpandableListAdapter(Context context) {		this.context = context;	}	@Override	public Object getChild(int groupPosition, int childPosition) { // 取得指定的子选项		return this.children[groupPosition][childPosition];	}	@Override	public long getChildId(int groupPosition, int childPosition) {		return childPosition;	}	private TextView buildTextView() {		AbsListView.LayoutParams params = new AbsListView.LayoutParams(				ViewGroup.LayoutParams.FILL_PARENT, 55);		TextView textView = new TextView(this.context);		textView.setLayoutParams(params);		textView.setTextSize(15.0f);		textView.setGravity(Gravity.LEFT);		textView.setPadding(70, 8, 3, 3);		return textView;	}	@Override	public View getChildView(int groupPosition, int childPosition,			boolean isLastChild, View convertView, ViewGroup parent) {		TextView textView = this.buildTextView();		textView.setText(this.getChild(groupPosition, childPosition).toString());		return textView;	}	@Override	public int getChildrenCount(int groupPosition) {		return this.children[groupPosition].length;	}	@Override	public Object getGroup(int groupPosition) {		return this.groups[groupPosition];	}	@Override	public int getGroupCount() {		return this.groups.length;	}	@Override	public long getGroupId(int groupPosition) {		return groupPosition;	}	@Override	public View getGroupView(int groupPosition, boolean isExpanded,			View convertView, ViewGroup parent) {		TextView textView = this.buildTextView();		textView.setText(this.getGroup(groupPosition).toString());		return textView;	}	@Override	public boolean hasStableIds() {		return true;	}	@Override	public boolean isChildSelectable(int groupPosition, int childPosition) {		return true;	}}
 

 

main.xml

Xml代码 复制代码  收藏代码
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent"  
  5.     android:orientation="vertical" >  
  6.   
  7.     <ExpandableListView  
  8.         android:id="@+id/elistview"  
  9.         android:layout_width="fill_parent"  
  10.         android:layout_height="wrap_content" />  
  11.   
  12. </LinearLayout>  
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:orientation="vertical" >    <ExpandableListView        android:id="@+id/elistview"        android:layout_width="fill_parent"        android:layout_height="wrap_content" /></LinearLayout>
 

 

3 、上下文菜单

ExpandableListView03Activity.java

Java代码 复制代码  收藏代码
  1. package com.iflytek.demo;   
  2.   
  3. import android.app.Activity;   
  4. import android.os.Bundle;   
  5. import android.view.ContextMenu;   
  6. import android.view.ContextMenu.ContextMenuInfo;   
  7. import android.view.View;   
  8. import android.widget.ExpandableListAdapter;   
  9. import android.widget.ExpandableListView;   
  10. import android.widget.ExpandableListView.OnChildClickListener;   
  11. import android.widget.ExpandableListView.OnGroupClickListener;   
  12. import android.widget.ExpandableListView.OnGroupCollapseListener;   
  13. import android.widget.ExpandableListView.OnGroupExpandListener;   
  14. import android.widget.Toast;   
  15.   
  16. public class ExpandableListView03Activity extends Activity {   
  17.     private ExpandableListView elistview = null;   
  18.     private ExpandableListAdapter adapter = null;   
  19.   
  20.     /** Called when the activity is first created. */  
  21.     @Override  
  22.     public void onCreate(Bundle savedInstanceState) {   
  23.         super.onCreate(savedInstanceState);   
  24.         setContentView(R.layout.main);   
  25.         this.elistview = (ExpandableListView) super  
  26.                 .findViewById(R.id.elistview);   
  27.         this.adapter = new MyExpandableListAdapter(this);   
  28.         this.elistview.setAdapter(this.adapter);   
  29.   
  30.         super.registerForContextMenu(this.elistview);   
  31.   
  32.         this.elistview.setOnChildClickListener(new OnChildClickListenerImpl());   
  33.         this.elistview.setOnGroupClickListener(new OnGroupClickListenerImpl());   
  34.         this.elistview   
  35.                 .setOnGroupExpandListener(new OnGroupExpandListenerImpl());   
  36.         this.elistview   
  37.                 .setOnGroupCollapseListener(new OnGroupCollapseListenerImpl());   
  38.     }   
  39.   
  40.     private class OnChildClickListenerImpl implements OnChildClickListener {   
  41.   
  42.         @Override  
  43.         public boolean onChildClick(ExpandableListView parent, View v,   
  44.                 int groupPosition, int childPosition, long id) {   
  45.             Toast.makeText(   
  46.                     ExpandableListView03Activity.this,   
  47.                     "子选项被选中,groupPosition = " + groupPosition   
  48.                             + ",childPosition = " + childPosition,   
  49.                     Toast.LENGTH_SHORT).show();   
  50.             return false;   
  51.         }   
  52.     }   
  53.   
  54.     private class OnGroupClickListenerImpl implements OnGroupClickListener {   
  55.   
  56.         @Override  
  57.         public boolean onGroupClick(ExpandableListView parent, View v,   
  58.                 int groupPosition, long id) {   
  59.             Toast.makeText(ExpandableListView03Activity.this,   
  60.                     "分组被选中,groupPosition = " + groupPosition,   
  61.                     Toast.LENGTH_SHORT).show();   
  62.             return false;   
  63.         }   
  64.   
  65.     }   
  66.   
  67.     private class OnGroupExpandListenerImpl implements OnGroupExpandListener {   
  68.   
  69.         @Override  
  70.         public void onGroupExpand(int groupPosition) {   
  71.             Toast.makeText(ExpandableListView03Activity.this,   
  72.                     "打开分组,groupPosition = " + groupPosition, Toast.LENGTH_SHORT)   
  73.                     .show();   
  74.   
  75.         }   
  76.   
  77.     }   
  78.   
  79.     private class OnGroupCollapseListenerImpl implements  
  80.             OnGroupCollapseListener {   
  81.   
  82.         @Override  
  83.         public void onGroupCollapse(int groupPosition) {   
  84.             Toast.makeText(ExpandableListView03Activity.this,   
  85.                     "关闭分组,groupPosition = " + groupPosition, Toast.LENGTH_SHORT)   
  86.                     .show();   
  87.   
  88.         }   
  89.   
  90.     }   
  91.   
  92.     @Override  
  93.     public void onCreateContextMenu(ContextMenu menu, View v,   
  94.             ContextMenuInfo menuInfo) {   
  95.         super.onCreateContextMenu(menu, v, menuInfo);   
  96.         ExpandableListView.ExpandableListContextMenuInfo info = (ExpandableListView.ExpandableListContextMenuInfo) menuInfo;   
  97.         int type = ExpandableListView   
  98.                 .getPackedPositionType(info.packedPosition); // 取得菜单项   
  99.         int group = ExpandableListView   
  100.                 .getPackedPositionGroup(info.packedPosition); // 取得所在组的索引   
  101.         int child = ExpandableListView   
  102.                 .getPackedPositionChild(info.packedPosition); // 取得子菜单项的索引   
  103.         Toast.makeText(ExpandableListView03Activity.this,   
  104.                 "type = " + type + ",group = " + group + ",child = " + child,   
  105.                 Toast.LENGTH_SHORT).show();   
  106.     }   
  107.   
  108. }  

你可能感兴趣的:(PopupWindow,Zoomcontrols)