Android弹出窗口与QQ好友分组列表(21)

  • 弹出窗口,其实就是在改变某一状态弹出的窗口

首先定义Popupwindow的布局文件popupwindow.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/popinfo"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="请选择您的状态" />

    <RadioGroup
        android:id="@+id/changestatus"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >

        <RadioButton
            android:id="@+id/online"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:checked="true"
            android:text="在线" />

        <RadioButton
            android:id="@+id/offline"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="离线" />

        <RadioButton
            android:id="@+id/stealth"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="隐身" />
    </RadioGroup>

    <Button
        android:id="@+id/cancel"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="取消" />

</LinearLayout>

 然后是main的主布局文件,我们一个组件显示状态,一个改变状态

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/LinearLayout1"
    android:layout_width="fill_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
  
    tools:context=".MainActivity" >

    <TextView
          android:id="@+id/statusinfo"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="当前用户状态:在线" />

    <Button
        android:id="@+id/popbut"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="状态" />

</LinearLayout>

Activity代码:

public class MainActivity extends Activity {

	private Button popbut=null;//状态改变按钮
	private RadioGroup changestatus=null;//选项
	private TextView statusinfo=null;//显示状态
	private Button cancel=null;//关闭弹出
	private PopupWindow popwin=null;//弹出窗口
	private View popview=null;//定义弹出窗口view
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		this.popbut=(Button)super.findViewById(R.id.popbut);//状态按钮
		this.statusinfo=(TextView)super.findViewById(R.id.statusinfo);//显示状态
		this.popbut.setOnClickListener(new OnClickListenerlmpl());
	}

	private class OnClickListenerlmpl implements OnClickListener{

		@Override
		public void onClick(View arg0) {
		LayoutInflater inflater=LayoutInflater.from(MainActivity.this);
		popview=inflater.inflate(R.layout.popwindow, null);
		popwin=new PopupWindow(popview,300,200);//popview视图显示在弹出窗口上
		changestatus=(RadioGroup)popview.findViewById(R.id.changestatus);
		cancel=(Button)popview.findViewById(R.id.cancel);
	    changestatus.setOnCheckedChangeListener(new Check());
	    cancel.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View arg0) {
				popwin.dismiss();//关掉弹出窗口
				
			}
	    	
	    });
	    popwin.showAtLocation(popbut, Gravity.CENTER, 0,0);//显示弹出窗口
	    
		
		
			
		}
		
	}
	private class Check implements OnCheckedChangeListener{

		@Override
		public void onCheckedChanged(RadioGroup group, int ischeckedid) {
			//得到选中按钮
			RadioButton but=(RadioButton)MainActivity.this.popview.findViewById(group.getCheckedRadioButtonId());
			statusinfo.setText("当前用户状态:"+but.getText().toString());
			popwin.dismiss();
			
		}
		
	}
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

}

这里讲一下:LayoutInflater

其实在Activity里面就使用了LayoutInflater来载入界面, 通过getSystemService(Context.LAYOUT_INFLATER_SERVICE)方法可以获得一个 LayoutInflater, 也可以通过LayoutInflater inflater = getLayoutInflater();来获得.然后使用inflate方法来载入layout的xml, 

 
 
 
首先我们要知道,什么是已经被载入的layout,什么是还没有载入的.我们启动一个应用,与入口Activity相关的layout{常见的是main.xml}就是被载入的,即在Oncreate()中的.而其他的layout是没有被载入的.就要动态载入了或通过另一个activity.
 
在实际开发种LayoutInflater这个类还是非常有用的,它的作用类似于 findViewById(),
不同点是LayoutInflater是用来找layout下xml布局文件,并且实例化!而findViewById()是找具体xml下的具体 widget控件.
实现效果如下:

Android弹出窗口与QQ好友分组列表(21)
 
Android弹出窗口与QQ好友分组列表(21)
  • 接下来我们来认识树型组件:ExpandableListView

我们敏锐的观察到后缀的listview,listview虽然能给用户提供列表显示功能,但是相对列表数据进行分类管理,则要使用树型组件了

这也是我们做QQ好友分组列表的基础,接下来上手:

与listview一样,先定义适配器:

public class ListAdapter extends BaseExpandableListAdapter{

	private String[] group={"我的好友","家人","蓝杰","黑名单"};
	private String[][] children={{"豆豆","婷婷","图图","乐乐",},{"父亲","母亲","三婶","四叔",},
			{"胡哥","强哥","熊哥",},{"前男友","前女友"}};
	private Context context=null;//保存上下文对象
	public ListAdapter(Context context){
		this.context=context;
	}
	
	public Object getChild(int groupPosition, int childPosition) {     //取得指定子项
		// TODO Auto-generated method stub
		return this.children[groupPosition][childPosition];
	}

	@Override
	public long getChildId(int groupPosition, int childPosition) {
		// TODO Auto-generated method stub
		return childPosition;
	}

	@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;

	}
	public TextView buildTextView(){
		AbsListView.LayoutParams params = new AbsListView.LayoutParams(
				ViewGroup.LayoutParams.FILL_PARENT, 35);//指定布局参数
		TextView textView = new TextView(this.context) ;//创建textview
		textView.setLayoutParams(params) ;
		textView.setTextSize(15.0f) ;
		textView.setGravity(Gravity.LEFT) ;//左对齐
		textView.setPadding(40, 8, 3, 3) ;//间距
		return textView ;

	}

	@Override
	public int getChildrenCount(int groupPosition) {
		// TODO Auto-generated method stub
		return this.children[groupPosition].length;
	}

	@Override
	public Object getGroup(int groupPosition) {
		// TODO Auto-generated method stub
		return this.group[groupPosition];
	}

	@Override
	public int getGroupCount() {
		// TODO Auto-generated method stub
		return this.group.length;
	}

	@Override
	public long getGroupId(int groupPosition) {
		// TODO Auto-generated method stub
		return groupPosition;
	}

	@Override
	public View getGroupView(int groupPosition, boolean arg1, View arg2, ViewGroup arg3) {
		TextView textView = this.buildTextView() ;
		textView.setText(this.getGroup(groupPosition).toString()) ;
	
		return textView;

	}

	@Override
	public boolean hasStableIds() {
		// TODO Auto-generated method stub
		return true;
	}

	@Override
	public boolean isChildSelectable(int arg0, int arg1) {
		// TODO Auto-generated method stub
		return true;
	}

}

布局文件:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/LinearLayout1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    
    tools:context=".MainActivity" >

    <ExpandableListView
        android:id="@+id/eListView"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >
    </ExpandableListView>

</LinearLayout>

Activity:

public class MainActivity extends Activity {

	private ExpandableListView elistview = null;
	private ExpandableListAdapter adapter = null;

	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		this.elistview = (ExpandableListView) super
				.findViewById(R.id.eListView);
		this.adapter = new ListAdapter(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(
					MainActivity.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(MainActivity.this,
					"分组被选中,groupPosition = " + groupPosition,
					Toast.LENGTH_SHORT).show();
			return false;
		}

	}

	private class OnGroupExpandListenerImpl implements OnGroupExpandListener {

		@Override
		public void onGroupExpand(int groupPosition) {
			Toast.makeText(MainActivity.this,
					"打开分组,groupPosition = " + groupPosition, Toast.LENGTH_SHORT)
					.show();

		}

	}

	private class OnGroupCollapseListenerImpl implements
			OnGroupCollapseListener {

		@Override
		public void onGroupCollapse(int groupPosition) {
			Toast.makeText(MainActivity.this,
					"关闭分组,groupPosition = " + groupPosition, Toast.LENGTH_SHORT)
					.show();

		}

	}
	public void onCreateContextMenu(ContextMenu menu, View v,
			ContextMenuInfo menuInfo) {
		super.onCreateContextMenu(menu, v, menuInfo);
		ExpandableListView.ExpandableListContextMenuInfo info = (ExpandableListView.ExpandableListContextMenuInfo) menuInfo;
		int type = ExpandableListView
				.getPackedPositionType(info.packedPosition); // 取得菜单项
		int group = ExpandableListView
				.getPackedPositionGroup(info.packedPosition); // 取得所在组的索引
		int child = ExpandableListView
				.getPackedPositionChild(info.packedPosition); // 取得子菜单项的索引
		Toast.makeText(MainActivity.this,
				"type = " + type + ",group = " + group + ",child = " + child,
				Toast.LENGTH_SHORT).show();
	}



	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

}

实现效果如下:


Android弹出窗口与QQ好友分组列表(21)
 
Android弹出窗口与QQ好友分组列表(21)
 


  •  
 

相关推荐